dir-odt-to-pdf/tests/test_log_timed_macro.rs
Jesús Pérex 13c65980ac refactor(logging): Improve thread safety and test configuration
This commit enhances the logging system with better thread safety and proper test configuration:

- Replace RefCell with RwLock in SimpleLogger for thread-safe logging
- Add proper feature flag configuration for test-sync
- Organize logging modules with clear separation between prod and test
- Update test files with proper feature flag annotations
- Fix module structure in lib.rs to avoid duplicate definitions

Technical changes:
- Use RwLock for thread-safe log writer access
- Add #![cfg(feature = "test-sync")] to all test files
- Configure .cargo/config.toml for test-sync feature
- Update Cargo.toml with proper test configurations
- Clean up logging module exports

This change ensures thread-safe logging in production while maintaining
separate test-specific synchronization primitives, improving overall
reliability and maintainability.
2025-05-27 09:57:16 +01:00

44 lines
1.3 KiB
Rust

#![cfg(feature = "test-sync")]
use dir_odt_to_pdf::error::{ProcessError, Result};
use dir_odt_to_pdf::log_timed;
use dir_odt_to_pdf::logging::{LogConfig, init_logging};
use log::{Level, LevelFilter, debug, info};
use std::fs;
mod common;
#[test]
fn test_log_timed_macro() -> Result<()> {
// Create a temporary file for logging
let (_temp_dir, log_path) = common::setup_test_log_file();
// Initialize logging with Debug level
let config = LogConfig {
log_file: Some(log_path.clone()),
log_level: LevelFilter::Debug,
append_log: false,
};
// Initialize logging
init_logging(config)?;
debug!("Starting test_log_timed_macro test");
// Test the macro
info!("Testing log_timed macro...");
log_timed!(Level::Info, "test operation", {
std::thread::sleep(std::time::Duration::from_millis(10));
});
// Read and verify the log file
let content = fs::read_to_string(&log_path).map_err(ProcessError::Io)?;
println!("=== Log File Contents ===");
println!("{}", content);
println!("=======================");
assert!(content.contains("test operation"));
assert!(content.contains("completed in"));
assert!(content.contains("ms"));
debug!("Test completed successfully");
Ok(())
}