diff --git a/Cargo.toml b/Cargo.toml index f796d32..42df66c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,13 +1,13 @@ [package] name = "scribe" version = "0.5.0" -authors = ["Jason Travis Smith "] +authors = ["Jason Travis Smith "] description = "Handles logging." license = "" -repository = "https://gitlab.com/CyberMages/Core/scribe.git" +repository = "https://workshop.cybermages.tech/CyberMages/scribe.git" documentation = "" keywords = ["scribe", "logging"] -edition = "2018" +edition = "2021" [features] diff --git a/examples/logger.rs b/examples/logger.rs index 2c61e29..4501240 100644 --- a/examples/logger.rs +++ b/examples/logger.rs @@ -60,7 +60,7 @@ impl TestLogger } #[cfg(not(feature="use_std"))] - fn set_logger() -> *const Logger + fn set_logger() -> *const dyn Logger { &CORE_LOGGER } diff --git a/src/guard.rs b/src/guard.rs index 056d8b2..6c37774 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -1,12 +1,12 @@ use std::ops::Deref; -use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; +use std::sync::atomic::{AtomicUsize, Ordering}; use crate::logger::Logger; /// The amount of references that have currently been created. -static REF_COUNT: AtomicUsize = ATOMIC_USIZE_INIT; +static REF_COUNT: AtomicUsize = AtomicUsize::new(0); @@ -16,14 +16,14 @@ static REF_COUNT: AtomicUsize = ATOMIC_USIZE_INIT; /// with static variables. pub struct Guard { - logger: &'static Logger + logger: &'static dyn Logger } impl Guard { /// Create a new reference counted Guard. - pub fn new(global_logger: *const Logger) -> Guard + pub fn new(global_logger: *const dyn Logger) -> Guard { REF_COUNT.fetch_add(1, Ordering::SeqCst); unsafe @@ -57,9 +57,9 @@ impl Drop for Guard impl Deref for Guard { - type Target = Logger; + type Target = dyn Logger; - fn deref(&self) -> &(Logger + 'static) + fn deref(&self) -> &(dyn Logger + 'static) { self.logger } diff --git a/src/lib.rs b/src/lib.rs index f66b915..06fe605 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ mod record; -use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; +use std::sync::atomic::{AtomicUsize, Ordering}; use self::faux_logger::FauxLogger; use self::guard::Guard; @@ -47,10 +47,10 @@ pub use self::record::Record; /// This is the currently set global Logger. /// It will default to a fake Logger that does nothing. -static mut GLOBAL_LOGGER: *const Logger = &FauxLogger; +static mut GLOBAL_LOGGER: *const dyn Logger = &FauxLogger; /// The STATE of the GLOBAL_LOGGER. -static STATE: AtomicUsize = ATOMIC_USIZE_INIT; +static STATE: AtomicUsize = AtomicUsize::new(0); @@ -58,14 +58,25 @@ static STATE: AtomicUsize = ATOMIC_USIZE_INIT; /// it ready to log new Records. fn set_logger_base(create_logger: F) -> Result<(), u8> - where F: FnOnce() -> *const Logger + where F: FnOnce() -> *const dyn Logger { - if STATE.compare_and_swap(LoggerStates::Uninitialized.into(), - LoggerStates::Initializing.into(), - Ordering::SeqCst) != - LoggerStates::Uninitialized.into() + match STATE.compare_exchange(LoggerStates::Uninitialized.into(), + LoggerStates::Initializing.into(), + Ordering::SeqCst, + Ordering::SeqCst) { - return Err(1); + Ok(val) => + { + if val != LoggerStates::Uninitialized.into() + { + return Err(0); + } + } + + Err(_val) => + { + return Err(0); + } } unsafe @@ -79,17 +90,28 @@ fn set_logger_base(create_logger: F) /// Handles clearing the GLOBAL_LOGGER. /// This will return a pointer to the Logger that /// was previously set. -fn clear_logger_base() -> Result<*const Logger, u8> +fn clear_logger_base() -> Result<*const dyn Logger, u8> { - let logger: *const Logger; + let logger: *const dyn Logger; // Set to INITIALIZING to prevent re-initialization after - if STATE.compare_and_swap(LoggerStates::Initialized.into(), + match STATE.compare_exchange(LoggerStates::Initialized.into(), LoggerStates::Initializing.into(), - Ordering::SeqCst) != - LoggerStates::Initialized.into() + Ordering::SeqCst, + Ordering::SeqCst) { - return Err(2); + Ok(val) => + { + if val != LoggerStates::Initialized.into() + { + return Err(2); + } + } + + Err(_val) => + { + return Err(2); + } } // Wait until there are no references alive to the @@ -133,7 +155,7 @@ fn get_logger() -> Option #[cfg(not(feature="use_std"))] pub fn set_logger(create_logger: F) -> Result<(), u8> - where F: FnOnce() -> *const Logger + where F: FnOnce() -> *const dyn Logger { set_logger_base(create_logger) } @@ -143,7 +165,7 @@ pub fn set_logger(create_logger: F) /// This will return a pointer to the Logger that /// was previously set. #[cfg(not(feature="use_std"))] -pub fn clear_logger() -> Result<*const Logger, u8> +pub fn clear_logger() -> Result<*const dyn Logger, u8> { clear_logger_base() }