<commit_before>use libc::{c_char, c_int, c_void};
use std::ffi::CString;
use std::ptr;

use super::ffi;
use {SqliteError, SqliteResult, SqliteConnection};

pub type LogCallback =
    Option<extern "C" fn (udp: *mut c_void, err: c_int, msg: *const c_char)>;

/// Set up the error logging callback
///
/// cf [The Error And Warning Log](http://sqlite.org/errlog.html).
pub fn config_log(cb: LogCallback) -> SqliteResult<()> {
    let rc = unsafe {
        let p_arg: *mut c_void = ptr::null_mut();
        ffi::sqlite3_config(ffi::SQLITE_CONFIG_LOG, cb, p_arg)
    };
    if rc != ffi::SQLITE_OK {
        return Err(SqliteError{ code: rc, message: "sqlite3_config(SQLITE_CONFIG_LOG, ...)".to_string() });
    }
    Ok(())
}

/// Write a message into the error log established by `config_log`.
pub fn log(err_code: c_int, msg: &str) {
    let msg = CString::new(msg).unwrap();
    unsafe {
        ffi::sqlite3_log(err_code, msg.as_ptr());
    }
}

pub type TraceCallback =
    Option<extern "C" fn (p_arg: *mut c_void, z_sql: *const c_char)>;
pub type ProfileCallback =
    Option<extern "C" fn (p_arg: *mut c_void, z_sql: *const c_char, nanoseconds: u64)>;
impl SqliteConnection {
    /// Register or clear a callback function that can be used for tracing the execution of SQL statements.
    /// Prepared statement placeholders are replaced/logged with their assigned values.
    /// There can only be a single tracer defined for each database connection.
    /// Setting a new tracer clears the old one.
    pub fn trace(&mut self, x_trace: TraceCallback) {
        let c = self.db.borrow_mut();
        unsafe { ffi::sqlite3_trace(c.db(), x_trace, ptr::null_mut()); }
    }
    /// Register or clear a callback function that can be used for profiling the execution of SQL statements.
    /// There can only be a single profiler defined for each database connection.
    /// Setting a new profiler clears the old one.
    pub fn profile(&mut self, x_profile: ProfileCallback) {
        let c = self.db.borrow_mut();
        unsafe { ffi::sqlite3_profile(c.db(), x_profile, ptr::null_mut()); }
    }
}

#[cfg(test)]
mod test {
    use libc::{c_char, c_int, c_void};
    use std::ffi::CStr;
    use std::io::Write;
    use std::str;

    use ffi;
    use SqliteConnection;

    extern "C" fn log_callback(_: *mut c_void, err: c_int, msg: *const c_char) {<commit_msg>Improve documentation.<commit_after>//! Tracing and profiling functions. Error and warning log.
use libc::{c_char, c_int, c_void};
use std::ffi::CString;
use std::ptr;

use super::ffi;
use {SqliteError, SqliteResult, SqliteConnection};

pub type LogCallback =
    Option<extern "C" fn (udp: *mut c_void, err: c_int, msg: *const c_char)>;

/// Set up the error logging callback
///
/// cf [The Error And Warning Log](http://sqlite.org/errlog.html).
pub fn config_log(cb: LogCallback) -> SqliteResult<()> {
    let rc = unsafe {
        let p_arg: *mut c_void = ptr::null_mut();
        ffi::sqlite3_config(ffi::SQLITE_CONFIG_LOG, cb, p_arg)
    };
    if rc != ffi::SQLITE_OK {
        return Err(SqliteError{ code: rc, message: "sqlite3_config(SQLITE_CONFIG_LOG, ...)".to_string() });
    }
    Ok(())
}

/// Write a message into the error log established by `config_log`.
pub fn log(err_code: c_int, msg: &str) {
    let msg = CString::new(msg).unwrap();
    unsafe {
        ffi::sqlite3_log(err_code, msg.as_ptr());
    }
}

/// The trace callback function signature.
pub type TraceCallback =
    Option<extern "C" fn (p_arg: *mut c_void, z_sql: *const c_char)>;
/// The profile callback function signature.
pub type ProfileCallback =
    Option<extern "C" fn (p_arg: *mut c_void, z_sql: *const c_char, nanoseconds: u64)>;

impl SqliteConnection {
    /// Register or clear a callback function that can be used for tracing the execution of SQL statements.
    ///
    /// Prepared statement placeholders are replaced/logged with their assigned values.
    /// There can only be a single tracer defined for each database connection.
    /// Setting a new tracer clears the old one.
    pub fn trace(&mut self, x_trace: TraceCallback) {
        let c = self.db.borrow_mut();
        unsafe { ffi::sqlite3_trace(c.db(), x_trace, ptr::null_mut()); }
    }
    /// Register or clear a callback function that can be used for profiling the execution of SQL statements.
    ///
    /// There can only be a single profiler defined for each database connection.
    /// Setting a new profiler clears the old one.
    pub fn profile(&mut self, x_profile: ProfileCallback) {
        let c = self.db.borrow_mut();
        unsafe { ffi::sqlite3_profile(c.db(), x_profile, ptr::null_mut()); }
    }
}

#[cfg(test)]
mod test {
    use libc::{c_char, c_int, c_void};
    use std::ffi::CStr;
    use std::io::Write;
    use std::str;

    use ffi;
    use SqliteConnection;

    extern "C" fn log_callback(_: *mut c_void, err: c_int, msg: *const c_char) {
{'fraction_non_alphanumeric': 0.13126783530370975, 'fraction_numerical': 0.0028536485935589076, 'mean_word_length': 3.285589519650655, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5779308', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1618, 'n_words': 528}