Skip to content

Commit

Permalink
Merge pull request #37 from mkmkme/mkmkme/fix-warning
Browse files Browse the repository at this point in the history
logging: remove `static mut` from the module
  • Loading branch information
dmitrypol authored Jul 16, 2024
2 parents 5fec2ec + 4550d2e commit eebbff9
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,13 @@ pub fn log_warning<T: AsRef<str>>(message: T) {

/// The [log] crate implementation of logging.
pub mod standard_log_implementation {
use std::sync::atomic::Ordering;
use std::sync::{atomic::Ordering, OnceLock};

use crate::ValkeyError;

use super::*;
use log::{Metadata, Record, SetLoggerError};

static mut LOGGER: ValkeyGlobalLogger = ValkeyGlobalLogger(ptr::null_mut());

/// The struct which has an implementation of the [log] crate's
/// logging interface.
///
Expand Down Expand Up @@ -152,13 +150,15 @@ pub mod standard_log_implementation {
.map_err(|e| ValkeyError::String(format!("Couldn't set up the logger: {e}")))
}

fn logger(context: *mut raw::RedisModuleCtx) -> &'static ValkeyGlobalLogger {
static LOGGER: OnceLock<ValkeyGlobalLogger> = OnceLock::new();
LOGGER.get_or_init(|| ValkeyGlobalLogger(context))
}

/// The same as [setup] but sets the custom module context.
#[allow(dead_code)]
pub fn setup_for_context(context: *mut raw::RedisModuleCtx) -> Result<(), SetLoggerError> {
unsafe {
LOGGER.0 = context;
log::set_logger(&LOGGER).map(|()| log::set_max_level(log::LevelFilter::Trace))
}
log::set_logger(logger(context)).map(|()| log::set_max_level(log::LevelFilter::Trace))
}

impl log::Log for ValkeyGlobalLogger {
Expand Down

0 comments on commit eebbff9

Please sign in to comment.