sqlite_plugin/
logger.rs

1use alloc::ffi::CString;
2use core::ffi::{c_char, c_int};
3
4use crate::vars;
5
6#[allow(non_snake_case)]
7type Sqlite3Log = unsafe extern "C" fn(iErrCode: c_int, arg2: *const c_char, ...);
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub enum SqliteLogLevel {
11    Error = 1,
12    Warn,
13    Notice,
14}
15
16impl SqliteLogLevel {
17    fn into_err_code(self) -> c_int {
18        match self {
19            Self::Notice => vars::SQLITE_NOTICE,
20            Self::Warn => vars::SQLITE_WARNING,
21            Self::Error => vars::SQLITE_INTERNAL,
22        }
23    }
24}
25
26#[derive(Clone, Copy)]
27pub struct SqliteLogger {
28    log: Sqlite3Log,
29}
30
31impl SqliteLogger {
32    pub(crate) fn new(log: Sqlite3Log) -> Self {
33        Self { log }
34    }
35
36    /// Log bytes directly to the `SQLite3` log handle.
37    /// Note that `SQLite` silently truncates writes larger than
38    /// roughly 230 bytes by default. It's recommended that you
39    /// split your log messages by lines before calling this method.
40    pub fn log(&self, level: SqliteLogLevel, msg: &str) {
41        let code = level.into_err_code();
42        let z_format = CString::new(msg).unwrap();
43        unsafe { (self.log)(code, z_format.as_ptr()) }
44    }
45}