Skip to content

Commit

Permalink
add console_bodyfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
donnie4w committed Dec 12, 2024
1 parent f869704 commit 5cf9425
Showing 1 changed file with 80 additions and 22 deletions.
102 changes: 80 additions & 22 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,35 @@ impl Logger {
}

pub fn print(&mut self, level: LEVEL, module: &str, message: &str) {
let mut console = self.fmthandle.get_console();
let mut console = String::new();
let mut msg =String::new();
let mut is_bodyfmt = false;

if let Some(f) = &self.attrfmt.bodyfmt {
is_bodyfmt = true;
msg = f(level, message.to_string());
}

if self.modmap.len() > 0 {
if let Some(mm) = self.modmap.get(module) {
let (lo, filename) = mm;
let mut is_mod_console = self.fmthandle.get_console();
let mut is_consolefmt = false;
if let Some(cs) = lo.console {
console = cs
is_mod_console = cs;
if is_mod_console {
if let Some(f) = &self.attrfmt.console_bodyfmt {
is_consolefmt = true;
console = f(level, message.to_string());
}
}
}
if filename != "" {
if *filename == self.filehandle.0 {
let _ = self.filehandle.1.print(console, message);
let _ = self.filehandle.1.print(is_mod_console,if is_mod_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
} else {
if let Some(fm) = self.fmap.get_mut(filename) {
let _ = fm.print(console, message);
let _ = fm.print(is_mod_console,if is_mod_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
}
}
return;
Expand All @@ -120,40 +135,72 @@ impl Logger {
if let Some(levels) = &self.levels {
if let Some(lp) = &levels[level as usize - 1] {
let (lo, filename) = lp;
let mut is_level_console = self.fmthandle.get_console();
let mut is_consolefmt = false;
if let Some(cs) = lo.console {
console = cs
is_level_console = cs;
if is_level_console && console.is_empty(){
if let Some(f) = &self.attrfmt.console_bodyfmt {
is_consolefmt = true;
console = f(level, message.to_string());
}
}
}
if filename != "" {
if *filename == self.filehandle.0 {
let _ = self.filehandle.1.print(console, message);
let _ = self.filehandle.1.print(is_level_console,if is_level_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
} else {
if let Some(fm) = self.fmap.get_mut(filename) {
let _ = fm.print(console, message);
let _ = fm.print(is_level_console,if is_level_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
}
}
return;
}
}
}

let _ = self.filehandle.1.print(console, message);
let is_console = self.fmthandle.get_console();
let mut is_consolefmt = false;
if is_console && console.is_empty() {
if let Some(f) = &self.attrfmt.console_bodyfmt {
is_consolefmt = true;
console = f(level, message.to_string());
}
}
let _ = self.filehandle.1.print(is_console,if is_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
}

pub fn safeprint(&mut self, level: LEVEL, module: &str, message: &str) {
let _guard = self.mutex.lock().expect("Failed to acquire lock");
let mut console = self.fmthandle.get_console();
let mut console = String::new();
let mut msg =String::new();
let mut is_bodyfmt = false;

if let Some(f) = &self.attrfmt.bodyfmt {
is_bodyfmt = true;
msg = f(level, message.to_string());
}

if self.modmap.len() > 0 {
if let Some(mm) = self.modmap.get(module) {
let (lo, filename) = mm;
let mut is_mod_console = self.fmthandle.get_console();
let mut is_consolefmt = false;
if let Some(cs) = lo.console {
console = cs
is_mod_console = cs;
if is_mod_console {
if let Some(f) = &self.attrfmt.console_bodyfmt {
is_consolefmt = true;
console = f(level, message.to_string());
}
}
}
if filename != "" {
if *filename == self.filehandle.0 {
let _ = self.filehandle.1.print(console, message);
let _ = self.filehandle.1.print(is_mod_console,if is_mod_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
} else {
if let Some(fm) = self.fmap.get_mut(filename) {
let _ = fm.print(console, message);
let _ = fm.print(is_mod_console,if is_mod_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
}
}
return;
Expand All @@ -164,23 +211,39 @@ impl Logger {
if let Some(levels) = &self.levels {
if let Some(lp) = &levels[level as usize - 1] {
let (lo, filename) = lp;
let mut is_level_console = self.fmthandle.get_console();
let mut is_consolefmt = false;
if let Some(cs) = lo.console {
console = cs
is_level_console = cs;
if is_level_console && console.is_empty(){
if let Some(f) = &self.attrfmt.console_bodyfmt {
is_consolefmt = true;
console = f(level, message.to_string());
}
}
}
if filename != "" {
if *filename == self.filehandle.0 {
let _ = self.filehandle.1.print(console, message);
let _ = self.filehandle.1.print(is_level_console,if is_level_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
} else {
if let Some(fm) = self.fmap.get_mut(filename) {
let _ = fm.print(console, message);
let _ = fm.print(is_level_console,if is_level_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
}
}
return;
}
}
}

let _ = self.filehandle.1.print(console, message);
let is_console = self.fmthandle.get_console();
let mut is_consolefmt = false;
if is_console && console.is_empty() {
if let Some(f) = &self.attrfmt.console_bodyfmt {
is_consolefmt = true;
console = f(level, message.to_string());
}
}
let _ = self.filehandle.1.print(is_console,if is_console{if is_consolefmt{console.as_str()}else{""}}else{""} , if is_bodyfmt{msg.as_str()}else{message});
}

pub fn log(&self, level: LEVEL, module: String, message: String) {
Expand Down Expand Up @@ -253,12 +316,7 @@ impl Logger {
}
}

let s = log_fmt(self.attrfmt.levelfmt.as_ref(), self.attrfmt.timefmt.as_ref(), fmat, formatter, level, filename, line, message.as_str());
if let Some(f) = &self.attrfmt.bodyfmt {
f(level, s)
} else {
s
}
log_fmt(self.attrfmt.levelfmt.as_ref(), self.attrfmt.timefmt.as_ref(), fmat, formatter, level, filename, line, message.as_str())
}

pub fn set_printmode(&mut self, mode: PRINTMODE) -> &mut Self {
Expand Down

0 comments on commit 5cf9425

Please sign in to comment.