|
15 | 15 | // limitations under the License.
|
16 | 16 | //
|
17 | 17 |
|
| 18 | +use bitflags::bitflags; |
18 | 19 | use crate::c_api::{
|
19 |
| - CBLLogDomain, CBLLogLevel, CBLLog_SetCallback, CBLLog_SetCallbackLevel, CBLLog_SetConsoleLevel, |
20 |
| - CBL_Log, FLString, |
| 20 | + kCBLLogDomainMaskAll, kCBLLogDomainMaskDatabase, kCBLLogDomainMaskNetwork, |
| 21 | + kCBLLogDomainMaskQuery, kCBLLogDomainMaskReplicator, CBLConsoleLogSink, CBLCustomLogSink, |
| 22 | + CBLLogDomain, CBLLogLevel, CBLLogSinks_SetConsole, CBLLogSinks_SetCustom, FLString, |
21 | 23 | };
|
22 | 24 |
|
23 | 25 | use enum_primitive::FromPrimitive;
|
24 |
| -use std::fmt; |
25 |
| -use std::ffi::CString; |
26 | 26 |
|
27 | 27 | enum_from_primitive! {
|
28 | 28 | /** Logging domains: subsystems that generate log messages. */
|
@@ -50,96 +50,65 @@ enum_from_primitive! {
|
50 | 50 | }
|
51 | 51 | }
|
52 | 52 |
|
53 |
| -pub type LogCallback = Option<fn(Domain, Level, &str)>; |
| 53 | +bitflags! { |
| 54 | + /** A bitmask representing a set of logging domains. |
| 55 | + * |
| 56 | + * Use this bitmask to specify one or more logging domains by combining the |
| 57 | + * constants with the bitwise OR operator (`|`). This is helpful for enabling |
| 58 | + * or filtering logs for specific domains. */ |
| 59 | + #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 60 | + pub struct DomainMask: u32 { |
| 61 | + const DATABASE = kCBLLogDomainMaskDatabase; |
| 62 | + const QUERY = kCBLLogDomainMaskQuery; |
| 63 | + const REPLICATOR = kCBLLogDomainMaskReplicator; |
| 64 | + const NETWORK = kCBLLogDomainMaskNetwork; |
| 65 | + const ALL = kCBLLogDomainMaskAll; |
| 66 | + } |
| 67 | +} |
54 | 68 |
|
55 |
| -/** Sets the detail level of console logging. |
56 |
| -Only messages whose level is ≥ the given level will be logged to the console. |
57 |
| -Default value is Info. */ |
58 |
| -pub fn set_console_level(level: Level) { |
59 |
| - unsafe { CBLLog_SetConsoleLevel(level as u8) } |
| 69 | +/** Console log sink configuration for logging to the cosole. */ |
| 70 | +pub struct ConsoleLogSink { |
| 71 | + // The minimum level of message to write (Required). |
| 72 | + pub level: Level, |
| 73 | + // Bitmask for enabled log domains. |
| 74 | + pub domains: DomainMask, |
60 | 75 | }
|
61 | 76 |
|
62 |
| -/** Sets the detail level of logging to the registered callback (if any.) |
63 |
| -Only messages whose level is ≥ the given level will be logged to the callback. |
64 |
| -Default value is Info. */ |
65 |
| -pub fn set_callback_level(level: Level) { |
66 |
| - unsafe { CBLLog_SetCallbackLevel(level as u8) } |
| 77 | +pub type LogCallback = Option<fn(Domain, Level, &str)>; |
| 78 | + |
| 79 | +/** Custom log sink configuration for logging to a user-defined callback. */ |
| 80 | +pub struct CustomLogSink { |
| 81 | + // The minimum level of message to write (Required). |
| 82 | + pub level: Level, |
| 83 | + // Custom log callback (Required). |
| 84 | + pub callback: LogCallback, |
| 85 | + // Bitmask for enabled log domains. |
| 86 | + pub domains: DomainMask, |
67 | 87 | }
|
68 | 88 |
|
69 |
| -/** Registers a function that will receive log messages. */ |
70 |
| -pub fn set_callback(callback: LogCallback) { |
| 89 | +/** Set the console log sink. To disable the console log sink, set the log level to None. */ |
| 90 | +pub fn set_console_log_sink(log_sink: ConsoleLogSink) { |
71 | 91 | unsafe {
|
72 |
| - LOG_CALLBACK = callback; |
73 |
| - if callback.is_some() { |
74 |
| - CBLLog_SetCallback(Some(invoke_log_callback)); |
75 |
| - } else { |
76 |
| - CBLLog_SetCallback(None); |
77 |
| - } |
| 92 | + CBLLogSinks_SetConsole(CBLConsoleLogSink { |
| 93 | + level: log_sink.level as u8, |
| 94 | + domains: log_sink.domains.bits() as u16, |
| 95 | + }) |
78 | 96 | }
|
79 | 97 | }
|
80 | 98 |
|
81 |
| -/** Writes a log message. */ |
82 |
| -pub fn write(domain: Domain, level: Level, message: &str) { |
| 99 | +/** Set the custom log sink. To disable the custom log sink, set the log level to None. */ |
| 100 | +pub fn set_custom_log_sink(log_sink: CustomLogSink) { |
83 | 101 | unsafe {
|
84 |
| - let cstr = CString::new(message).unwrap(); |
85 |
| - CBL_Log(domain as u8, level as u8, cstr.as_ptr()); |
| 102 | + LOG_CALLBACK = log_sink.callback; |
86 | 103 |
|
87 |
| - // CBL_Log doesn't invoke the callback, so do it manually: |
88 |
| - if let Some(callback) = LOG_CALLBACK { |
89 |
| - //if CBLLog_WillLogToConsole(domain as u8, level as u8) { |
90 |
| - callback(domain, level, message); |
91 |
| - //} |
92 |
| - } |
| 104 | + CBLLogSinks_SetCustom(CBLCustomLogSink { |
| 105 | + level: log_sink.level as u8, |
| 106 | + callback: Some(invoke_log_callback), |
| 107 | + domains: log_sink.domains.bits() as u16, |
| 108 | + }) |
93 | 109 | }
|
94 | 110 | }
|
95 | 111 |
|
96 |
| -/** Writes a log message using the given format arguments. */ |
97 |
| -pub fn write_args(domain: Domain, level: Level, args: fmt::Arguments) { |
98 |
| - write(domain, level, &format!("{:?}", args)); |
99 |
| -} |
100 |
| - |
101 |
| -//////// LOGGING MACROS: |
102 |
| - |
103 |
| -/// A macro that writes a formatted Error-level log message. |
104 |
| -#[macro_export] |
105 |
| -macro_rules! error { |
106 |
| - ($($arg:tt)*) => ($crate::logging::write_args( |
107 |
| - $crate::logging::Domain::Database, $crate::logging::Level::Error, |
108 |
| - format_args!($($arg)*))); |
109 |
| -} |
110 |
| - |
111 |
| -/// A macro that writes a formatted Warning-level log message. |
112 |
| -#[macro_export] |
113 |
| -macro_rules! warn { |
114 |
| - ($($arg:tt)*) => ($crate::logging::write_args( |
115 |
| - $crate::logging::Domain::Database, $crate::logging::Level::Warning, |
116 |
| - format_args!($($arg)*))); |
117 |
| -} |
118 |
| - |
119 |
| -/// A macro that writes a formatted Info-level log message. |
120 |
| -#[macro_export] |
121 |
| -macro_rules! info { |
122 |
| - ($($arg:tt)*) => ($crate::logging::write_args( |
123 |
| - $crate::logging::Domain::Database, $crate::logging::Level::Info, |
124 |
| - format_args!($($arg)*))); |
125 |
| -} |
126 |
| - |
127 |
| -/// A macro that writes a formatted Verbose-level log message. |
128 |
| -#[macro_export] |
129 |
| -macro_rules! verbose { |
130 |
| - ($($arg:tt)*) => ($crate::logging::write_args( |
131 |
| - $crate::logging::Domain::Database, $crate::logging::Level::Verbose, |
132 |
| - format_args!($($arg)*))); |
133 |
| -} |
134 |
| - |
135 |
| -/// A macro that writes a formatted Debug-level log message. |
136 |
| -#[macro_export] |
137 |
| -macro_rules! debug { |
138 |
| - ($($arg:tt)*) => ($crate::logging::write_args( |
139 |
| - $crate::logging::Domain::Database, $crate::logging::Level::Debug, |
140 |
| - format_args!($($arg)*))); |
141 |
| -} |
142 |
| - |
143 | 112 | //////// INTERNALS:
|
144 | 113 |
|
145 | 114 | static mut LOG_CALLBACK: LogCallback = None;
|
|
0 commit comments