Skip to content

Commit

Permalink
Optimized log module
Browse files Browse the repository at this point in the history
  • Loading branch information
BeichenY1 committed Feb 8, 2024
1 parent b20b5e9 commit 9e21e99
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
//! This file contains various logging, toml parsing functions and environment configuration
//! used by the ruxgo library
use std::sync::{Arc, RwLock};
use std::sync::{Arc, RwLock, Once};
use std::{io::Read, path::Path};
use std::fs::{self, File};
use toml::{Table, Value};
use colored::Colorize;
use std::default::Default;
use crate::builder::Target;
use std::process::{Command, Stdio};
use serde::Serialize;
use crate::builder::Target;

static INIT: Once = Once::new();
static LOG_LEVEL: RwLock<LogLevel> = RwLock::new(LogLevel::Info);

/// This enum is used to represent the different log levels
#[derive(PartialEq, PartialOrd, Debug)]
Expand All @@ -21,6 +24,23 @@ pub enum LogLevel {
Error,
}

/// Initializes the log level, which is called only once when the program starts
fn init_log_level() {
let level = std::env::var("RUXGO_LOG_LEVEL").unwrap_or_else(|_| "Info".to_string());
let log_level = match level.as_str() {
"Debug" => LogLevel::Debug,
"Info" => LogLevel::Info,
"Log" => LogLevel::Log,
"Warn" => LogLevel::Warn,
"Error" => LogLevel::Error,
_ => LogLevel::Log,
};

// Use write lock to update the log level
let mut write_lock = LOG_LEVEL.write().unwrap();
*write_lock = log_level;
}

/// This function is used to log messages to the console
/// # Arguments
/// * `level` - The log level of the message
Expand All @@ -41,32 +61,18 @@ pub enum LogLevel {
/// * `Error`
/// If the environment variable is not set, the default log level is `Log`
pub fn log(level: LogLevel, message: &str) {
INIT.call_once(|| {
init_log_level();
});
let level_str = match level {
LogLevel::Debug => "[DEBUG]".purple(),
LogLevel::Info => "[INFO]".blue(),
LogLevel::Log => "[LOG]".green(),
LogLevel::Warn => "[WARN]".yellow(),
LogLevel::Error => "[ERROR]".red(),
};
let log_level = match std::env::var("RUXGO_LOG_LEVEL") {
Ok(val) => {
if val == "Debug" {
LogLevel::Debug
} else if val == "Info" {
LogLevel::Info
} else if val == "Log" {
LogLevel::Log
} else if val == "Warn" {
LogLevel::Warn
} else if val == "Error" {
LogLevel::Error
} else {
LogLevel::Log
}
}
Err(_) => LogLevel::Info,
};
if level >= log_level {
// Use read lock to check log level
if level >= *LOG_LEVEL.read().unwrap() {
println!("{} {}", level_str, message);
}
}
Expand Down

0 comments on commit 9e21e99

Please sign in to comment.