Class: Win32::EventLog

Inherits:
Object
  • Object
show all
Extended by:
Windows::EventLogFunctions
Includes:
Windows::EventLogConstants, Windows::EventLogFunctions, Windows::EventLogStructs
Defined in:
lib/win32/eventlog.rb

Overview

The EventLog class encapsulates an Event Log source and provides methods for interacting with that source.

Defined Under Namespace

Classes: Error, EventLogStruct

Constant Summary collapse

VERSION =

The version of the win32-eventlog library

'0.6.7'.freeze
FORWARDS_READ =

The log is read in chronological order, i.e. oldest to newest.

EVENTLOG_FORWARDS_READ
BACKWARDS_READ =

The log is read in reverse chronological order, i.e. newest to oldest.

EVENTLOG_BACKWARDS_READ
SEEK_READ =

Begin reading from a specific record.

EVENTLOG_SEEK_READ
SEQUENTIAL_READ =

Read the records sequentially. If this is the first read operation, the EVENTLOG_FORWARDS_READ or EVENTLOG_BACKWARDS_READ flags determines which record is read first.

EVENTLOG_SEQUENTIAL_READ
SUCCESS =

Information event, an event that describes the successful operation of an application, driver or service.

EVENTLOG_SUCCESS
ERROR_TYPE =

Error event, an event that indicates a significant problem such as loss of data or functionality.

EVENTLOG_ERROR_TYPE
WARN_TYPE =

Warning event, an event that is not necessarily significant but may indicate a possible future problem.

EVENTLOG_WARNING_TYPE
INFO_TYPE =

Information event, an event that describes the successful operation of an application, driver or service.

EVENTLOG_INFORMATION_TYPE
AUDIT_SUCCESS =

Success audit event, an event that records an audited security attempt that is successful.

EVENTLOG_AUDIT_SUCCESS
AUDIT_FAILURE =

Failure audit event, an event that records an audited security attempt that fails.

EVENTLOG_AUDIT_FAILURE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = 'Application', server = nil, file = nil) ⇒ EventLog

Opens a handle to the new EventLog source on server, or the local machine if no host is specified. Typically, your source will be ‘Application, ’Security’ or ‘System’, although you can specify a custom log file as well.

If a custom, registered log file name cannot be found, the event logging service opens the ‘Application’ log file. This is the behavior of the underlying Windows function, not my own doing.

Raises:

  • (TypeError)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/win32/eventlog.rb', line 94

def initialize(source = 'Application', server = nil, file = nil)
  @source = source || 'Application' # In case of explicit nil
  @server = server
  @file   = file

  # Avoid potential segfaults from win32-api
  raise TypeError unless @source.is_a?(String)
  raise TypeError unless @server.is_a?(String) if @server

  if file.nil?
    function = 'OpenEventLog'
    @handle = OpenEventLog(@server, @source)
  else
    function = 'OpenBackupEventLog'
    @handle = OpenBackupEventLog(@server, @file)
  end

  if @handle == 0
    raise SystemCallError.new(function, FFI.errno)
  end

  # Ensure the handle is closed at the end of a block
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
end

Instance Attribute Details

#fileObject (readonly)

The name of the file used in the EventLog.open_backup method. This is set to nil if the file was not opened using the EventLog.open_backup method.



83
84
85
# File 'lib/win32/eventlog.rb', line 83

def file
  @file
end

#serverObject (readonly)

The name of the server which the event log is reading from.



77
78
79
# File 'lib/win32/eventlog.rb', line 77

def server
  @server
end

#sourceObject (readonly)

The name of the event log source. This will typically be ‘Application’, ‘System’ or ‘Security’, but could also refer to a custom event log source.



73
74
75
# File 'lib/win32/eventlog.rb', line 73

def source
  @source
end

Class Method Details

.add_event_source(args) ⇒ Object

Adds an event source to the registry. Returns the disposition, which is either REG_CREATED_NEW_KEY (1) or REG_OPENED_EXISTING_KEY (2).

