Skip to content

Commit 192614c

Browse files
author
janekdererste
committed
Make log files optional too. Otherwise we are creating a lot of files when running with many cores
1 parent 816c532 commit 192614c

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/simulation/logging.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ pub fn init_std_out_logging() {
2121
tracing::subscriber::set_global_default(collector).expect("Unable to set a global collector");
2222
}
2323

24-
pub fn init_logging(config: &Config, part: u32) -> (WorkerGuard, Option<WriterGuard>) {
24+
pub fn init_logging(config: &Config, part: u32) -> (Option<WorkerGuard>, Option<WriterGuard>) {
2525
let file_discriminant = part.to_string();
2626
let dir = PathBuf::from(&config.output().output_dir);
27-
let log_file_name = format!("log_process_{file_discriminant}.txt");
28-
let log_file_appender = rolling::never(&dir, log_file_name);
29-
let (log_file, _guard_log) = non_blocking(log_file_appender);
3027

3128
let (csv_layer, guard) = if let Profiling::CSV(level) = config.output().profiling {
3229
let duration_dir = dir.join("instrument");
@@ -38,16 +35,23 @@ pub fn init_logging(config: &Config, part: u32) -> (WorkerGuard, Option<WriterGu
3835
} else {
3936
(None, None)
4037
};
38+
let (log_layer, log_guard) = if Logging::Info == config.output().logging {
39+
let log_file_name = format!("log_process_{file_discriminant}.txt");
40+
let log_file_appender = rolling::never(&dir, log_file_name);
41+
let (log_file, log_guard) = non_blocking(log_file_appender);
42+
let layer = fmt::Layer::new()
43+
.with_writer(log_file)
44+
.json()
45+
.with_ansi(false)
46+
.with_filter(LevelFilter::INFO);
47+
(Some(layer), Some(log_guard))
48+
} else {
49+
(None, None)
50+
};
4151

4252
let collector = tracing_subscriber::registry()
4353
.with(csv_layer)
44-
.with((config.output().logging == Logging::Info).then(|| {
45-
fmt::Layer::new()
46-
.with_writer(log_file)
47-
.json()
48-
.with_ansi(false)
49-
.with_filter(LevelFilter::INFO)
50-
}))
54+
.with(log_layer)
5155
// process 0 should log to console as well
5256
.with((part == 0).then(|| {
5357
fmt::layer()
@@ -57,5 +61,5 @@ pub fn init_logging(config: &Config, part: u32) -> (WorkerGuard, Option<WriterGu
5761
}));
5862

5963
tracing::subscriber::set_global_default(collector).expect("Unable to set a global collector");
60-
(_guard_log, guard)
64+
(log_guard, guard)
6165
}

0 commit comments

Comments
 (0)