blob: 9fd553cb075959f0b380525284f232a84b6f9bd6 (
plain) (
blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#[macro_export]
macro_rules! only_debug {
($($expr:expr);* $(;)?) => {
#[cfg(feature = "debug")]
{
$($expr;)*
}
};
}
#[macro_export]
macro_rules! trace {
($($arg:tt)*) => {
$crate::only_debug! {
log::trace!($($arg)*)
}
};
}
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {
$crate::only_debug! {
log::debug!($($arg)*)
}
};
}
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {
$crate::only_debug! {
log::info!($($arg)*)
}
};
}
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {
$crate::only_debug! {
log::warn!($($arg)*)
}
};
}
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {
$crate::only_debug! {
log::error!($($arg)*)
}
};
}
#[cfg(feature = "debug")]
pub fn init_env_logger() {
let mut log_path = std::env::current_exe()
.expect("Failed to get current executable path")
.parent()
.expect("Failed to get parent directory")
.to_path_buf();
// Search for _log.txt in parent directories
let mut current_dir = log_path.parent().unwrap().to_path_buf();
let mut found_log = false;
while current_dir.parent().is_some() {
let log_file = current_dir.join("_log.txt");
if log_file.exists() {
log_path = log_file;
found_log = true;
break;
}
current_dir = current_dir.parent().unwrap().to_path_buf();
}
// If not found, use "log.txt" in the original location
if !found_log {
log_path = std::env::current_exe()
.expect("Failed to get current executable path")
.parent()
.expect("Failed to get parent directory")
.to_path_buf();
log_path.push("log.txt");
}
// Only initialize logger if log file exists
if log_path.exists() {
use env_logger::Target;
use log::LevelFilter;
use std::fs::OpenOptions;
let log_file = OpenOptions::new()
.create(true)
.append(true)
.open(&log_path)
.expect("Failed to open log file");
env_logger::Builder::new()
.filter_level(LevelFilter::Trace)
.target(Target::Pipe(Box::new(log_file)))
.init();
}
}
|