7 unstable releases

new 0.3.0 Oct 25, 2024
0.2.2 Feb 18, 2023
0.1.1 Oct 31, 2021
0.1.0 Jun 25, 2020
0.0.0 Jun 25, 2020

#86 in Debugging

Download history 433/week @ 2024-07-10 519/week @ 2024-07-17 850/week @ 2024-07-24 600/week @ 2024-07-31 767/week @ 2024-08-07 820/week @ 2024-08-14 1025/week @ 2024-08-21 874/week @ 2024-08-28 832/week @ 2024-09-04 655/week @ 2024-09-11 576/week @ 2024-09-18 551/week @ 2024-09-25 463/week @ 2024-10-02 593/week @ 2024-10-09 611/week @ 2024-10-16 1141/week @ 2024-10-23

2,937 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

18KB
282 lines

eventlog

Latest version Documentation

A log-compatible wrapper around the Windows Event Log API.

Thanks to Jeroen C. van Gelderen for creating the winlog project from which this is forked.

Features

  • Writes Rust log messages to the Windows event log using the RegisterEventSourceW and ReportEventW APIs.
  • Provides utility functions to register/unregister your event source in the Windows registry.
  • Embeds a small (120-byte) message resource library containing the necessary log message templates in your executable.
  • Does not panic.

The five Rust log levels are mapped to Windows event types as follows:

Rust Log Level Windows Event Type Windows Event Id
Error Error 1
Warn Warning 2
Info Informational 3
Debug Informational 4
Trace Informational 5

Requirements

  • Rust 1.29+ on Windows with MSVC toolchain
  • [Windows, optional] mc.exe and rc.exe (only required when eventmsgs.mc is changed)
  • [Windows, optional] PowerShell (used for the end-to-end test)

Usage

Cargo.toml

[dependencies]
eventlog = "0.1.0"

Register log source with Windows

Register the log source in the Windows registry:

eventlog::register("Example Log").unwrap();

This usually requires Administrator permission so this is usually done during installation time.

If your MSI installer (or similar) registers your event sources you should not call this.

Log events

eventlog::init("Example Log", log::Level::Trace).unwrap();

info!("Hello, Event Log");
trace!("This will be logged too");

Deregister log source

Deregister the log source:

eventlog::deregister("Example Log").unwrap();

This is usually done during program uninstallation. If your MSI installer (or similar) deregisters your event sources you should not call this.

Internals

Artifacts eventmsgs.lib and eventmsgs.rs are under source control so users don't need to have mc.exe and rc.exe installed for a standard build.

  1. If build.rs determines that eventmsgs.mc was changed then build.rs:
    • invokes mc.exe (which creates eventmsgs.h)
    • invokes rc.exe (which creates eventmsgs.lib)
    • creates eventmsgs.rs from eventmsgs.h.
  2. build.rs emits linker flags so eventmsgs.lib can found.
  3. Standard cargo build follows.

Testing

The end-to-end test requires 'Full Control' permissions on the HKLM\SYSTEM\CurrentControlSet\Services\EventLog\Application registry key.

cargo test

Process:

  1. Create a unique temporary event source name (eventlog-test-###########).
  2. Register our compiled test executable as EventMessageFile for the event source in the Windows registry. You can see a new key at HKLM\SYSTEM\CurrentControlSet\Services\EventLog\Application\eventlog-test-###########.
  3. Write some log messages to the event source.
  4. Use PowerShell to retrieve the logged messages.
  5. Deregister our event source. This removes the eventlog-test-########### registry key.
  6. Assert that the retrieved log messages are correct.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~129MB
~2M SLoC