The following are valid keys:

  • source # Source name. Set to “Application” by default

  • key_name # Name stored as the registry key

  • category_count # Number of supported (custom) categories

  • event_message_file # File (dll) that defines events

  • category_message_file # File (dll) that defines categories

  • parameter_message_file # File (dll) that contains values for variables in the event description.

  • supported_types # See the ‘event types’ constants

Of these keys, only key_name is mandatory. An ArgumentError is raised if you attempt to use an invalid key. If supported_types is not specified then the following value is used:

EventLog::ERROR_TYPE | EventLog::WARN_TYPE | EventLog::INFO_TYPE

The event_message_file and category_message_file are typically, though not necessarily, the same file. See the documentation on .mc files for more details.

You will need administrative privileges to use this method.

Raises:

  • (TypeError)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/win32/eventlog.rb', line 171

def self.add_event_source(args)
  raise TypeError unless args.is_a?(Hash)

  valid_keys = %w[
    source
    key_name
    category_count
    event_message_file
    category_message_file
    parameter_message_file
    supported_types
  ]

  key_base = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\"

  # Default values
  hash = {
    'source'          => 'Application',
    'supported_types' => ERROR_TYPE | WARN_TYPE | INFO_TYPE
  }

  # Validate the keys, and convert symbols and case to lowercase strings.
  args.each{ |key, val|
    key = key.to_s.downcase
    unless valid_keys.include?(key)
      raise ArgumentError, "invalid key '#{key}'"
    end
    hash[key] = val
  }

  # The key_name must be specified
  unless hash['key_name']
    raise ArgumentError, 'no event_type specified'
  end

  hkey = FFI::MemoryPointer.new(:uintptr_t)
  disposition = FFI::MemoryPointer.new(:ulong)

  key = key_base + hash['source']

  rv = RegCreateKeyEx(
    HKEY_LOCAL_MACHINE,
    key,
    0,
    nil,
    REG_OPTION_NON_VOLATILE,
    KEY_WRITE,
    nil,
    hkey,
    disposition
  )

  if rv != ERROR_SUCCESS
    raise SystemCallError.new('RegCreateKeyEx', rv)
  end

  hkey = hkey.read_pointer.to_i
  data = "%SystemRoot%\\System32\\config\\#{hash['source']}.evt"

  begin
    rv = RegSetValueEx(
      hkey,
      'File',
      0,
      REG_EXPAND_SZ,
      data,
      data.size
    )

    if rv != ERROR_SUCCESS
      raise SystemCallError.new('RegSetValueEx', rv)
    end
  ensure
    RegCloseKey(hkey)
  end

  hkey = FFI::MemoryPointer.new(:uintptr_t)
  disposition = FFI::MemoryPointer.new(:ulong)

  key  = key_base << hash['source'] << "\\" << hash['key_name']

  begin
    rv = RegCreateKeyEx(
      HKEY_LOCAL_MACHINE,
      key,
      0,
      nil,
      REG_OPTION_NON_VOLATILE,
      KEY_WRITE,
      nil,
      hkey,
      disposition
    )

    if rv != ERROR_SUCCESS
      raise SystemCallError.new('RegCreateKeyEx', rv)
    end

    hkey = hkey.read_pointer.to_i

    if hash['category_count']
      data = FFI::MemoryPointer.new(:ulong).write_ulong(hash['category_count'])

      rv = RegSetValueEx(
        hkey,
        'CategoryCount',
        0,
        REG_DWORD,
        data,
        data.size
      )

      if rv != ERROR_SUCCESS
        raise SystemCallError.new('RegSetValueEx', rv)
      end
    end

    if hash['category_message_file']
      data = File.expand_path(hash['category_message_file'])
      data = FFI::MemoryPointer.from_string(data)

      rv = RegSetValueEx(
        hkey,
        'CategoryMessageFile',
        0,
        REG_EXPAND_SZ,
        data,
        data.size
      )

      if rv != ERROR_SUCCESS
        raise SystemCallError.new('RegSetValueEx', rv)
      end
    end

    if hash['event_message_file']
      data = File.expand_path(hash['event_message_file'])
      data = FFI::MemoryPointer.from_string(data)

      rv = RegSetValueEx(
        hkey,
        'EventMessageFile',
        0,
        REG_EXPAND_SZ,
        data,
        data.size
      )

      if rv != ERROR_SUCCESS
        raise SystemCallError.new('RegSetValueEx', rv)
      end
    end

    if hash['parameter_message_file']
      data = File.expand_path(hash['parameter_message_file'])
      data = FFI::MemoryPointer.from_string(data)

      rv = RegSetValueEx(
        hkey,
        'ParameterMessageFile',
        0,
        REG_EXPAND_SZ,
        data,
        data.size
      )

      if rv != ERROR_SUCCESS
        raise SystemCallError.new('RegSetValueEx', rv)
      end
    end

    data = FFI::MemoryPointer.new(:ulong).write_ulong(hash['supported_types'])

    rv = RegSetValueEx(
      hkey,
      'TypesSupported',
      0,
      REG_DWORD,
      data,
      data.size
    )

    if rv != ERROR_SUCCESS
      raise SystemCallError.new('RegSetValueEx', rv)
    end
  ensure
    RegCloseKey(hkey)
  end

  disposition.read_ulong
