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
+59 -7
View File
@@ -13,6 +13,15 @@ dependencies = [
"cpufeatures",
]
[[package]]
name = "aho-corasick"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
dependencies = [
"memchr",
]
[[package]]
name = "android_system_properties"
version = "0.1.5"
@@ -279,6 +288,12 @@ version = "0.4.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432"
[[package]]
name = "memchr"
version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -318,6 +333,35 @@ version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "regex"
version = "1.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
[[package]]
name = "rustversion"
version = "1.0.22"
@@ -401,6 +445,20 @@ dependencies = [
"unshell-obfuscate",
]
[[package]]
name = "unshell-crypt"
version = "0.1.0"
dependencies = [
"aes",
"block-padding 0.4.1",
"cbc",
"getrandom",
"hex",
"hex-literal",
"regex",
"sha2",
]
[[package]]
name = "unshell-lib"
version = "0.0.0"
@@ -422,16 +480,10 @@ dependencies = [
name = "unshell-obfuscate"
version = "0.0.0"
dependencies = [
"aes",
"block-padding 0.4.1",
"cbc",
"getrandom",
"hex",
"hex-literal",
"proc-macro2",
"quote",
"sha2",
"syn",
"unshell-crypt",
]
[[package]]
+1 -3
View File
@@ -11,8 +11,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut input = String::new();
stdin().read_line(&mut input).expect("Failed to read line");
// println!("{}", input);
let args = input.trim().split(" ").collect::<Vec<&str>>();
match args[0] {
@@ -32,6 +30,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
println!("{:?}", args);
// println!("{:?}", args);
}
}
+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);
}
+1
View File
@@ -6,6 +6,7 @@ edition = "2024"
[features]
obfuscate = ["unshell-obfuscate/obfuscate"]
log_debug = ["unshell-lib/log_debug"]
[dependencies]
env_logger = "0.11.8"
+4 -4
View File
@@ -7,17 +7,17 @@ use unshell_lib::{
extern crate unshell_lib;
fn main() {
// #[cfg(not(feature = "obfuscate"))]
#[cfg(not(feature = "obfuscate"))]
unshell_lib::logger::PrettyLogger::init();
info!("Initialized");
debug!("Initialized");
match || -> Result<(), ModuleError> {
let args = std::env::args();
let mut modules = Vec::new();
for arg in args.skip(1) {
info!("Loading module: {}", arg);
debug!("Loading module: {}", arg);
modules.push(Module::new(&arg)?)
}
Manager::run(modules);
@@ -26,7 +26,7 @@ fn main() {
}() {
Ok(_) => {}
Err(e) => {
info!("ERROR! {:?}", e);
debug!("ERROR! {:?}", e);
}
}
}