This is a fork of winlog.
A simple Rust log backend to send messages to the Windows event log.
- Writes Rust log messages to the Windows event log using the RegisterEventSourceW and ReportEventW APIs.
- Supports
env_loggerfiltering, initialized from RUST_LOG environment variable. (optional) - 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.
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 |
- Windows or MinGW
- [Windows, optional] PowerShell (used for the end-to-end test)
Plain winlog:
[dependencies]
log = "*"
winlog = "*"
Or to enable env_logger filtering support:
[dependencies]
log = "*"
winlog = { version = "0.2.5", features = ["env_logger"] }
Register the log source in the Windows registry:
winlog::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.
Without env_logger filtering:
use log::{info, trace};
winlog::init("Example Log").unwrap();
info!("Hello, Event Log");
trace!("This will be logged too");
Use the winlog backend with env_logger filter enabled:
use log::{info, trace};
// # export RUST_LOG="info"
winlog::init("Example Log").unwrap();
info!("Hello, Event Log");
trace!("This will be filtered out");
Deregister the log source:
winlog::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.
- Fork from original repo.
- Use
windows-sysinstead ofwinapi. - Update other dependencies.
- Generate
eventmsgs.rcand compile it withwinres. - Fix
end-to-endtest to deregister correctly even if it fails. - Remove APIs that silently fails.
- Disable unneeded regex features to speed up the build.
- Improve error reporting/handling in
build.rs.
- Gitlab CI builds on Windows 10 and Debian/MinGW.
- Optional support for env_logger event (enable feature
env_logger). - Always run
windrc/windrcon MinGW. - Include linker configuration in
.cargo/config.
cargo build --releaseInstall MinGW (Ubuntu):
sudo apt install mingw-w64Install Rust:
rustup target install x86_64-pc-windows-gnuCurrently 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"
Build:
cargo build --releaseArtifacts eventmsgs.rc and MSG00409.bin are under source control so users
don't need to have mc.exe installed for a standard build.
The end-to-end test requires 'Full Control' permissions on the
HKLM\SYSTEM\CurrentControlSet\Services\EventLog\Application
registry key.
cargo test
Process:
- Create a unique temporary event source name (
winlog-test-###########). - Register our compiled test executable as
EventMessageFilefor the event source in the Windows registry. You can see a new key atHKLM\SYSTEM\CurrentControlSet\Services\EventLog\Application\winlog-test-###########. - Write some log messages to the event source.
- Use PowerShell to retrieve the logged messages.
- Assert that the retrieved log messages are correct.
- Deregister our event source. This removes the
winlog-test-###########registry key.
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.