-
Notifications
You must be signed in to change notification settings - Fork 757
/
Copy pathwriter.rs
375 lines (333 loc) · 10.9 KB
/
writer.rs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// -------------------------------------------------------------------------------------------------
// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
// https://nautechsystems.io
//
// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -------------------------------------------------------------------------------------------------
use std::{
collections::VecDeque,
fs::{File, create_dir_all},
io::{self, BufWriter, Stderr, Stdout, Write},
path::PathBuf,
sync::OnceLock,
};
use chrono::Utc;
use log::LevelFilter;
use regex::Regex;
use crate::logging::logger::LogLine;
static ANSI_RE: OnceLock<Regex> = OnceLock::new();
pub trait LogWriter {
/// Writes a log line.
fn write(&mut self, line: &str);
/// Flushes buffered logs.
fn flush(&mut self);
/// Checks if a line needs to be written to the writer or not.
fn enabled(&self, line: &LogLine) -> bool;
}
#[derive(Debug)]
pub struct StdoutWriter {
pub is_colored: bool,
io: Stdout,
level: LevelFilter,
}
impl StdoutWriter {
/// Creates a new [`StdoutWriter`] instance.
#[must_use]
pub fn new(level: LevelFilter, is_colored: bool) -> Self {
Self {
io: io::stdout(),
level,
is_colored,
}
}
}
impl LogWriter for StdoutWriter {
fn write(&mut self, line: &str) {
match self.io.write_all(line.as_bytes()) {
Ok(()) => {}
Err(e) => eprintln!("Error writing to stdout: {e:?}"),
}
}
fn flush(&mut self) {
match self.io.flush() {
Ok(()) => {}
Err(e) => eprintln!("Error flushing stdout: {e:?}"),
}
}
fn enabled(&self, line: &LogLine) -> bool {
// Prevent error logs also writing to stdout
line.level > LevelFilter::Error && line.level <= self.level
}
}
#[derive(Debug)]
pub struct StderrWriter {
pub is_colored: bool,
io: Stderr,
}
impl StderrWriter {
/// Creates a new [`StderrWriter`] instance.
#[must_use]
pub fn new(is_colored: bool) -> Self {
Self {
io: io::stderr(),
is_colored,
}
}
}
impl LogWriter for StderrWriter {
fn write(&mut self, line: &str) {
match self.io.write_all(line.as_bytes()) {
Ok(()) => {}
Err(e) => eprintln!("Error writing to stderr: {e:?}"),
}
}
fn flush(&mut self) {
match self.io.flush() {
Ok(()) => {}
Err(e) => eprintln!("Error flushing stderr: {e:?}"),
}
}
fn enabled(&self, line: &LogLine) -> bool {
line.level == LevelFilter::Error
}
}
/// File rotation config
#[derive(Debug, Clone)]
pub struct FileRotateConfig {
/// Maximum file size in bytes before rotating
pub max_file_size: u64,
/// Maximum number of backup files to keep
pub max_backup_count: u32,
/// Current file size tracking
cur_file_size: u64,
/// Queue of backup file paths (oldest first)
backup_files: VecDeque<PathBuf>,
}
impl Default for FileRotateConfig {
fn default() -> Self {
Self {
max_file_size: 10 * 1024 * 1024, // 10MB default
max_backup_count: 5,
cur_file_size: 0,
backup_files: VecDeque::new(),
}
}
}
impl From<(u64, u32)> for FileRotateConfig {
fn from(value: (u64, u32)) -> Self {
let (max_file_size, max_backup_count) = value;
Self {
max_file_size,
max_backup_count,
cur_file_size: 0,
backup_files: VecDeque::new(),
}
}
}
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.common")
)]
#[derive(Debug, Clone, Default)]
pub struct FileWriterConfig {
pub directory: Option<String>,
pub file_name: Option<String>,
pub file_format: Option<String>,
pub file_rotate: Option<FileRotateConfig>,
}
impl FileWriterConfig {
/// Creates a new [`FileWriterConfig`] instance.
#[must_use]
pub fn new(
directory: Option<String>,
file_name: Option<String>,
file_format: Option<String>,
file_rotate: Option<(u64, u32)>,
) -> Self {
let file_rotate = file_rotate.map(FileRotateConfig::from);
Self {
directory,
file_name,
file_format,
file_rotate,
}
}
}
#[derive(Debug)]
pub struct FileWriter {
pub json_format: bool,
buf: BufWriter<File>,
path: PathBuf,
file_config: FileWriterConfig,
trader_id: String,
instance_id: String,
level: LevelFilter,
}
impl FileWriter {
/// Creates a new [`FileWriter`] instance.
pub fn new(
trader_id: String,
instance_id: String,
file_config: FileWriterConfig,
fileout_level: LevelFilter,
) -> Option<Self> {
// Set up log file
let json_format = match file_config.file_format.as_ref().map(|s| s.to_lowercase()) {
Some(ref format) if format == "json" => true,
None => false,
Some(ref unrecognized) => {
tracing::error!(
"Unrecognized log file format: {unrecognized}. Using plain text format as default."
);
false
}
};
let file_path =
Self::create_log_file_path(&file_config, &trader_id, &instance_id, json_format);
match File::options()
.create(true)
.append(true)
.open(file_path.clone())
{
Ok(file) => Some(Self {
json_format,
buf: BufWriter::new(file),
path: file_path,
file_config,
trader_id,
instance_id,
level: fileout_level,
}),
Err(e) => {
tracing::error!("Error creating log file: {e}");
None
}
}
}
fn create_log_file_path(
file_config: &FileWriterConfig,
trader_id: &str,
instance_id: &str,
is_json_format: bool,
) -> PathBuf {
let basename = if let Some(file_name) = file_config.file_name.as_ref() {
if file_config.file_rotate.is_some() {
let current_date_utc = Utc::now().format("%Y-%m-%d_%H%M%S:%3f");
format!("{file_name}_{current_date_utc}")
} else {
file_name.clone()
}
} else {
// default base name
let current_date_utc = Utc::now().format("%Y-%m-%d_%H%M%S:%3f");
format!("{trader_id}_{current_date_utc}_{instance_id}")
};
let suffix = if is_json_format { "json" } else { "log" };
let mut file_path = PathBuf::new();
if let Some(directory) = file_config.directory.as_ref() {
file_path.push(directory);
create_dir_all(&file_path).expect("Failed to create directories for log file");
}
file_path.push(basename);
file_path.set_extension(suffix);
file_path
}
#[must_use]
pub fn should_rotate_file(&mut self) -> bool {
self.file_config
.file_rotate
.as_ref()
.map(|config| config.cur_file_size >= config.max_file_size)
.unwrap_or(false)
}
fn rotate_file(&mut self) {
// Flush current file
self.flush();
// Create new file
let new_path = Self::create_log_file_path(
&self.file_config,
&self.trader_id,
&self.instance_id,
self.json_format,
);
match File::options().create(true).append(true).open(&new_path) {
Ok(new_file) => {
// Rotate existing file
if let Some(rotate_config) = &mut self.file_config.file_rotate {
// Add current file to backup queue
rotate_config.backup_files.push_back(self.path.clone());
rotate_config.cur_file_size = 0;
cleanup_backups(rotate_config);
}
self.buf = BufWriter::new(new_file);
self.path = new_path;
}
Err(e) => tracing::error!("Error creating log file: {e}"),
}
tracing::info!("Rotated log file, now logging to: {}", self.path.display());
}
}
/// Clean up old backup files if we exceed the max backup count
///
/// TODO: Minor consider using a more specific version to pop a single file
/// since normal execution will not create more than 1 excess file
fn cleanup_backups(rotate_config: &mut FileRotateConfig) {
let files_to_remove = rotate_config
.backup_files
.len()
.saturating_sub(rotate_config.max_backup_count as usize);
// Remove oldest files if we exceed the max backup count
rotate_config
.backup_files
.drain(..files_to_remove)
.filter(|path| path.exists())
.for_each(|path| match std::fs::remove_file(&path) {
Ok(_) => tracing::debug!("Removed old log file: {}", path.display()),
Err(e) => tracing::error!("Failed to remove old log file {}: {}", path.display(), e),
});
}
impl LogWriter for FileWriter {
fn write(&mut self, line: &str) {
// Check if we need to rotate the file
if self.should_rotate_file() {
self.rotate_file();
}
let line = strip_ansi_codes(line);
match self.buf.write_all(line.as_bytes()) {
Ok(()) => {
// Update current file size
if let Some(rotate_config) = &mut self.file_config.file_rotate {
rotate_config.cur_file_size += line.len() as u64;
}
}
Err(e) => tracing::error!("Error writing to file: {e:?}"),
}
}
fn flush(&mut self) {
match self.buf.flush() {
Ok(()) => {}
Err(e) => tracing::error!("Error flushing file: {e:?}"),
}
}
fn enabled(&self, line: &LogLine) -> bool {
line.level <= self.level
}
}
fn strip_nonprinting_except_newline(s: &str) -> String {
s.chars()
.filter(|&c| c == '\n' || (!c.is_control() && c != '\u{7F}'))
.collect()
}
fn strip_ansi_codes(s: &str) -> String {
let re = ANSI_RE.get_or_init(|| Regex::new(r"\x1B\[[0-9;?=]*[A-Za-z]|\x1B\].*?\x07").unwrap());
let no_controls = strip_nonprinting_except_newline(s);
re.replace_all(&no_controls, "").to_string()
}