Class: Puppet::Util::Windows::EventLog
- Extended by:
- FFI::Library
- Defined in:
- lib/puppet/util/windows.rb,
lib/puppet/util/windows/eventlog.rb
Defined Under Namespace
Classes: EventLogError
Constant Summary collapse
- EVENTLOG_ERROR_TYPE =
0x0001
- EVENTLOG_WARNING_TYPE =
0x0002
- EVENTLOG_INFORMATION_TYPE =
0x0004
- NULL_HANDLE =
These are duplicate definitions from Puppet::Util::Windows::ApiTypes, established here so this class can be standalone from Puppet, and public so we can reference them in tests.
0
- WIN32_FALSE =
0
Class Method Summary collapse
-
.to_native(level) ⇒ Array
Query event identifier info for a given log level.
Instance Method Summary collapse
-
#close ⇒ void
Close this instance’s event log handle.
-
#initialize(source_name = 'Puppet') ⇒ void
constructor
Register an event log handle for the application.
-
#report_event(args = {}) ⇒ void
Report an event to this instance’s event log handle.
Constructor Details
#initialize(source_name = 'Puppet') ⇒ void
Register an event log handle for the application
33 34 35 36 37 38 39 |
# File 'lib/puppet/util/windows/eventlog.rb', line 33 def initialize(source_name = 'Puppet') @eventlog_handle = RegisterEventSourceW(FFI::Pointer::NULL, wide_string(source_name)) if @eventlog_handle == NULL_HANDLE # TRANSLATORS 'Windows' is the operating system and 'RegisterEventSourceW' is a API call and should not be translated raise EventLogError.new(_("RegisterEventSourceW failed to open Windows eventlog"), FFI.errno) end end |
Class Method Details
.to_native(level) ⇒ Array
Query event identifier info for a given log level
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/puppet/util/windows/eventlog.rb', line 93 def to_native(level) case level when :debug, :info, :notice [EVENTLOG_INFORMATION_TYPE, 0x01] when :warning [EVENTLOG_WARNING_TYPE, 0x02] when :err, :alert, :emerg, :crit [EVENTLOG_ERROR_TYPE, 0x03] else raise ArgumentError, _("Invalid log level %{level}") % { level: level } end end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close this instance’s event log handle
44 45 46 47 48 |
# File 'lib/puppet/util/windows/eventlog.rb', line 44 def close DeregisterEventSource(@eventlog_handle) ensure @eventlog_handle = nil end |
#report_event(args = {}) ⇒ void
This method returns an undefined value.
Report an event to this instance’s event log handle. Accepts a string to
report (:data => <string>) and event type (:event_type => Integer) and id
(:event_id => Integer) as returned by #to_native. The additional arguments to ReportEventW seen in this method aren’t exposed - though ReportEventW technically can accept multiple strings as well as raw binary data to log, we accept a single string from Puppet::Util::Log
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/puppet/util/windows/eventlog.rb', line 60 def report_event(args = {}) unless args[:data].is_a?(String) raise ArgumentError, _("data must be a string, not %{class_name}") % { class_name: args[:data].class } end from_string_to_wide_string(args[:data]) do || FFI::MemoryPointer.new(:pointer) do || .write_pointer() user_sid = FFI::Pointer::NULL raw_data = FFI::Pointer::NULL raw_data_size = 0 num_strings = 1 eventlog_category = 0 report_result = ReportEventW(@eventlog_handle, args[:event_type], eventlog_category, args[:event_id], user_sid, num_strings, raw_data_size, , raw_data) if report_result == WIN32_FALSE # TRANSLATORS 'Windows' is the operating system and 'ReportEventW' is a API call and should not be translated raise EventLogError.new(_("ReportEventW failed to report event to Windows eventlog"), FFI.errno) end end end end |