-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): better logging system
- Loading branch information
Showing
9 changed files
with
157 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use colored::Colorize; | ||
use std::fs::{File, OpenOptions}; | ||
use std::io::Write; | ||
use std::sync::Mutex; | ||
|
||
// Open a LOG_FILE in the default log directory | ||
static mut LOG_FILE: Option<Mutex<File>> = None; | ||
|
||
pub fn init(path: String) { | ||
unsafe { | ||
LOG_FILE = Some(Mutex::new( | ||
OpenOptions::new() | ||
.create(true) | ||
.append(true) | ||
.open(path) | ||
.expect("Permission denied when opening log file"), | ||
)); | ||
} | ||
} | ||
|
||
pub fn append_logfile(message: String) { | ||
if unsafe { LOG_FILE.as_ref().is_none() } { | ||
return; | ||
} | ||
|
||
let pretty_date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); | ||
let mut file = unsafe { LOG_FILE.as_ref().unwrap().lock().unwrap() }; | ||
file | ||
.write_all(format!("{} | {}\n", pretty_date, message).as_bytes()) | ||
.unwrap(); | ||
} | ||
|
||
pub fn print_pretty(kind: String, message: String) { | ||
let pretty_date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); | ||
println!("[{}] {} {}", pretty_date, kind.bold(), message); | ||
} | ||
|
||
pub fn _print_error(message: String) { | ||
print_pretty("[ERROR]".red().to_string(), message.clone()); | ||
|
||
// Write to log file | ||
append_logfile(format!("[ERROR] {}", message)); | ||
} | ||
|
||
pub fn _print_warning(message: String) { | ||
print_pretty("[WARNING]".yellow().to_string(), message.clone()); | ||
|
||
// Write to log file | ||
append_logfile(format!("[WARNING] {}", message)); | ||
} | ||
|
||
pub fn print_info(message: String) { | ||
print_pretty("[INFO]".blue().to_string(), message.clone()); | ||
|
||
// Write to log file | ||
append_logfile(format!("[INFO] {}", message)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters