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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use crate::config::{Format, TargetPadding, TimeFormat};
use crate::{Config, LevelPadding, ThreadLogMode, ThreadPadding};
use chrono::DateTime;
use log::Record;
use std::any::Any;
use std::io::{Error, Write};
use std::str::FromStr;
use std::thread;
use termcolor2::{BufferedStandardStream, Color, ColorSpec, WriteColor};
/// Attempts to log a message based on the provided configuration.
/// Writes the log message to the provided writer if it should not be skipped.
#[inline(always)]
pub fn try_log<W>(config: &Config, record: &Record<'_>, write: &mut W) -> Result<(), Error>
where
W: Write + Sized + Any,
{
if should_skip(config, record) {
return Ok(());
}
if record.level() > config.min_level || record.level() < config.max_level {
return Ok(());
}
let mut level = String::new();
let mut time = String::new();
let mut thread = String::new();
let mut target = String::new();
let mut location = String::new();
let mut module = String::new();
if config.format & Format::Time != 0 {
time = write_time(config)?;
}
if config.format & Format::LevelFlag != 0 {
level = write_level(record, config)?;
}
if config.format & Format::Thread != 0 {
thread = match config.thread_log_mode {
ThreadLogMode::IDs => write_thread_id(config)?,
ThreadLogMode::Names | ThreadLogMode::Both => write_thread_name(config)?,
}
}
if config.format & Format::Target != 0 {
target = write_target(record, config)?;
}
if config.format & Format::FileLocation != 0 {
location = write_location(record)?;
}
if config.format & Format::Module != 0 {
module = write_module(record)?;
}
let args = write_args(record, &config.line_ending)?;
if config.formatter.is_some() {
parse_and_format_log(
write, config, &level, &time, &thread, &target, &location, &module, &args,
)?;
} else {
if !time.is_empty() {
write!(write, "{}", time)?;
}
if !level.is_empty() {
write!(write, " [{}]", level)?;
}
if !thread.is_empty() {
write!(write, " ({})", thread)?;
}
if !target.is_empty() {
write!(write, " {}:", target)?;
}
write!(write, " {}", args)?;
if !location.is_empty() {
write!(write, " [{}]", location)?;
}
writeln!(write)?;
}
Ok(())
}
/// Writes the current time based on the configured format.
#[inline(always)]
pub fn write_time(config: &Config) -> Result<String, Error> {
use chrono::Local;
let dt: DateTime<Local> = Local::now();
let formatted_time = match config.time_format.clone() {
TimeFormat::Rfc2822 => dt.to_rfc2822(),
TimeFormat::Rfc3339 => dt.to_rfc3339(),
TimeFormat::Custom(format) => dt.format(format).to_string(),
};
Ok(formatted_time)
}
/// Writes the log level to a string based on the configured padding.
#[inline(always)]
pub fn write_level(record: &Record<'_>, config: &Config) -> Result<String, Error> {
let level = match config.level_padding {
LevelPadding::Left => format!("{: >5}", record.level()),
LevelPadding::Right => format!("{: <5}", record.level()),
LevelPadding::Off => record.level().to_string(),
};
let formatted_level = level.to_string();
Ok(formatted_level)
}
/// Writes the target (module) of the log record based on the configured padding.
#[inline(always)]
pub fn write_target(record: &Record<'_>, config: &Config) -> Result<String, Error> {
let target = match config.target_padding {
TargetPadding::Left(pad) => format!("{:>pad$}", record.target(), pad = pad),
TargetPadding::Right(pad) => format!("{:<pad$}", record.target(), pad = pad),
TargetPadding::Off => record.target().to_string(),
};
Ok(target)
}
/// Writes the file and line number of the log record's source location.
#[inline(always)]
pub fn write_location(record: &Record<'_>) -> Result<String, Error> {
let file = record.file().unwrap_or("<unknown>").replace("\\", "/");
let location = if let Some(line) = record.line() {
format!("{}:{}", file, line)
} else {
format!("{}:<unknown>", file)
};
Ok(location)
}
/// Writes the module path of the log record.
#[inline(always)]
pub fn write_module(record: &Record<'_>) -> Result<String, Error> {
let module = record.module_path().unwrap_or("<unknown>");
Ok(module.to_string())
}
/// Writes the current thread's name based on the configuration.
pub fn write_thread_name(config: &Config) -> Result<String, Error> {
if let Some(name) = thread::current().name() {
let thread_name = match config.thread_padding {
ThreadPadding::Left { 0: qty } => {
format!("{:>width$}", name, width = qty)
}
ThreadPadding::Right { 0: qty } => {
format!("{:<width$}", name, width = qty)
}
ThreadPadding::Off => name.to_string(),
};
Ok(thread_name)
} else if config.thread_log_mode == ThreadLogMode::Both {
write_thread_id(config)
} else {
Ok(String::new())
}
}
/// Writes the current thread's ID based on the configuration.
pub fn write_thread_id(config: &Config) -> Result<String, Error> {
let id = format!("{:?}", thread::current().id())
.replace("ThreadId(", "")
.replace(")", "");
let thread_id = match config.thread_padding {
ThreadPadding::Left { 0: qty } => {
format!("{:>width$}", id, width = qty)
}
ThreadPadding::Right { 0: qty } => {
format!("{:<width$}", id, width = qty)
}
ThreadPadding::Off => id.to_string(),
};
Ok(thread_id)
}
/// Writes the arguments of the log record, appending a line ending.
#[inline(always)]
pub fn write_args(record: &Record<'_>, line_ending: &str) -> Result<String, Error> {
Ok(format!("{}{}", record.args(), line_ending))
}
/// Determines whether the log record should be skipped based on the configuration's filters.
#[inline(always)]
pub fn should_skip(config: &Config, record: &Record<'_>) -> bool {
// If a module path and allowed list are available
match (record.target(), &*config.filter_allow) {
(path, allowed) if !allowed.is_empty() => {
// Check that the module path matches at least one allow filter
if !allowed.iter().any(|v| path.starts_with(&**v)) {
// If not, skip any further writing
return true;
}
}
_ => {}
}
// If a module path and ignore list are available
match (record.target(), &*config.filter_ignore) {
(path, ignore) if !ignore.is_empty() => {
// Check that the module path does not match any ignore filters
if ignore.iter().any(|v| path.starts_with(&**v)) {
// If not, skip any further writing
return true;
}
}
_ => {}
}
false
}
#[inline]
fn apply_style(style: &str) -> Option<(Color, bool)> {
let is_bg = style.starts_with("bg");
let new_style = match is_bg {
true => &style[2..],
false => style,
};
if let Ok(color) = Color::from_str(new_style) {
return Some((color, !is_bg));
}
None
}
#[inline]
#[allow(clippy::too_many_arguments)]
pub fn parse_and_format_log_term(
writer: &mut BufferedStandardStream,
level_color: Option<Color>,
config: &Config,
level: &str,
time: &str,
thread: &str,
target: &str,
file: &str,
module: &str,
message: &str,
) -> Result<(), Error> {
parse_and_format_log_internal(
writer,
level_color,
config,
level,
time,
thread,
target,
file,
module,
message,
true,
)
}
#[inline]
#[allow(clippy::too_many_arguments)]
pub fn parse_and_format_log<W>(
writer: &mut W,
config: &Config,
level: &str,
time: &str,
thread: &str,
target: &str,
file: &str,
module: &str,
message: &str,
) -> Result<(), Error>
where
W: Write + Sized + Any,
{
parse_and_format_log_internal(
writer, None, config, level, time, thread, target, file, module, message, false,
)
}
#[allow(clippy::too_many_arguments)]
fn parse_and_format_log_internal<W>(
writer: &mut W,
level_color: Option<Color>,
config: &Config,
level: &str,
time: &str,
thread: &str,
target: &str,
file: &str,
module: &str,
message: &str,
is_terminal: bool,
) -> Result<(), Error>
where
W: Write + Sized + Any,
{
let format_str = config.formatter.clone().unwrap();
let mut last_end = 0; // Tracks the position of the last match's end
let mut chars = format_str.chars().enumerate().peekable(); // To look ahead for brackets
while let Some((i, c)) = chars.next() {
if c == '[' {
// Check for double brackets `[[`
if let Some((_, next_c)) = chars.peek() {
if *next_c == '[' {
chars.next(); // Consume the second `[`
// Find the closing double brackets `]]`
if let Some(end) = format_str[i + 2..].find("]]") {
let end = i + 2 + end;
// Write the part before the placeholder
if last_end < i {
write!(writer, "{}", &format_str[last_end..i])?;
}
// Include the brackets in the output by simply writing them
write!(writer, "[")?;
let placeholder = &format_str[i + 2..end];
process_placeholder(
writer,
placeholder,
level_color.clone(),
config,
level,
time,
thread,
target,
file,
module,
message,
is_terminal,
)?;
write!(writer, "]")?;
last_end = end + 2; // Update last_end to the character after `]]`
continue;
}
}
}
// Handle single brackets `[`
if let Some(end) = format_str[i + 1..].find(']') {
let end = i + 1 + end;
// Write the part before the placeholder
if last_end < i {
write!(writer, "{}", &format_str[last_end..i])?;
}
let placeholder = &format_str[i + 1..end];
process_placeholder(
writer,
placeholder,
level_color.clone(),
config,
level,
time,
thread,
target,
file,
module,
message,
is_terminal,
)?;
last_end = end + 1; // Update last_end to the character after `]`
}
}
}
// Write any remaining part of the format_str after the last match
if last_end < format_str.len() {
write!(writer, "{}", &format_str[last_end..])?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
fn process_placeholder<W>(
writer: &mut W,
placeholder: &str,
level_color: Option<Color>,
config: &Config,
level: &str,
time: &str,
thread: &str,
target: &str,
file: &str,
module: &str,
message: &str,
is_terminal: bool,
) -> Result<(), Error>
where
W: Write + Sized + Any,
{
let parts: Vec<&str> = placeholder.split(':').collect();
let key = parts[0];
let mut use_bracket_level = true;
if is_terminal {
let styles = if parts.len() > 1 {
parts[1..].to_vec()
} else {
vec![]
};
let mut fg_color = None;
let mut bg_color = None;
let mut bold = false;
let mut italic = false;
let mut dim = false;
let mut underline = false;
let mut strikethrough = false;
for style in styles {
match style.to_ascii_lowercase().as_str() {
"bold" => bold = true,
"italic" => italic = true,
"dim" => dim = true,
"underline" => underline = true,
"strikethrough" => strikethrough = true,
"nb" | "nobrackets" | "no_brackets" => {
if key == "level" {
use_bracket_level = false;
}
}
_ => {
if let Some((color, is_fg)) = apply_style(style) {
if is_fg {
fg_color = fg_color.or(Some(color));
} else {
bg_color = bg_color.or(Some(color));
}
}
}
}
}
if key == "level" {
fg_color = fg_color.or(level_color.clone());
}
if config.enable_colors {
if let Some(writer) = (writer as &mut dyn Any).downcast_mut::<BufferedStandardStream>()
{
writer.set_color(
ColorSpec::new()
.set_fg(fg_color)
.set_bg(bg_color)
.set_bold(bold)
.set_italic(italic)
.set_dimmed(dim)
.set_underline(underline)
.set_strikethrough(strikethrough),
)?;
}
}
}
match key {
"time" => write!(writer, "{}", time)?,
"thread" => write!(writer, "{}", thread)?,
"target" => write!(writer, "{}", target)?,
"level" => {
if use_bracket_level {
write!(writer, "[{}]", level)?
} else {
write!(writer, "{}", level)?
}
}
"file" => write!(writer, "{}", file)?,
"module" => write!(writer, "{}", module)?,
"message" => write!(writer, "{}", message)?,
_ => write!(writer, "{}", placeholder)?,
}
if is_terminal && config.enable_colors {
if let Some(writer) = (writer as &mut dyn Any).downcast_mut::<BufferedStandardStream>() {
writer.reset()?;
}
}
Ok(())
}
// #[allow(clippy::too_many_arguments)]
// fn parse_and_format_log_internal<W>(
// writer: &mut W,
// level_color: Option<Color>,
// config: &Config,
// level: &str,
// time: &str,
// thread: &str,
// target: &str,
// file: &str,
// module: &str,
// message: &str,
// is_terminal: bool,
// ) -> Result<(), Error>
// where
// W: Write + Sized + Any,
// {
// let format_str = config.formatter.clone().unwrap();
// let mut last_end = 0;
//
// for (i, c) in format_str.char_indices() {
// if c == '[' {
// let mut closing_bracket = ']';
// let mut start_idx = i + 1;
//
// // Detect double-brackets for literal brackets
// if format_str[start_idx..].starts_with('[') {
// closing_bracket = ']'; // Double brackets use a single closing bracket
// start_idx += 1; // Adjust start index
// }
//
// // Find the closing bracket
// if let Some(end_idx) = format_str[start_idx..].find(closing_bracket) {
// let end_idx = start_idx + end_idx;
//
// // Write the part before the placeholder
// if last_end < i {
// write!(writer, "{}", &format_str[last_end..i])?;
// }
//
// // Extract the placeholder content
// let placeholder = &format_str[start_idx..end_idx];
// let parts: Vec<&str> = placeholder.split(':').collect();
// let key = parts[0];
//
// // Extract styles (if any)
// let style = parts.get(1).cloned();
//
// // Apply styles if terminal supports it
// if is_terminal && config.enable_colors {
// if let Some(writer) = (writer as &mut dyn Any).downcast_mut::<BufferedStandardStream>()
// {
// apply_style(writer, style)?;
// }
// }
//
// // Write the resolved placeholder value
// let value = match key {
// "time" => time,
// "thread" => thread,
// "target" => target,
// "level" => level,
// "file" => file,
// "message" => message,
// _ => key, // Unknown placeholders are treated as literal keys
// };
//
// if closing_bracket == ']' && placeholder.starts_with('[') {
// // Double brackets -> wrap output in brackets
// write!(writer, "[{}]", value)?;
// } else {
// // Single brackets -> raw output
// write!(writer, "{}", value)?;
// }
//
// if is_terminal && config.enable_colors {
// if let Some(writer) = (writer as &mut dyn Any).downcast_mut::<BufferedStandardStream>()
// {
// writer.reset()?; // Reset styles
// }
// }
//
// last_end = end_idx + 1; // Update last_end
// }
// }
// }
//
// // Write remaining text after the last placeholder
// if last_end < format_str.len() {
// write!(writer, "{}", &format_str[last_end..])?;
// }
//
//
// Ok(())
// }