• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Tracing and profiling functions. Error and warning log.
2 
3 use std::ffi::{CStr, CString};
4 use std::mem;
5 use std::os::raw::{c_char, c_int, c_void};
6 use std::panic::catch_unwind;
7 use std::ptr;
8 use std::time::Duration;
9 
10 use super::ffi;
11 use crate::error::error_from_sqlite_code;
12 use crate::{Connection, Result};
13 
14 /// Set up the process-wide SQLite error logging callback.
15 ///
16 /// # Safety
17 ///
18 /// This function is marked unsafe for two reasons:
19 ///
20 /// * The function is not threadsafe. No other SQLite calls may be made while
21 ///   `config_log` is running, and multiple threads may not call `config_log`
22 ///   simultaneously.
23 /// * The provided `callback` itself function has two requirements:
24 ///     * It must not invoke any SQLite calls.
25 ///     * It must be threadsafe if SQLite is used in a multithreaded way.
26 ///
27 /// cf [The Error And Warning Log](http://sqlite.org/errlog.html).
config_log(callback: Option<fn(c_int, &str)>) -> Result<()>28 pub unsafe fn config_log(callback: Option<fn(c_int, &str)>) -> Result<()> {
29     extern "C" fn log_callback(p_arg: *mut c_void, err: c_int, msg: *const c_char) {
30         let c_slice = unsafe { CStr::from_ptr(msg).to_bytes() };
31         let callback: fn(c_int, &str) = unsafe { mem::transmute(p_arg) };
32 
33         let s = String::from_utf8_lossy(c_slice);
34         drop(catch_unwind(|| callback(err, &s)));
35     }
36 
37     let rc = if let Some(f) = callback {
38         ffi::sqlite3_config(
39             ffi::SQLITE_CONFIG_LOG,
40             log_callback as extern "C" fn(_, _, _),
41             f as *mut c_void,
42         )
43     } else {
44         let nullptr: *mut c_void = ptr::null_mut();
45         ffi::sqlite3_config(ffi::SQLITE_CONFIG_LOG, nullptr, nullptr)
46     };
47 
48     if rc == ffi::SQLITE_OK {
49         Ok(())
50     } else {
51         Err(error_from_sqlite_code(rc, None))
52     }
53 }
54 
55 /// Write a message into the error log established by
56 /// `config_log`.
57 #[inline]
log(err_code: c_int, msg: &str)58 pub fn log(err_code: c_int, msg: &str) {
59     let msg = CString::new(msg).expect("SQLite log messages cannot contain embedded zeroes");
60     unsafe {
61         ffi::sqlite3_log(err_code, b"%s\0" as *const _ as *const c_char, msg.as_ptr());
62     }
63 }
64 
65 impl Connection {
66     /// Register or clear a callback function that can be
67     /// used for tracing the execution of SQL statements.
68     ///
69     /// Prepared statement placeholders are replaced/logged with their assigned
70     /// values. There can only be a single tracer defined for each database
71     /// connection. Setting a new tracer clears the old one.
trace(&mut self, trace_fn: Option<fn(&str)>)72     pub fn trace(&mut self, trace_fn: Option<fn(&str)>) {
73         unsafe extern "C" fn trace_callback(p_arg: *mut c_void, z_sql: *const c_char) {
74             let trace_fn: fn(&str) = mem::transmute(p_arg);
75             let c_slice = CStr::from_ptr(z_sql).to_bytes();
76             let s = String::from_utf8_lossy(c_slice);
77             drop(catch_unwind(|| trace_fn(&s)));
78         }
79 
80         let c = self.db.borrow_mut();
81         match trace_fn {
82             Some(f) => unsafe {
83                 ffi::sqlite3_trace(c.db(), Some(trace_callback), f as *mut c_void);
84             },
85             None => unsafe {
86                 ffi::sqlite3_trace(c.db(), None, ptr::null_mut());
87             },
88         }
89     }
90 
91     /// Register or clear a callback function that can be
92     /// used for profiling the execution of SQL statements.
93     ///
94     /// There can only be a single profiler defined for each database
95     /// connection. Setting a new profiler clears the old one.
profile(&mut self, profile_fn: Option<fn(&str, Duration)>)96     pub fn profile(&mut self, profile_fn: Option<fn(&str, Duration)>) {
97         unsafe extern "C" fn profile_callback(
98             p_arg: *mut c_void,
99             z_sql: *const c_char,
100             nanoseconds: u64,
101         ) {
102             let profile_fn: fn(&str, Duration) = mem::transmute(p_arg);
103             let c_slice = CStr::from_ptr(z_sql).to_bytes();
104             let s = String::from_utf8_lossy(c_slice);
105             const NANOS_PER_SEC: u64 = 1_000_000_000;
106 
107             let duration = Duration::new(
108                 nanoseconds / NANOS_PER_SEC,
109                 (nanoseconds % NANOS_PER_SEC) as u32,
110             );
111             drop(catch_unwind(|| profile_fn(&s, duration)));
112         }
113 
114         let c = self.db.borrow_mut();
115         match profile_fn {
116             Some(f) => unsafe {
117                 ffi::sqlite3_profile(c.db(), Some(profile_callback), f as *mut c_void)
118             },
119             None => unsafe { ffi::sqlite3_profile(c.db(), None, ptr::null_mut()) },
120         };
121     }
122 }
123 
124 #[cfg(test)]
125 mod test {
126     use lazy_static::lazy_static;
127     use std::sync::Mutex;
128     use std::time::Duration;
129 
130     use crate::{Connection, Result};
131 
132     #[test]
test_trace() -> Result<()>133     fn test_trace() -> Result<()> {
134         lazy_static! {
135             static ref TRACED_STMTS: Mutex<Vec<String>> = Mutex::new(Vec::new());
136         }
137         fn tracer(s: &str) {
138             let mut traced_stmts = TRACED_STMTS.lock().unwrap();
139             traced_stmts.push(s.to_owned());
140         }
141 
142         let mut db = Connection::open_in_memory()?;
143         db.trace(Some(tracer));
144         {
145             let _ = db.query_row("SELECT ?", [1i32], |_| Ok(()));
146             let _ = db.query_row("SELECT ?", ["hello"], |_| Ok(()));
147         }
148         db.trace(None);
149         {
150             let _ = db.query_row("SELECT ?", [2i32], |_| Ok(()));
151             let _ = db.query_row("SELECT ?", ["goodbye"], |_| Ok(()));
152         }
153 
154         let traced_stmts = TRACED_STMTS.lock().unwrap();
155         assert_eq!(traced_stmts.len(), 2);
156         assert_eq!(traced_stmts[0], "SELECT 1");
157         assert_eq!(traced_stmts[1], "SELECT 'hello'");
158         Ok(())
159     }
160 
161     #[test]
test_profile() -> Result<()>162     fn test_profile() -> Result<()> {
163         lazy_static! {
164             static ref PROFILED: Mutex<Vec<(String, Duration)>> = Mutex::new(Vec::new());
165         }
166         fn profiler(s: &str, d: Duration) {
167             let mut profiled = PROFILED.lock().unwrap();
168             profiled.push((s.to_owned(), d));
169         }
170 
171         let mut db = Connection::open_in_memory()?;
172         db.profile(Some(profiler));
173         db.execute_batch("PRAGMA application_id = 1")?;
174         db.profile(None);
175         db.execute_batch("PRAGMA application_id = 2")?;
176 
177         let profiled = PROFILED.lock().unwrap();
178         assert_eq!(profiled.len(), 1);
179         assert_eq!(profiled[0].0, "PRAGMA application_id = 1");
180         Ok(())
181     }
182 }
183