Make debug logging into an optional feature

This commit is contained in:
Michael Mikovsky
2025-11-11 11:00:28 -07:00
parent 379b6a7e25
commit 258599c9c7
12 changed files with 151 additions and 38 deletions
+3
View File
@@ -2,6 +2,9 @@
name = "unshell-lib"
edition = "2024"
[features]
log_debug = []
[dependencies]
# Base dependencies
libloading = {version = "0.8.9"}
+9 -3
View File
@@ -25,9 +25,15 @@ impl RuntimeTest {
Self {
thread_handle: thread::spawn(move || {
info!("Connecting to server...");
let mut stream = TcpStream::connect("localhost:1234").unwrap();
info!("Connectied");
debug!("Connecting to server...");
let mut stream = match TcpStream::connect("localhost:1234") {
Ok(stream) => stream,
Err(e) => {
error!("Failed to connect to server: {}", e);
return;
}
};
info!("Connected");
// let reader = BufReader::new(stream.try_clone().unwrap());
// let mut writer = BufWriter::new(stream.try_clone().unwrap());
-1
View File
@@ -1 +0,0 @@
-1
View File
@@ -1,7 +1,6 @@
#![no_main]
pub mod client;
pub mod crypt;
pub mod logger;
pub mod module;
pub mod server;
+61 -8
View File
@@ -1,3 +1,4 @@
#[cfg(feature = "log_debug")]
#[macro_export]
macro_rules! debug {
($fmt:tt) => {{
@@ -5,7 +6,12 @@ macro_rules! debug {
$crate::logger::add_record(
$crate::logger::LogLevel::Debug,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
@@ -15,13 +21,30 @@ macro_rules! debug {
$crate::logger::add_record(
$crate::logger::LogLevel::Debug,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
}};
}
#[cfg(not(feature = "log_debug"))]
#[macro_export]
macro_rules! debug {
($fmt:tt) => {{
let _ = $fmt;
}};
($fmt:tt, $($arg:expr),*) => {{
let _ = $fmt;
let _ = ($($arg),*);
}};
}
#[macro_export]
macro_rules! info {
($fmt:tt) => {{
@@ -29,7 +52,12 @@ macro_rules! info {
$crate::logger::add_record(
$crate::logger::LogLevel::Info,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
@@ -39,7 +67,12 @@ macro_rules! info {
$crate::logger::add_record(
$crate::logger::LogLevel::Info,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
@@ -53,7 +86,12 @@ macro_rules! warn {
$crate::logger::add_record(
$crate::logger::LogLevel::Warn,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
@@ -63,7 +101,12 @@ macro_rules! warn {
$crate::logger::add_record(
$crate::logger::LogLevel::Warn,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
@@ -77,7 +120,12 @@ macro_rules! error {
$crate::logger::add_record(
$crate::logger::LogLevel::Error,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
@@ -87,7 +135,12 @@ macro_rules! error {
$crate::logger::add_record(
$crate::logger::LogLevel::Error,
String::from(unshell_obfuscate::file_symbol!()),
#[cfg(feature = "log_debug")]
Some(String::from(unshell_obfuscate::file_symbol!())),
#[cfg(not(feature = "log_debug"))]
None,
std::time::SystemTime::now(),
log_result
);
+7 -2
View File
@@ -18,7 +18,7 @@ pub enum LogLevel {
#[derive(Debug)]
pub struct Record {
log_level: LogLevel,
location: String,
location: Option<String>,
// line: u32,
time: SystemTime,
message: String,
@@ -46,7 +46,12 @@ pub fn set_logger(logger: &'static dyn Logger) {
}
}
pub fn add_record(log_level: LogLevel, location: String, time: SystemTime, message: String) {
pub fn add_record(
log_level: LogLevel,
location: Option<String>,
time: SystemTime,
message: String,
) {
logger().log(Record {
log_level,
location,
+3 -1
View File
@@ -27,9 +27,11 @@ impl Logger for PrettyLogger {
let date: DateTime<Utc> = message.time.into();
let date = date.to_rfc2822().to_string();
let location = message.location.unwrap_or("".to_string());
println!(
"{OFF_WHITE}[{TIME_COLOR}{}{OFF_WHITE}] {} {WHITE}{} {GREY}{}{WHITE}",
date, log_level, message.message, message.location
date, log_level, message.message, location
);
}
}
+3 -8
View File
@@ -29,10 +29,9 @@ impl Manager {
let mut runtimes: Vec<Box<dyn ModuleRuntime>> = Vec::new();
for (name, component) in components {
for (_name, component) in components {
let module_runtime = component.start_runtime(this.clone());
if let Some(module_runtime) = module_runtime {
info!("Initialized {}", name);
runtimes.push(module_runtime);
}
}
@@ -47,12 +46,8 @@ impl Manager {
components: HashMap::new(),
};
// let mut runtimes = Vec::new();
info!("Symbol name: {}", symbol!("get_components"));
for i in 0..module_count {
info!("Importing module {}", i);
debug!("Importing module {}", i);
// let this_lock = .unwrap();
let component_func = if let Ok(component_func) = this.modules[i]
.get_symbol::<fn() -> HashMap<&'static str, Box<dyn Component>>>(
@@ -67,7 +62,7 @@ impl Manager {
let components = component_func();
let len = components.len();
info!("[{}] Loaded {} components", i, len);
debug!("[{}] Loaded {} components", i, len);
this.components.extend(components);
}