A simple Rust log backend to send messages to the Windows event log.
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 |
eventmsgs.mc
is changed)Add to cargo.toml
:
[dependencies]
winlog = "*"
Register the log source in the Windows registry:
winlog::register("Example Log"); // silently ignores errors
// or
winlog::try_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.
Use the winlog backend:
winlog::init("Example Log").unwrap();
info!("Hello, Event Log");
Deregister the log source:
winlog::deregister("Example Log"); // silently ignores errors
// or
winlog::try_deregister("Example Log").unwrap();
This is usually done during program uninstall. If your MSI
installer (or similar) deregisters your event sources you should not call this.
sh
cargo build --release
Install MinGW (Ubuntu):
sh
sudo apt install mingw-w64
Install Rust:
sh
rustup target install x86_64-pc-windows-gnu
rustup target install i686-pc-windows-gnu
Currently the install from rustup doesn't use the correct linker so you have to add the following to .cargo/config
:
[target.x86_64-pc-windows-gnu]
linker = "/usr/bin/x86_64-w64-mingw32-gcc"
[target.i686-pc-windows-gnu]
linker = "/usr/bin/i686-w64-mingw32-gcc"
rustflags = "-C panic=abort"
Build:
sh
cargo build --release
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.
build.rs
determines that eventmsgs.mc
was changed then build.rs
:
mc.exe
(which creates eventmsgs.h
)rc.exe
(which creates eventmsgs.lib
)eventmsgs.rs
from eventmsgs.h
.build.rs
emits linker flags so eventmsgs.lib
can found.cargo build
follows.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 (winlog-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\winlog-test-###########
.
2. Write some log messages to the event source.
3. Use PowerShell to retrieve the logged messages.
4. Deregister our event source. This removes the winlog-test-###########
registry key.
5. Assert that the retrieved log messages are correct.
Licensed under either of
at your option.
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.