Change config_log to take a Rust fn instead of an extern "C" fn.

Moves the unit test for config_log out of #[ignore] and into its own
test file since it affects the entire process.
This commit is contained in:
John Gallagher
2015-11-30 15:29:50 -05:00
parent eaf080261b
commit dbfa6ca31f
3 changed files with 76 additions and 30 deletions

View File

@@ -1,31 +1,55 @@
//! Tracing and profiling functions. Error and warning log.
use libc::{c_char, c_int, c_void};
use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::ptr;
use std::str;
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
/// Set up the process-wide SQLite error logging callback.
/// This function is marked unsafe for two reasons:
///
/// * The function is not threadsafe. No other SQLite calls may be made while
/// `config_log` is running, and multiple threads may not call `config_log`
/// simultaneously.
/// * The provided `callback` itself function has two requirements:
/// * It must not invoke any SQLite calls.
/// * It must be threadsafe if SQLite is used in a multithreaded way.
///
/// 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)
pub unsafe fn config_log(callback: Option<fn(c_int, &str)>) -> SqliteResult<()> {
extern "C" fn log_callback(p_arg: *mut c_void, err: c_int, msg: *const c_char) {
let c_slice = unsafe { CStr::from_ptr(msg).to_bytes() };
let callback: fn(c_int, &str) = unsafe { mem::transmute(p_arg) };
if let Ok(s) = str::from_utf8(c_slice) {
callback(err, s);
}
}
let rc = match callback {
Some(f) => {
let p_arg: *mut c_void = mem::transmute(f);
ffi::sqlite3_config(ffi::SQLITE_CONFIG_LOG, Some(log_callback), p_arg)
},
None => {
let nullptr: *mut c_void = ptr::null_mut();
ffi::sqlite3_config(ffi::SQLITE_CONFIG_LOG, nullptr, nullptr)
}
};
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();
let msg = CString::new(msg).expect("SQLite log messages cannot contain embedded zeroes");
unsafe {
ffi::sqlite3_log(err_code, msg.as_ptr());
}
@@ -60,35 +84,16 @@ impl SqliteConnection {
#[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) {
unsafe {
let c_slice = CStr::from_ptr(msg).to_bytes();
let _ = writeln!(::std::io::stderr(), "{}: {:?}", err, str::from_utf8(c_slice));
}
}
#[test] #[ignore] // To avoid freezing tests
fn test_log() {
unsafe { ffi::sqlite3_shutdown() };
super::config_log(Some(log_callback)).unwrap();
//super::log(ffi::SQLITE_NOTICE, "message from rusqlite");
super::config_log(None).unwrap();
}
extern "C" fn profile_callback(_: *mut ::libc::c_void, sql: *const ::libc::c_char, nanoseconds: u64) {
use std::time::Duration;
unsafe {
let c_slice = ::std::ffi::CStr::from_ptr(sql).to_bytes();
let d = Duration::from_millis(nanoseconds / 1_000_000);
let _ = writeln!(::std::io::stderr(), "PROFILE: {:?} ({})", ::std::str::from_utf8(c_slice), d);
let _ = writeln!(::std::io::stderr(), "PROFILE: {:?} ({:?})", ::std::str::from_utf8(c_slice), d);
}
}