end

.open_backup(file, source = 'Application', server = nil, &block) ⇒ Object

Nearly identical to EventLog.open, except that the source is a backup file and not an event source (and there is no default).

Raises:

  • (TypeError)


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/win32/eventlog.rb', line 133

def self.open_backup(file, source = 'Application', server = nil, &block)
  @file   = file
  @source = source
  @server = server

  # Avoid potential segfaults from win32-api
  raise TypeError unless @file.is_a?(String)
  raise TypeError unless @source.is_a?(String)
  raise TypeError unless @server.is_a?(String) if @server

  self.new(source, server, file, &block)
end

.read(source = 'Application', server = nil, flags = nil, offset = 0) ⇒ Object

This class method is nearly identical to the EventLog#read instance method, except that it takes a source and server as the first two arguments.



608
609
610
611
612
613
614
615
616
# File 'lib/win32/eventlog.rb', line 608

def self.read(source='Application', server=nil, flags=nil, offset=0)
  self.new(source, server){ |log|
    if block_given?
      log.read(flags, offset){ |els| yield els }
    else
      return log.read(flags, offset)
    end
  }
end

Instance Method Details

#backup(file) ⇒ Object

Backs up the event log to file. Note that you cannot backup to a file that already exists or a Error will be raised.

Raises:

  • (TypeError)


366
367
368
369
370
371
# File 'lib/win32/eventlog.rb', line 366

def backup(file)
  raise TypeError unless file.is_a?(String)
  unless BackupEventLog(@handle, file)
    raise SystemCallError.new('BackupEventLog', FFI.errno)
  end
end

#clear(backup_file = nil) ⇒ Object

Clears the EventLog. If backup_file is provided, it backs up the event log to that file first.

Raises:

  • (TypeError)


376
377
378
379
380
381
382
# File 'lib/win32/eventlog.rb', line 376

def clear(backup_file = nil)
  raise TypeError unless backup_file.is_a?(String) if backup_file

  unless ClearEventLog(@handle, backup_file)
    raise SystemCallError.new('ClearEventLog', FFI.errno)
  end
end

#closeObject

Closes the EventLog handle. The handle is automatically closed for you if you use the block form of EventLog.new.



387
388
389
# File 'lib/win32/eventlog.rb', line 387

def close
  CloseEventLog(@handle)
end

#full?Boolean

Indicates whether or not the event log is full.

Returns:

  • (Boolean)


393
394
395
396
397
398
399
400
401
402
# File 'lib/win32/eventlog.rb', line 393

def full?
  ptr = FFI::MemoryPointer.new(:ulong, 1)
  needed = FFI::MemoryPointer.new(:ulong)

  unless GetEventLogInformation(@handle, 0, ptr, ptr.size, needed)
    raise SystemCallError.new('GetEventLogInformation', FFI.errno)
  end

  ptr.read_ulong != 0
