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
use super::constants::*;
use cyfs_base::*;
use log::Record;
use serde::{Serialize, Deserialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CyfsLogRecord {
level: LogLevel,
target: String,
time: u64,
file: Option<String>,
line: Option<u32>,
content: String,
}
impl CyfsLogRecord {
pub fn new(record: &Record) -> Self {
let level: LogLevel = record.metadata().level().into();
let target = record.metadata().target().to_owned();
let time = cyfs_base::bucky_time_now();
let content = format!("{}", record.args());
Self {
level,
target,
time,
file: record.file().map(|v| v.to_owned()),
line: record.line(),
content,
}
}
pub fn easy_log(level: LogLevel, content: String) -> Self {
Self {
level,
time: cyfs_base::bucky_time_now(),
target: "".to_string(),
file: None,
line: None,
content,
}
}
pub fn content(&self) -> String {
self.content.clone()
}
pub fn level(&self) -> LogLevel {
self.level
}
pub fn time(&self) -> u64 {
self.time
}
pub fn file(&self) -> String {
match &self.file {
Some(f) => f.clone(),
_ => "".to_string(),
}
}
pub fn line(&self) -> u32 {
match &self.line {
Some(l) => *l,
_ => 0,
}
}
}
use chrono::offset::Local;
use chrono::DateTime;
impl std::fmt::Display for CyfsLogRecord {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let system_time = bucky_time_to_system_time(self.time);
let datetime: DateTime<Local> = system_time.into();
let time_str = datetime.format("%Y-%m-%d %H:%M:%S%.3f %:z");
write!(
f,
"[{}] {} [{}:{}] {}",
time_str,
self.level.to_string().to_uppercase(),
self.file.as_deref().unwrap_or("<unnamed>"),
self.line.unwrap_or(0),
self.content,
)
}
}
pub trait CyfsLogTarget: Send + Sync {
fn log(&self, record: &CyfsLogRecord);
}
pub struct ConsoleCyfsLogTarget {}
impl CyfsLogTarget for ConsoleCyfsLogTarget {
fn log(&self, record: &CyfsLogRecord) {
println!(">>>{}", record);
}
}