[#1] Updated the library to compile for Rust 2021.

This was mostly an issue of changing the way Trait objects were
implemented now that you are supposed to use the dyn keyword.

Also changed some atomic operations to use the new non-deprecated
functions.
This commit is contained in:
Myrddin Dundragon 2025-02-19 19:47:37 -05:00
parent a1ebb45e35
commit 45efdb8ccb
4 changed files with 49 additions and 27 deletions

View File

@ -1,13 +1,13 @@
[package] [package]
name = "scribe" name = "scribe"
version = "0.5.0" version = "0.5.0"
authors = ["Jason Travis Smith <Myrddin@CyberMagesLLC.com>"] authors = ["Jason Travis Smith <Myrddin@CyberMages.tech>"]
description = "Handles logging." description = "Handles logging."
license = "" license = ""
repository = "https://gitlab.com/CyberMages/Core/scribe.git" repository = "https://workshop.cybermages.tech/CyberMages/scribe.git"
documentation = "" documentation = ""
keywords = ["scribe", "logging"] keywords = ["scribe", "logging"]
edition = "2018" edition = "2021"
[features] [features]

View File

@ -60,7 +60,7 @@ impl TestLogger
} }
#[cfg(not(feature="use_std"))] #[cfg(not(feature="use_std"))]
fn set_logger() -> *const Logger fn set_logger() -> *const dyn Logger
{ {
&CORE_LOGGER &CORE_LOGGER
} }

View File

@ -1,12 +1,12 @@
use std::ops::Deref; use std::ops::Deref;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
use crate::logger::Logger; use crate::logger::Logger;
/// The amount of references that have currently been created. /// 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. /// with static variables.
pub struct Guard pub struct Guard
{ {
logger: &'static Logger logger: &'static dyn Logger
} }
impl Guard impl Guard
{ {
/// Create a new reference counted 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); REF_COUNT.fetch_add(1, Ordering::SeqCst);
unsafe unsafe
@ -57,9 +57,9 @@ impl Drop for Guard
impl Deref 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 self.logger
} }

View File

@ -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::faux_logger::FauxLogger;
use self::guard::Guard; use self::guard::Guard;
@ -47,10 +47,10 @@ pub use self::record::Record;
/// This is the currently set global Logger. /// This is the currently set global Logger.
/// It will default to a fake Logger that does nothing. /// 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. /// 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. /// it ready to log new Records.
fn set_logger_base<F>(create_logger: F) fn set_logger_base<F>(create_logger: F)
-> Result<(), u8> -> Result<(), u8>
where F: FnOnce() -> *const Logger where F: FnOnce() -> *const dyn Logger
{ {
if STATE.compare_and_swap(LoggerStates::Uninitialized.into(), match STATE.compare_exchange(LoggerStates::Uninitialized.into(),
LoggerStates::Initializing.into(), LoggerStates::Initializing.into(),
Ordering::SeqCst) != Ordering::SeqCst,
LoggerStates::Uninitialized.into() Ordering::SeqCst)
{ {
return Err(1); Ok(val) =>
{
if val != LoggerStates::Uninitialized.into()
{
return Err(0);
}
}
Err(_val) =>
{
return Err(0);
}
} }
unsafe unsafe
@ -79,17 +90,28 @@ fn set_logger_base<F>(create_logger: F)
/// Handles clearing the GLOBAL_LOGGER. /// Handles clearing the GLOBAL_LOGGER.
/// This will return a pointer to the Logger that /// This will return a pointer to the Logger that
/// was previously set. /// 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 // 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(), LoggerStates::Initializing.into(),
Ordering::SeqCst) != Ordering::SeqCst,
LoggerStates::Initialized.into() 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 // Wait until there are no references alive to the
@ -133,7 +155,7 @@ fn get_logger() -> Option<Guard>
#[cfg(not(feature="use_std"))] #[cfg(not(feature="use_std"))]
pub fn set_logger<F>(create_logger: F) pub fn set_logger<F>(create_logger: F)
-> Result<(), u8> -> Result<(), u8>
where F: FnOnce() -> *const Logger where F: FnOnce() -> *const dyn Logger
{ {
set_logger_base(create_logger) set_logger_base(create_logger)
} }
@ -143,7 +165,7 @@ pub fn set_logger<F>(create_logger: F)
/// This will return a pointer to the Logger that /// This will return a pointer to the Logger that
/// was previously set. /// was previously set.
#[cfg(not(feature="use_std"))] #[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() clear_logger_base()
} }