end

#notify_change(&block) ⇒ Object

Yields an EventLogStruct every time a record is written to the event log. Unlike EventLog#tail, this method breaks out of the block after the event.

Raises an Error if no block is provided.



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/win32/eventlog.rb', line 436

def notify_change(&block)
  unless block_given?
    raise ArgumentError, 'block missing for notify_change'
  end

  # Reopen the handle because the NotifyChangeEventLog() function will
  # choke after five or six reads otherwise.
  @handle = OpenEventLog(@server, @source)

  if @handle == 0
    raise SystemCallError.new('OpenEventLog', FFI.errno)
  end

  event = CreateEvent(nil, 0, 0, nil)

  unless NotifyChangeEventLog(@handle, event)
    raise SystemCallError.new('NotifyChangeEventLog', FFI.errno)
  end

  wait_result = WaitForSingleObject(event, INFINITE)

  begin
    if wait_result == WAIT_FAILED
      raise SystemCallError.new('WaitForSingleObject', FFI.errno)
    else
      last = read_last_event
      block.call(last)
    end
  ensure
    CloseHandle(event)
  end

  self
end

#oldest_record_numberObject

Returns the absolute record number of the oldest record. Note that this is not guaranteed to be 1 because event log records can be overwritten.



408
409
410
411
412
413
414
415
416
# File 'lib/win32/eventlog.rb', line 408

def oldest_record_number
  rec = FFI::MemoryPointer.new(:ulong)

  unless GetOldestEventLogRecord(@handle, rec)
    raise SystemCallError.new('GetOldestEventLogRecord', FFI.errno)
  end

  rec.read_ulong
end

#read(flags = nil, offset = 0) ⇒ Object

Iterates over each record in the event log, yielding a EventLogStruct for each record. The offset value is only used when used in conjunction with the EventLog::SEEK_READ flag. Otherwise, it is ignored. If no flags are specified, then the default flags are:

EventLog::SEQUENTIAL_READ | EventLog::FORWARDS_READ

Note that, if you’re performing a SEEK_READ, then the offset must refer to a record number that actually exists. The default of 0 may or may not work for your particular event log.

The EventLogStruct struct contains the following members:

  • record_number # Fixnum

  • time_generated # Time

  • time_written # Time

  • event_id # Fixnum

  • event_type # String

  • category # String

  • source # String

  • computer # String

  • user # String or nil

  • description # String or nil

  • string_inserts # An array of Strings or nil

If no block is given the method returns an array of EventLogStruct’s.



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/win32/eventlog.rb', line 531

def read(flags = nil, offset = 0)
  buf    = FFI::MemoryPointer.new(:char, BUFFER_SIZE)
  read   = FFI::MemoryPointer.new(:ulong)
  needed = FFI::MemoryPointer.new(:ulong)
  array  = []
  lkey   = HKEY_LOCAL_MACHINE

  unless flags
    flags = FORWARDS_READ | SEQUENTIAL_READ
  end

  if @server
    hkey = FFI::MemoryPointer.new(:uintptr_t)
    if RegConnectRegistry(@server, HKEY_LOCAL_MACHINE, hkey) != 0
      raise SystemCallError.new('RegConnectRegistry', FFI.errno)
    end
    lkey = hkey.read_pointer.to_i
  end

  while ReadEventLog(@handle, flags, offset, buf, buf.size, read, needed) ||
    FFI.errno == ERROR_INSUFFICIENT_BUFFER

    if FFI.errno == ERROR_INSUFFICIENT_BUFFER
      needed = needed.read_ulong / EVENTLOGRECORD.size
      buf = FFI::MemoryPointer.new(EVENTLOGRECORD, needed)
      unless ReadEventLog(@handle, flags, offset, buf, buf.size, read, needed)
        raise SystemCallError.new('ReadEventLog', FFI.errno)
      end
    end

    dwread = read.read_ulong

    while dwread > 0
      struct = EventLogStruct.new
      record = EVENTLOGRECORD.new(buf)

      struct.source         = buf.read_bytes(buf.size)[56..-1][/^[^\0]*/]
      struct.computer       = buf.read_bytes(buf.size)[56 + struct.source.length + 1..-1][/^[^\0]*/]
      struct.record_number  = record[:RecordNumber]
      struct.time_generated = Time.at(record[:TimeGenerated])
      struct.time_written   = Time.at(record[:TimeWritten])
      struct.event_id       = record[:EventID] & 0x0000FFFF
      struct.event_type     = get_event_type(record[:EventType])
      struct.user           = get_user(record)
      struct.category       = record[:EventCategory]
      struct.string_inserts, struct.description = get_description(buf, struct.source, lkey)

      struct.freeze # This is read-only information

      if block_given?
        yield struct
      else
        array.push(struct)
      end

      if flags & EVENTLOG_BACKWARDS_READ > 0
        offset = record[:RecordNumber] - 1
      else
        offset = record[:RecordNumber] + 1
      end

      length = record[:Length]

      dwread -= length
      buf += length
    end

    buf  = FFI::MemoryPointer.new(:char, BUFFER_SIZE)
  end

  block_given? ? nil : array
