1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::raw;
use crate::LogLevel;
use std::ffi::CString;
use std::ptr;

pub(crate) fn log_internal(ctx: *mut raw::RedisModuleCtx, level: LogLevel, message: &str) {
    if cfg!(test) {
        return;
    }

    let level = CString::new(level.as_ref()).unwrap();
    let fmt = CString::new(message).unwrap();
    unsafe { raw::RedisModule_Log.unwrap()(ctx, level.as_ptr(), fmt.as_ptr()) }
}

/// This function should be used when a callback is returning a critical error
/// to the caller since cannot load or save the data for some critical reason.
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub fn log_io_error(io: *mut raw::RedisModuleIO, level: LogLevel, message: &str) {
    if cfg!(test) {
        return;
    }
    let level = CString::new(level.as_ref()).unwrap();
    let fmt = CString::new(message).unwrap();
    unsafe { raw::RedisModule_LogIOError.unwrap()(io, level.as_ptr(), fmt.as_ptr()) }
}

/// Log a message to the Redis log with the given log level, without
/// requiring a context. This prevents Redis from including the module
/// name in the logged message.
pub fn log(level: LogLevel, message: &str) {
    log_internal(ptr::null_mut(), level, message);
}

/// Log a message to the Redis log with DEBUG log level.
pub fn log_debug(message: &str) {
    log(LogLevel::Debug, message);
}

/// Log a message to the Redis log with NOTICE log level.
pub fn log_notice(message: &str) {
    log(LogLevel::Notice, message);
}

/// Log a message to the Redis log with VERBOSE log level.
pub fn log_verbose(message: &str) {
    log(LogLevel::Verbose, message);
}

/// Log a message to the Redis log with WARNING log level.
pub fn log_warning(message: &str) {
    log(LogLevel::Warning, message);
}