Skip to content

Commit

Permalink
fixed excessive locking in logger
Browse files Browse the repository at this point in the history
  • Loading branch information
lexa-diky committed Jul 10, 2024
1 parent c4dc1a9 commit e051305
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/compiler/compiler_source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use crate::util::logger;

pub(crate) trait CompilerSource {
fn read(&self) -> Result<String, std::io::Error>;
Expand All @@ -20,6 +21,7 @@ impl FileCompilerSource {

impl CompilerSource for FileCompilerSource {
fn read(&self) -> Result<String, std::io::Error> {
logger::debug!("Reading source file: {:?}", self.path);
let mut p = File::open(self.path.clone())?;
let mut buff = String::new();
p.read_to_string(&mut buff)?;
Expand Down
84 changes: 46 additions & 38 deletions src/util/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,60 +80,68 @@ lazy_static!(

macro_rules! debug {
($($arg:tt)*) => {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Debug {
instance.debug(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
{
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Debug {
instance.debug(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
}
}
};
}

macro_rules! error {
($($arg:tt)*) => {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Error {
instance.error(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
{
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Error {
instance.error(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
}
}
};
}

macro_rules! info {
($($arg:tt)*) => {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Info {
instance.info(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
{
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Info {
instance.info(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
}
}
};
}

macro_rules! warn {
macro_rules! warning {
($($arg:tt)*) => {
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Warn {
instance.warn(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
{
let instance = crate::util::logger::INSTANCE.lock().unwrap();
if *instance.level() <= crate::util::logger::LogLevel::Warn {
instance.warn(
format!(
"{}: {}",
module_path!(),
format!($($arg)*).as_str()
).as_str()
);
}
}
};
}
Expand All @@ -147,4 +155,4 @@ macro_rules! output {
};
}

pub(crate) use {debug, output, error};
pub(crate) use {debug, output, error, info, warning};

0 comments on commit e051305

Please sign in to comment.