Skip to content

Commit c9c9617

Browse files
committed
feat: add separate log files for debug and trace
1 parent 17d1952 commit c9c9617

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

ROADMAP.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ List of ideas I want to implement in the future.
2222
- Try to update config files from `.json` to `.yaml` or `.toml`
2323
- Make sure we create all folders and files it they don't exist
2424
- Write debug output into log files
25+
- Check if there are unnecessary Ok() wrappers
2526
- New commands
2627
- Add auto update `hbox upgrade`
2728
- Add `hbox update` to update index

src/logging.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use log::{Level, LevelFilter, Log, Metadata, Record};
66
use std::error::Error;
77
use std::fs::{File, OpenOptions};
88
use std::io::Write;
9+
use std::path::MAIN_SEPARATOR;
910
use std::sync::{Arc, Mutex};
1011

1112
pub fn setup_logger() -> Result<(), Box<dyn Error>> {
@@ -60,16 +61,20 @@ impl Log for Logger {
6061
let timestamp = now.format("%Y-%m-%d %H:%M:%S%.3f");
6162

6263
let file_info = match (record.file(), record.line()) {
63-
(Some(file), Some(line)) => format!("{}:{}", file, line),
64+
(Some(file), Some(line)) => format!("{}:{}", strip_src_prefix(file).to_string(), line),
6465
_ => String::from("unknown"),
6566
};
6667

68+
// Define fixed widths
6769
let log_line = format!(
68-
"[{} {} {}] - {}",
70+
"[{:<width$} {:<level_width$} {:<file_info_width$}] {}",
6971
timestamp,
7072
record.level(),
7173
file_info,
72-
record.args()
74+
record.args(),
75+
width = 23,
76+
level_width = 5,
77+
file_info_width = 23
7378
);
7479

7580
if self.enabled {
@@ -84,7 +89,7 @@ impl Log for Logger {
8489
Level::Error => {
8590
eprintln!("{}", record.args());
8691
}
87-
_ => ()
92+
_ => (),
8893
}
8994
}
9095

@@ -93,3 +98,8 @@ impl Log for Logger {
9398
file.flush().unwrap();
9499
}
95100
}
101+
102+
fn strip_src_prefix(file_path: &str) -> &str {
103+
let prefix = format!("src{}", MAIN_SEPARATOR);
104+
file_path.strip_prefix(&prefix).unwrap_or(file_path)
105+
}

0 commit comments

Comments
 (0)