end

#read_last_eventObject

Reads the last event record.



734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/win32/eventlog.rb', line 734

def read_last_event
  buf    = FFI::MemoryPointer.new(:char, BUFFER_SIZE)
  read   = FFI::MemoryPointer.new(:ulong)
  needed = FFI::MemoryPointer.new(:ulong)
  lkey   = HKEY_LOCAL_MACHINE

  flags = EVENTLOG_BACKWARDS_READ | EVENTLOG_SEQUENTIAL_READ

  unless ReadEventLog(@handle, flags, 0, buf, buf.size, read, needed)
    if FFI.errno == ERROR_INSUFFICIENT_BUFFER
      needed = needed.read_ulong / EVENTLOGRECORD.size
      buf = FFI::MemoryPointer.new(EVENTLOGRECORD, needed)
      unless ReadEventLog(@handle, flags, 0, buf, buf.size, read, needed)
        raise SystemCallError.new('ReadEventLog', FFI.errno)
      end
    else
      raise SystemCallError.new('ReadEventLog', FFI.errno)
    end
  end

  if @server
    hkey = FFI::MemoryPointer.new(:uintptr_t)
    if RegConnectRegistry(@server, HKEY_LOCAL_MACHINE, hkey) != 0
      raise SystemCallError.new('RegConnectRegistry', FFI.errno)
    end
    lkey = hkey.read_pointer.to_i
  end

  record = EVENTLOGRECORD.new(buf)

  struct = EventLogStruct.new
  struct.source         = buf.read_bytes(buf.size)[56..-1][/^[^\0]*/]
  struct.computer       = buf.read_bytes(buf.size)[56 + struct.source.length + 1..-1][/^[^\0]*/]
  struct.record_number  = record[:RecordNumber]
  struct.time_generated = Time.at(record[:TimeGenerated])
  struct.time_written   = Time.at(record[:TimeWritten])
  struct.event_id       = record[:EventID] & 0x0000FFFF
  struct.event_type     = get_event_type(record[:EventType])
  struct.user           = get_user(record)
  struct.category       = record[:EventCategory]
  struct.string_inserts, struct.description = get_description(buf, struct.source, lkey)

  struct.freeze # This is read-only information

  struct
end

#report_event(args) ⇒ Object Also known as: write

Writes an event to the event log. The following are valid keys:

  • source # Event log source name. Defaults to “Application”.

  • event_id # Event ID (defined in event message file).

  • category # Event category (defined in category message file).

  • user_sid # object of Win32::Security::SID or SID struct (e.g returned by Win32::Security::SID.open(‘username’).sid).

  • data # String, or array of strings, that is written to the log.

  • event_type # Type of event, e.g. EventLog::ERROR_TYPE, etc.

