-
Notifications
You must be signed in to change notification settings - Fork 0
cblite 3.2.2 update: New logging API #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,11 @@ void log_callback(CBLLogDomain domain, CBLLogLevel level, FLString message) { | |
} | ||
|
||
int main(void) { | ||
CBLLog_SetCallbackLevel(kCBLLogVerbose); | ||
CBLLog_SetConsoleLevel(kCBLLogVerbose); | ||
CBLLog_SetCallback(log_callback); | ||
CBLConsoleLogSink log_sink = {}; | ||
log_sink.level = kCBLLogDebug; | ||
log_sink.domains = kCBLLogDomainMaskAll; | ||
|
||
CBLLogSinks_SetConsole(log_sink); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR review help: Just for testing... |
||
|
||
// Open database | ||
CBLError error; | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -15,14 +15,14 @@ | |||
// limitations under the License. | ||||
// | ||||
|
||||
use bitflags::bitflags; | ||||
use crate::c_api::{ | ||||
CBLLogDomain, CBLLogLevel, CBLLog_SetCallback, CBLLog_SetCallbackLevel, CBLLog_SetConsoleLevel, | ||||
CBL_Log, FLString, | ||||
kCBLLogDomainMaskAll, kCBLLogDomainMaskDatabase, kCBLLogDomainMaskNetwork, | ||||
kCBLLogDomainMaskQuery, kCBLLogDomainMaskReplicator, CBLConsoleLogSink, CBLCustomLogSink, | ||||
CBLLogDomain, CBLLogLevel, CBLLogSinks_SetConsole, CBLLogSinks_SetCustom, FLString, | ||||
}; | ||||
|
||||
use enum_primitive::FromPrimitive; | ||||
use std::fmt; | ||||
use std::ffi::CString; | ||||
|
||||
enum_from_primitive! { | ||||
/** Logging domains: subsystems that generate log messages. */ | ||||
|
@@ -50,96 +50,65 @@ enum_from_primitive! { | |||
} | ||||
} | ||||
|
||||
pub type LogCallback = Option<fn(Domain, Level, &str)>; | ||||
bitflags! { | ||||
/** A bitmask representing a set of logging domains. | ||||
* | ||||
* Use this bitmask to specify one or more logging domains by combining the | ||||
* constants with the bitwise OR operator (`|`). This is helpful for enabling | ||||
* or filtering logs for specific domains. */ | ||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||
pub struct DomainMask: u32 { | ||||
const DATABASE = kCBLLogDomainMaskDatabase; | ||||
const QUERY = kCBLLogDomainMaskQuery; | ||||
const REPLICATOR = kCBLLogDomainMaskReplicator; | ||||
const NETWORK = kCBLLogDomainMaskNetwork; | ||||
const ALL = kCBLLogDomainMaskAll; | ||||
} | ||||
} | ||||
|
||||
/** Sets the detail level of console logging. | ||||
Only messages whose level is ≥ the given level will be logged to the console. | ||||
Default value is Info. */ | ||||
pub fn set_console_level(level: Level) { | ||||
unsafe { CBLLog_SetConsoleLevel(level as u8) } | ||||
/** Console log sink configuration for logging to the cosole. */ | ||||
pub struct ConsoleLogSink { | ||||
// The minimum level of message to write (Required). | ||||
pub level: Level, | ||||
// Bitmask for enabled log domains. | ||||
pub domains: DomainMask, | ||||
} | ||||
|
||||
/** Sets the detail level of logging to the registered callback (if any.) | ||||
Only messages whose level is ≥ the given level will be logged to the callback. | ||||
Default value is Info. */ | ||||
pub fn set_callback_level(level: Level) { | ||||
unsafe { CBLLog_SetCallbackLevel(level as u8) } | ||||
pub type LogCallback = Option<fn(Domain, Level, &str)>; | ||||
|
||||
/** Custom log sink configuration for logging to a user-defined callback. */ | ||||
pub struct CustomLogSink { | ||||
// The minimum level of message to write (Required). | ||||
pub level: Level, | ||||
// Custom log callback (Required). | ||||
pub callback: LogCallback, | ||||
// Bitmask for enabled log domains. | ||||
pub domains: DomainMask, | ||||
} | ||||
|
||||
/** Registers a function that will receive log messages. */ | ||||
pub fn set_callback(callback: LogCallback) { | ||||
/** Set the console log sink. To disable the console log sink, set the log level to None. */ | ||||
pub fn set_console_log_sink(log_sink: ConsoleLogSink) { | ||||
unsafe { | ||||
LOG_CALLBACK = callback; | ||||
if callback.is_some() { | ||||
CBLLog_SetCallback(Some(invoke_log_callback)); | ||||
} else { | ||||
CBLLog_SetCallback(None); | ||||
} | ||||
CBLLogSinks_SetConsole(CBLConsoleLogSink { | ||||
level: log_sink.level as u8, | ||||
domains: log_sink.domains.bits() as u16, | ||||
}) | ||||
} | ||||
} | ||||
|
||||
/** Writes a log message. */ | ||||
pub fn write(domain: Domain, level: Level, message: &str) { | ||||
/** Set the custom log sink. To disable the custom log sink, set the log level to None. */ | ||||
pub fn set_custom_log_sink(log_sink: CustomLogSink) { | ||||
unsafe { | ||||
let cstr = CString::new(message).unwrap(); | ||||
CBL_Log(domain as u8, level as u8, cstr.as_ptr()); | ||||
LOG_CALLBACK = log_sink.callback; | ||||
|
||||
// CBL_Log doesn't invoke the callback, so do it manually: | ||||
if let Some(callback) = LOG_CALLBACK { | ||||
//if CBLLog_WillLogToConsole(domain as u8, level as u8) { | ||||
callback(domain, level, message); | ||||
//} | ||||
} | ||||
CBLLogSinks_SetCustom(CBLCustomLogSink { | ||||
level: log_sink.level as u8, | ||||
callback: Some(invoke_log_callback), | ||||
domains: log_sink.domains.bits() as u16, | ||||
}) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR review help: implementation of CBLite C lib (thanks to bindings), more info in Line 126 in 36a9151
|
||||
} | ||||
} | ||||
|
||||
/** Writes a log message using the given format arguments. */ | ||||
pub fn write_args(domain: Domain, level: Level, args: fmt::Arguments) { | ||||
write(domain, level, &format!("{:?}", args)); | ||||
} | ||||
|
||||
//////// LOGGING MACROS: | ||||
|
||||
/// A macro that writes a formatted Error-level log message. | ||||
#[macro_export] | ||||
macro_rules! error { | ||||
($($arg:tt)*) => ($crate::logging::write_args( | ||||
$crate::logging::Domain::Database, $crate::logging::Level::Error, | ||||
format_args!($($arg)*))); | ||||
} | ||||
|
||||
/// A macro that writes a formatted Warning-level log message. | ||||
#[macro_export] | ||||
macro_rules! warn { | ||||
($($arg:tt)*) => ($crate::logging::write_args( | ||||
$crate::logging::Domain::Database, $crate::logging::Level::Warning, | ||||
format_args!($($arg)*))); | ||||
} | ||||
|
||||
/// A macro that writes a formatted Info-level log message. | ||||
#[macro_export] | ||||
macro_rules! info { | ||||
($($arg:tt)*) => ($crate::logging::write_args( | ||||
$crate::logging::Domain::Database, $crate::logging::Level::Info, | ||||
format_args!($($arg)*))); | ||||
} | ||||
|
||||
/// A macro that writes a formatted Verbose-level log message. | ||||
#[macro_export] | ||||
macro_rules! verbose { | ||||
($($arg:tt)*) => ($crate::logging::write_args( | ||||
$crate::logging::Domain::Database, $crate::logging::Level::Verbose, | ||||
format_args!($($arg)*))); | ||||
} | ||||
|
||||
/// A macro that writes a formatted Debug-level log message. | ||||
#[macro_export] | ||||
macro_rules! debug { | ||||
($($arg:tt)*) => ($crate::logging::write_args( | ||||
$crate::logging::Domain::Database, $crate::logging::Level::Debug, | ||||
format_args!($($arg)*))); | ||||
} | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PR review help: Removed because useless |
||||
//////// INTERNALS: | ||||
|
||||
static mut LOG_CALLBACK: LogCallback = None; | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR review help: New dependency to implement bit masks (we need to map to a CBlite C structure that create bit mask to concatenate domains to log, see
couchbase-lite-rust/libcblite_community/include/cbl/CBLLogSinks.h
Lines 50 to 61 in 3ffe3a4