1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#[cfg(test)]
mod tests {
use pywatt_sdk::logging::init_module;
use std::env;
use std::sync::Once;
use tracing::{debug, error, info, trace, warn};
// We can only call init_module once, so we use this to ensure it's only done once
static INIT: Once = Once::new();
// Helper function to initialize the logger once
fn init_logger_once() {
INIT.call_once(|| {
// Store the original value
let original = env::var("RUST_LOG").ok();
// Set a test value
unsafe {
env::set_var("RUST_LOG", "debug");
}
// Initialize
init_module();
// Restore the original value or remove if it wasn't set
match original {
Some(val) => unsafe { env::set_var("RUST_LOG", val) },
None => unsafe { env::remove_var("RUST_LOG") },
}
});
}
// Test that we can initialize the logger without panicking
#[test]
fn test_init_module() {
init_logger_once();
// Since this is mostly testing that the call doesn't panic,
// and we can't easily inspect the logger's configuration,
// we'll just assert that the function returns without error
}
// Test logging at various levels
#[test]
fn test_logging_macros() {
init_logger_once();
// Test the macro with various log levels
// These should not panic
trace!("Test trace log");
debug!("Test debug log");
info!("Test info log");
warn!("Test warn log");
error!("Test error log");
// Test structured logging
info!(value = "test", "Structured log");
// Test with a value that would be redacted if registered
let secret_value = "password123";
info!(secret = secret_value, "Log with potentially secret value");
// We can't easily verify the output, but we can at least ensure
// that the macro compiles and executes without panicking
}
}