The event_type keyword is the only mandatory keyword. The others are optional. Although the source defaults to “Application”, I recommend that you create an application specific event source and use that instead. See the ‘EventLog.add_event_source’ method for more details.

The event_id and category values are defined in the message file(s) that you created for your application. See the tutorial.txt file for more details on how to create a message file.

An ArgumentError is raised if you attempt to use an invalid key.

Raises:

  • (TypeError)


639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/win32/eventlog.rb', line 639

def report_event(args)
  raise TypeError unless args.is_a?(Hash)

  valid_keys  = %w[source event_id category data event_type user_sid]
  num_strings = 0

  # Default values
  hash = {
    'source'   => @source,
    'event_id' => 0,
    'category' => 0,
    'data'     => 0,
    'user_sid' => nil
  }

  # Validate the keys, and convert symbols and case to lowercase strings.
  args.each{ |key, val|
    key = key.to_s.downcase
    unless valid_keys.include?(key)
      raise ArgumentError, "invalid key '#{key}'"
    end
    hash[key] = val
  }

  # The event_type must be specified
  unless hash['event_type']
    raise ArgumentError, 'no event_type specified'
  end

  handle = RegisterEventSource(@server, hash['source'])

  if handle == 0
    raise SystemCallError.new('RegisterEventSource', FFI.errno)
  end

  if hash['data'].is_a?(String)
    strptrs = []
    strptrs << FFI::MemoryPointer.from_string(hash['data'])
    strptrs << nil

    data = FFI::MemoryPointer.new(:pointer, strptrs.size)

    strptrs.each_with_index do |p, i|
      data[i].put_pointer(0, p)
    end

    num_strings = 1
  elsif hash['data'].is_a?(Array)
    strptrs = []

    hash['data'].each{ |str|
      strptrs << FFI::MemoryPointer.from_string(str)
    }

    strptrs << nil
    data = FFI::MemoryPointer.new(:pointer, strptrs.size)

    strptrs.each_with_index do |p, i|
      data[i].put_pointer(0, p)
    end

    num_strings = hash['data'].size
  else
    data = nil
    num_strings = 0
  end

  if hash['user_sid']
    sid = hash['user_sid'].respond_to?(:sid) ? hash['user_sid'].sid : hash['user_sid']
    user_sid = FFI::MemoryPointer.from_string(sid)
  else
    user_sid = nil
  end

  bool = ReportEvent(
    handle,
    hash['event_type'],
    hash['category'],
    hash['event_id'],
    user_sid,
    num_strings,
    0,
    data,
    nil
  )

  unless bool
    raise SystemCallError.new('ReportEvent', FFI.errno)
  end
end

#tail(frequency = 5) ⇒ Object

Yields an EventLogStruct every time a record is written to the event log, once every frequency seconds. Unlike EventLog#notify_change, this method does not break out of the block after the event. The read frequency is set to 5 seconds by default.

Raises an Error if no block is provided.

The delay between reads is due to the nature of the Windows event log. It is not really designed to be tailed in the manner of a Unix syslog, for example, in that not nearly as many events are typically recorded. It’s just not designed to be polled that heavily.



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/win32/eventlog.rb', line 483

def tail(frequency = 5)
  unless block_given?
    raise ArgumentError, 'block missing for tail'
  end

  old_total = total_records()
  flags     = FORWARDS_READ | SEEK_READ
  rec_num   = read_last_event.record_number

  while true
    new_total = total_records()
    if new_total != old_total
      rec_num = oldest_record_number() if full?
      read(flags, rec_num).each{ |log| yield log }
      old_total = new_total
      rec_num   = read_last_event.record_number + 1
    end
    sleep frequency
  end
end

#total_recordsObject

Returns the total number of records for the given event log.



420
421
422
423
424
425
426
427
428
# File 'lib/win32/eventlog.rb', line 420

def total_records
  total = FFI::MemoryPointer.new(:ulong)

  unless GetNumberOfEventLogRecords(@handle, total)
    raise SystemCallError.new('GetNumberOfEventLogRecords', FFI.errno)
  end

  total.read_ulong
end