Skip to content

Commit

Permalink
Merge pull request #318 from RedisLabsModules/add-logging-implementation
Browse files Browse the repository at this point in the history
Provide the standard logging facility implementation.
  • Loading branch information
iddm authored May 11, 2023
2 parents 02c1ab3 + de65b4e commit 2cba2be
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ serde = { version = "1", features = ["derive"] }
nix = "0.26"
cfg-if = "1"
redis-module-macros-internals = { path = "./redismodule-rs-macros-internals" }
log = "0.4"

[dev-dependencies]
anyhow = "1"
Expand Down
6 changes: 3 additions & 3 deletions examples/load_unload.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use redis_module::{redis_module, Context, LogLevel, RedisString, Status};
use redis_module::{logging::RedisLogLevel, redis_module, Context, RedisString, Status};

static mut GLOBAL_STATE: Option<String> = None;

Expand All @@ -10,7 +10,7 @@ fn init(ctx: &Context, args: &[RedisString]) -> Status {
(before, after)
};
ctx.log(
LogLevel::Warning,
RedisLogLevel::Warning,
&format!("Update global state on LOAD. BEFORE: {before:?}, AFTER: {after:?}",),
);

Expand All @@ -24,7 +24,7 @@ fn deinit(ctx: &Context) -> Status {
(before, after)
};
ctx.log(
LogLevel::Warning,
RedisLogLevel::Warning,
&format!("Update global state on UNLOAD. BEFORE: {before:?}, AFTER: {after:?}"),
);

Expand Down
23 changes: 9 additions & 14 deletions src/context/call_reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,20 +548,15 @@ impl TryFrom<&str> for VerbatimStringFormat {
)));
}
let mut res = VerbatimStringFormat::default();
value
.chars()
.into_iter()
.take(3)
.enumerate()
.try_for_each(|(i, c)| {
if c as u32 >= 127 {
return Err(RedisError::String(
"Verbatim format must contains only ASCI values.".to_owned(),
));
}
res.0[i] = c as c_char;
Ok(())
})?;
value.chars().take(3).enumerate().try_for_each(|(i, c)| {
if c as u32 >= 127 {
return Err(RedisError::String(
"Verbatim format must contains only ASCI values.".to_owned(),
));
}
res.0[i] = c as c_char;
Ok(())
})?;
Ok(res)
}
}
Expand Down
35 changes: 21 additions & 14 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use std::os::raw::{c_char, c_int, c_long, c_longlong};
use std::ptr::{self, NonNull};
use std::sync::atomic::{AtomicPtr, Ordering};

use crate::add_info_section;
use crate::key::{RedisKey, RedisKeyWritable};
use crate::logging::RedisLogLevel;
use crate::raw::{ModuleOptions, Version};
use crate::redisvalue::RedisValueKey;
use crate::{add_info_field_long_long, add_info_field_str, raw, utils, Status};
use crate::{add_info_section, LogLevel};
use crate::{RedisError, RedisResult, RedisString, RedisValue};

use std::ffi::CStr;
Expand Down Expand Up @@ -123,37 +124,43 @@ impl CallOptionsBuilder {
/// It is implemented `Send` and `Sync` so it can safely be used
/// from within different threads.
pub struct DetachedContext {
ctx: AtomicPtr<raw::RedisModuleCtx>,
pub(crate) ctx: AtomicPtr<raw::RedisModuleCtx>,
}

impl Default for DetachedContext {
fn default() -> Self {
impl DetachedContext {
pub const fn new() -> Self {
DetachedContext {
ctx: AtomicPtr::new(ptr::null_mut()),
}
}
}

impl Default for DetachedContext {
fn default() -> Self {
Self::new()
}
}

impl DetachedContext {
pub fn log(&self, level: LogLevel, message: &str) {
pub fn log(&self, level: RedisLogLevel, message: &str) {
let c = self.ctx.load(Ordering::Relaxed);
crate::logging::log_internal(c, level, message);
}

pub fn log_debug(&self, message: &str) {
self.log(LogLevel::Debug, message);
self.log(RedisLogLevel::Debug, message);
}

pub fn log_notice(&self, message: &str) {
self.log(LogLevel::Notice, message);
self.log(RedisLogLevel::Notice, message);
}

pub fn log_verbose(&self, message: &str) {
self.log(LogLevel::Verbose, message);
self.log(RedisLogLevel::Verbose, message);
}

pub fn log_warning(&self, message: &str) {
self.log(LogLevel::Warning, message);
self.log(RedisLogLevel::Warning, message);
}

pub fn set_context(&self, ctx: &Context) -> Result<(), RedisError> {
Expand Down Expand Up @@ -266,24 +273,24 @@ impl Context {
}
}

pub fn log(&self, level: LogLevel, message: &str) {
pub fn log(&self, level: RedisLogLevel, message: &str) {
crate::logging::log_internal(self.ctx, level, message);
}

pub fn log_debug(&self, message: &str) {
self.log(LogLevel::Debug, message);
self.log(RedisLogLevel::Debug, message);
}

pub fn log_notice(&self, message: &str) {
self.log(LogLevel::Notice, message);
self.log(RedisLogLevel::Notice, message);
}

pub fn log_verbose(&self, message: &str) {
self.log(LogLevel::Verbose, message);
self.log(RedisLogLevel::Verbose, message);
}

pub fn log_warning(&self, message: &str) {
self.log(LogLevel::Warning, message);
self.log(RedisLogLevel::Warning, message);
}

/// # Panics
Expand Down
17 changes: 5 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//#![allow(dead_code)]

pub use crate::context::InfoContext;
use strum_macros::AsRefStr;
extern crate num_traits;

pub mod alloc;
Expand Down Expand Up @@ -44,15 +41,11 @@ pub use crate::raw::*;
pub use crate::redismodule::*;
use backtrace::Backtrace;

/// `LogLevel` is a level of logging to be specified with a Redis log directive.
#[derive(Clone, Copy, Debug, AsRefStr)]
#[strum(serialize_all = "snake_case")]
pub enum LogLevel {
Debug,
Notice,
Verbose,
Warning,
}
/// The detached Redis module context (the context of this module). It
/// is only set to a proper value after the module is initialised via the
/// provided [redis_module] macro.
/// See [DetachedContext].
pub static MODULE_CONTEXT: DetachedContext = DetachedContext::new();

pub fn base_info_func(
ctx: &InfoContext,
Expand Down
Loading

0 comments on commit 2cba2be

Please sign in to comment.