1use std::{
17 collections::VecDeque,
18 fs::{File, create_dir_all},
19 io::{self, BufWriter, Stderr, Stdout, Write},
20 path::PathBuf,
21 sync::OnceLock,
22};
23
24use chrono::{NaiveDate, Utc};
25use log::LevelFilter;
26use regex::Regex;
27
28use crate::logging::logger::LogLine;
29
30static ANSI_RE: OnceLock<Regex> = OnceLock::new();
31
32pub trait LogWriter {
33 fn write(&mut self, line: &str);
35 fn flush(&mut self);
37 fn enabled(&self, line: &LogLine) -> bool;
39}
40
41#[derive(Debug)]
42pub struct StdoutWriter {
43 pub is_colored: bool,
44 io: Stdout,
45 level: LevelFilter,
46}
47
48impl StdoutWriter {
49 #[must_use]
51 pub fn new(level: LevelFilter, is_colored: bool) -> Self {
52 Self {
53 io: io::stdout(),
54 level,
55 is_colored,
56 }
57 }
58}
59
60impl LogWriter for StdoutWriter {
61 fn write(&mut self, line: &str) {
62 match self.io.write_all(line.as_bytes()) {
63 Ok(()) => {}
64 Err(e) => eprintln!("Error writing to stdout: {e:?}"),
65 }
66 }
67
68 fn flush(&mut self) {
69 match self.io.flush() {
70 Ok(()) => {}
71 Err(e) => eprintln!("Error flushing stdout: {e:?}"),
72 }
73 }
74
75 fn enabled(&self, line: &LogLine) -> bool {
76 line.level > LevelFilter::Error && line.level <= self.level
78 }
79}
80
81#[derive(Debug)]
82pub struct StderrWriter {
83 pub is_colored: bool,
84 io: Stderr,
85}
86
87impl StderrWriter {
88 #[must_use]
90 pub fn new(is_colored: bool) -> Self {
91 Self {
92 io: io::stderr(),
93 is_colored,
94 }
95 }
96}
97
98impl LogWriter for StderrWriter {
99 fn write(&mut self, line: &str) {
100 match self.io.write_all(line.as_bytes()) {
101 Ok(()) => {}
102 Err(e) => eprintln!("Error writing to stderr: {e:?}"),
103 }
104 }
105
106 fn flush(&mut self) {
107 match self.io.flush() {
108 Ok(()) => {}
109 Err(e) => eprintln!("Error flushing stderr: {e:?}"),
110 }
111 }
112
113 fn enabled(&self, line: &LogLine) -> bool {
114 line.level == LevelFilter::Error
115 }
116}
117
118#[derive(Debug, Clone)]
120pub struct FileRotateConfig {
121 pub max_file_size: u64,
123 pub max_backup_count: u32,
125 cur_file_size: u64,
127 cur_file_creation_date: NaiveDate,
129 backup_files: VecDeque<PathBuf>,
131}
132
133impl Default for FileRotateConfig {
134 fn default() -> Self {
135 Self {
136 max_file_size: 100 * 1024 * 1024, max_backup_count: 5,
138 cur_file_size: 0,
139 cur_file_creation_date: Utc::now().date_naive(),
140 backup_files: VecDeque::new(),
141 }
142 }
143}
144
145impl From<(u64, u32)> for FileRotateConfig {
146 fn from(value: (u64, u32)) -> Self {
147 let (max_file_size, max_backup_count) = value;
148 Self {
149 max_file_size,
150 max_backup_count,
151 cur_file_size: 0,
152 cur_file_creation_date: Utc::now().date_naive(),
153 backup_files: VecDeque::new(),
154 }
155 }
156}
157
158#[cfg_attr(
159 feature = "python",
160 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.common")
161)]
162#[derive(Debug, Clone, Default)]
163pub struct FileWriterConfig {
164 pub directory: Option<String>,
165 pub file_name: Option<String>,
166 pub file_format: Option<String>,
167 pub file_rotate: Option<FileRotateConfig>,
168}
169
170impl FileWriterConfig {
171 #[must_use]
173 pub fn new(
174 directory: Option<String>,
175 file_name: Option<String>,
176 file_format: Option<String>,
177 file_rotate: Option<(u64, u32)>,
178 ) -> Self {
179 let file_rotate = file_rotate.map(FileRotateConfig::from);
180 Self {
181 directory,
182 file_name,
183 file_format,
184 file_rotate,
185 }
186 }
187}
188
189#[derive(Debug)]
190pub struct FileWriter {
191 pub json_format: bool,
192 buf: BufWriter<File>,
193 path: PathBuf,
194 file_config: FileWriterConfig,
195 trader_id: String,
196 instance_id: String,
197 level: LevelFilter,
198 cur_file_date: NaiveDate,
199}
200
201impl FileWriter {
202 pub fn new(
204 trader_id: String,
205 instance_id: String,
206 file_config: FileWriterConfig,
207 fileout_level: LevelFilter,
208 ) -> Option<Self> {
209 let json_format = match file_config.file_format.as_ref().map(|s| s.to_lowercase()) {
211 Some(ref format) if format == "json" => true,
212 None => false,
213 Some(ref unrecognized) => {
214 tracing::error!(
215 "Unrecognized log file format: {unrecognized}. Using plain text format as default."
216 );
217 false
218 }
219 };
220
221 let file_path =
222 Self::create_log_file_path(&file_config, &trader_id, &instance_id, json_format);
223
224 match File::options()
225 .create(true)
226 .append(true)
227 .open(file_path.clone())
228 {
229 Ok(file) => {
230 let mut file_config = file_config;
232 if let Some(ref mut rotate_config) = file_config.file_rotate
233 && let Ok(metadata) = file.metadata()
234 {
235 rotate_config.cur_file_size = metadata.len();
236 }
237
238 Some(Self {
239 json_format,
240 buf: BufWriter::new(file),
241 path: file_path,
242 file_config,
243 trader_id,
244 instance_id,
245 level: fileout_level,
246 cur_file_date: Utc::now().date_naive(),
247 })
248 }
249 Err(e) => {
250 tracing::error!("Error creating log file: {e}");
251 None
252 }
253 }
254 }
255
256 fn create_log_file_path(
257 file_config: &FileWriterConfig,
258 trader_id: &str,
259 instance_id: &str,
260 is_json_format: bool,
261 ) -> PathBuf {
262 let utc_now = Utc::now();
263
264 let basename = match file_config.file_name.as_ref() {
265 Some(file_name) => {
266 if file_config.file_rotate.is_some() {
267 let utc_datetime = utc_now.format("%Y-%m-%d_%H%M%S:%3f");
268 format!("{file_name}_{utc_datetime}")
269 } else {
270 file_name.clone()
271 }
272 }
273 None => {
274 let utc_component = if file_config.file_rotate.is_some() {
276 utc_now.format("%Y-%m-%d_%H%M%S:%3f")
277 } else {
278 utc_now.format("%Y-%m-%d")
279 };
280
281 format!("{trader_id}_{utc_component}_{instance_id}")
282 }
283 };
284
285 let suffix = if is_json_format { "json" } else { "log" };
286 let mut file_path = PathBuf::new();
287
288 if let Some(directory) = file_config.directory.as_ref() {
289 file_path.push(directory);
290 create_dir_all(&file_path).expect("Failed to create directories for log file");
291 }
292
293 file_path.push(basename);
294 file_path.set_extension(suffix);
295 file_path
296 }
297
298 #[must_use]
299 fn should_rotate_file(&self, next_line_size: u64) -> bool {
300 if let Some(ref rotate_config) = self.file_config.file_rotate {
302 rotate_config.cur_file_size + next_line_size > rotate_config.max_file_size
303 } else if self.file_config.file_name.is_none() {
305 let today = Utc::now().date_naive();
306 self.cur_file_date != today
307 } else {
309 false
310 }
311 }
312
313 fn rotate_file(&mut self) {
314 self.flush();
316
317 let new_path = Self::create_log_file_path(
319 &self.file_config,
320 &self.trader_id,
321 &self.instance_id,
322 self.json_format,
323 );
324 match File::options().create(true).append(true).open(&new_path) {
325 Ok(new_file) => {
326 if let Some(rotate_config) = &mut self.file_config.file_rotate {
328 rotate_config.backup_files.push_back(self.path.clone());
330 rotate_config.cur_file_size = 0;
331 rotate_config.cur_file_creation_date = Utc::now().date_naive();
332 cleanup_backups(rotate_config);
333 } else {
334 self.cur_file_date = Utc::now().date_naive();
336 }
337
338 self.buf = BufWriter::new(new_file);
339 self.path = new_path;
340 }
341 Err(e) => tracing::error!("Error creating log file: {e}"),
342 }
343
344 tracing::info!("Rotated log file, now logging to: {}", self.path.display());
345 }
346}
347
348fn cleanup_backups(rotate_config: &mut FileRotateConfig) {
353 let excess = rotate_config
355 .backup_files
356 .len()
357 .saturating_sub(rotate_config.max_backup_count as usize);
358 for _ in 0..excess {
359 if let Some(path) = rotate_config.backup_files.pop_front() {
360 if path.exists() {
361 match std::fs::remove_file(&path) {
362 Ok(_) => tracing::debug!("Removed old log file: {}", path.display()),
363 Err(e) => {
364 tracing::error!("Failed to remove old log file {}: {e}", path.display());
365 }
366 }
367 }
368 } else {
369 break;
370 }
371 }
372}
373
374impl LogWriter for FileWriter {
375 fn write(&mut self, line: &str) {
376 let line = strip_ansi_codes(line);
377 let line_size = line.len() as u64;
378
379 if self.should_rotate_file(line_size) {
381 self.rotate_file();
382 }
383
384 match self.buf.write_all(line.as_bytes()) {
385 Ok(()) => {
386 if let Some(rotate_config) = &mut self.file_config.file_rotate {
388 rotate_config.cur_file_size += line_size;
389 }
390 }
391 Err(e) => tracing::error!("Error writing to file: {e:?}"),
392 }
393 }
394
395 fn flush(&mut self) {
396 match self.buf.flush() {
397 Ok(()) => {}
398 Err(e) => tracing::error!("Error flushing file: {e:?}"),
399 }
400
401 match self.buf.get_ref().sync_all() {
402 Ok(()) => {}
403 Err(e) => tracing::error!("Error syncing file: {e:?}"),
404 }
405 }
406
407 fn enabled(&self, line: &LogLine) -> bool {
408 line.level <= self.level
409 }
410}
411
412fn strip_nonprinting_except_newline(s: &str) -> String {
413 s.chars()
414 .filter(|&c| c == '\n' || (!c.is_control() && c != '\u{7F}'))
415 .collect()
416}
417
418fn strip_ansi_codes(s: &str) -> String {
419 let re = ANSI_RE.get_or_init(|| Regex::new(r"\x1B\[[0-9;?=]*[A-Za-z]|\x1B\].*?\x07").unwrap());
420 let no_controls = strip_nonprinting_except_newline(s);
421 re.replace_all(&no_controls, "").to_string()
422}
423
424#[cfg(test)]
429mod tests {
430 use log::LevelFilter;
431 use rstest::rstest;
432 use tempfile::tempdir;
433
434 use super::*;
435
436 #[rstest]
437 fn test_file_writer_with_rotation_creates_new_timestamped_file() {
438 let temp_dir = tempdir().unwrap();
439
440 let config = FileWriterConfig {
441 directory: Some(temp_dir.path().to_str().unwrap().to_string()),
442 file_name: Some("test".to_string()),
443 file_format: None,
444 file_rotate: Some(FileRotateConfig::from((2000, 5))),
445 };
446
447 let writer = FileWriter::new(
448 "TRADER-001".to_string(),
449 "instance-123".to_string(),
450 config,
451 LevelFilter::Info,
452 )
453 .unwrap();
454
455 assert_eq!(
456 writer
457 .file_config
458 .file_rotate
459 .as_ref()
460 .unwrap()
461 .cur_file_size,
462 0
463 );
464 assert!(writer.path.to_str().unwrap().contains("test_"));
465 }
466
467 #[rstest]
468 #[case("Hello, World!", "Hello, World!")]
469 #[case("Line1\nLine2", "Line1\nLine2")]
470 #[case("Tab\there", "Tabhere")]
471 #[case("Null\0char", "Nullchar")]
472 #[case("DEL\u{7F}char", "DELchar")]
473 #[case("Bell\u{07}sound", "Bellsound")]
474 #[case("Mix\t\0\u{7F}ed", "Mixed")]
475 fn test_strip_nonprinting_except_newline(#[case] input: &str, #[case] expected: &str) {
476 let result = strip_nonprinting_except_newline(input);
477 assert_eq!(result, expected);
478 }
479
480 #[rstest]
481 #[case("Plain text", "Plain text")]
482 #[case("\x1B[31mRed\x1B[0m", "[31mRed[0m")]
483 #[case("\x1B[1;32mBold Green\x1B[0m", "[1;32mBold Green[0m")]
484 #[case("Before\x1B[0mAfter", "Before[0mAfter")]
485 #[case("\x1B]0;Title\x07Content", "]0;TitleContent")]
486 #[case("Text\t\x1B[31mRed\x1B[0m", "Text[31mRed[0m")]
487 fn test_strip_ansi_codes(#[case] input: &str, #[case] expected: &str) {
488 let result = strip_ansi_codes(input);
489 assert_eq!(result, expected);
490 }
491}