Skip to content

Commit

Permalink
Update lib.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanpoland committed Nov 20, 2024
1 parent f0d06b8 commit 7493f6f
Showing 1 changed file with 52 additions and 41 deletions.
93 changes: 52 additions & 41 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::OnceLock;
use chrono::Local;

// import async specific stuff, if enabled
#[cfg(feature = "async-tokio")]
use tokio::fs::File;
#[cfg(feature = "async-tokio")]
Expand All @@ -11,7 +10,6 @@ use tokio::io::AsyncWriteExt;
#[cfg(feature = "async-tokio")]
use tokio::fs::OpenOptions;

// and then blocking specific stuff, if enabled
#[cfg(not(feature = "async-tokio"))]
use std::fs::File;
#[cfg(not(feature = "async-tokio"))]
Expand All @@ -24,77 +22,90 @@ use std::fs::OpenOptions;
static LOG_FILE: OnceLock<Mutex<File>> = OnceLock::new();

#[cfg(not(feature = "async-tokio"))]
pub fn log_message(message: &str) {
pub fn log_message(message: &str) -> std::io::Result<()> {
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
let log_message = format!("[{}] {}\n", timestamp, message);

print!("{}", log_message); // Print to console

print!("{}", log_message); // Print to console

if let Some(lock) = LOG_FILE.get() {
if let Ok(mut file) = lock.lock() {
let _ = file.write_all(log_message.as_bytes());
}
let mut file = lock.lock().map_err(|_| {
std::io::Error::new(std::io::ErrorKind::Other, "Failed to acquire lock")
})?;
file.write_all(log_message.as_bytes())?;
file.flush()?; // Ensure the data is written to disk
}
Ok(())
}

#[cfg(feature = "async-tokio")]
pub async fn log_message(message: &str) {
pub async fn log_message(message: &str) -> std::io::Result<()> {
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
let log_message = format!("[{}] {}\n", timestamp, message);

print!("{}", log_message); // Print to console

print!("{}", log_message); // Print to console

if let Some(lock) = LOG_FILE.get() {
let mut file = lock.lock().await;
let _ = file.write_all(log_message.as_bytes()).await;
file.write_all(log_message.as_bytes()).await?;
file.flush().await?; // Ensure the data is written to disk
}
Ok(())
}

#[cfg(feature = "async-tokio")]
#[macro_export]
macro_rules! println {
($($arg:tt)*) => {{
let message = format!($($arg)*);
$crate::log_message(&message).await;
if let Err(e) = $crate::log_message(&message).await {
eprintln!("Logging error: {}", e);
}
}};
}

#[cfg(not(feature = "async-tokio"))]
#[macro_export]
macro_rules! println {
($($arg:tt)*) => {{
let message = format!($($arg)*);
$crate::log_message(&message);
if let Err(e) = $crate::log_message(&message) {
eprintln!("Logging error: {}", e);
}
}};
}

#[cfg(not(feature = "async-tokio"))]
pub fn init() {
let log_file: Mutex<File> = Mutex::new(
OpenOptions::new()
.create(true)
.append(true)
.open("server.log")
.expect("Failed to open log file")
.into()
);
LOG_FILE.set(log_file).expect("Failed to initialize LOG_FILE");

pub fn init() -> std::io::Result<()> {
let file = OpenOptions::new()
.create(true)
.append(true)
.open("server.log")?;

let log_file: Mutex<File> = Mutex::new(file);
LOG_FILE.set(log_file)
.map_err(|_| std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to initialize LOG_FILE"
))?;

println!("Logging system initialized");
Ok(())
}

#[cfg(feature = "async-tokio")]
pub async fn init() {
let log_file: Mutex<File> = Mutex::new(
OpenOptions::new()
.create(true)
.append(true)
.open("server.log")
.await
.expect("Failed to open log file")
.into()
);
LOG_FILE.set(log_file).expect("Failed to initialize LOG_FILE");

pub async fn init() -> std::io::Result<()> {
let file = OpenOptions::new()
.create(true)
.append(true)
.open("server.log")
.await?;

let log_file: Mutex<File> = Mutex::new(file);
LOG_FILE.set(log_file)
.map_err(|_| std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to initialize LOG_FILE"
))?;

println!("Logging system initialized");
}

Ok(())
}

0 comments on commit 7493f6f

Please sign in to comment.