Add log handling

pull/4/head
Thijs Cadier 9 years ago
parent 55a0add371
commit eb1888b00c

@ -13,6 +13,7 @@ name = "tests"
[dependencies]
libc = "*"
log = "*"
[dependencies.bson]
git = "https://github.com/zonyitoo/bson-rs.git"

@ -20,6 +20,12 @@ set this environment var for building to work (see https://github.com/crabtw/rus
export DYLD_LIBRARY_PATH=/Library/Developer/CommandLineTools/usr/lib
```
## Logging
All internal logging by mongoc is redirected to the macros in the [log
crate](http://doc.rust-lang.org/log/log/index.html). See the `log` docs
to configure output in your application.
## Examples
See the tests directory for examples of how to use the driver.

@ -1,4 +1,5 @@
#![feature(convert)]
#![feature(cstr_to_str)]
extern crate libc;
extern crate mongoc_sys as mongoc;
@ -6,6 +7,11 @@ extern crate mongoc_sys as mongoc;
#[macro_use]
extern crate bson;
#[macro_use]
extern crate log;
use std::ffi::CStr;
use std::ptr;
use std::result;
use std::sync::{Once,ONCE_INIT};
@ -34,10 +40,41 @@ static MONGOC_INIT: Once = ONCE_INIT;
/// anything else.
fn init() {
MONGOC_INIT.call_once(|| {
unsafe { bindings::mongoc_init(); }
unsafe {
// Init mongoc subsystem
bindings::mongoc_init();
// Set mongoc log handler
bindings::mongoc_log_set_handler(
Some(mongoc_log_handler),
ptr::null_mut()
);
}
});
}
extern fn mongoc_log_handler(
log_level: bindings::mongoc_log_level_t,
log_domain: *const ::libc::c_char,
message: *const ::libc::c_char,
_: *mut ::libc::c_void
) {
let log_domain_str = unsafe { CStr::from_ptr(log_domain).to_string_lossy() };
let message_str = unsafe { CStr::from_ptr(message).to_string_lossy() };
let log_line = format!("mongoc: {} - {}", log_domain_str, message_str);
match log_level {
bindings::MONGOC_LOG_LEVEL_ERROR => error!("{}", log_line),
bindings::MONGOC_LOG_LEVEL_CRITICAL => error!("{}", log_line),
bindings::MONGOC_LOG_LEVEL_WARNING => warn!("{}", log_line),
bindings::MONGOC_LOG_LEVEL_MESSAGE => info!("{}", log_line),
bindings::MONGOC_LOG_LEVEL_INFO => info!("{}", log_line),
bindings::MONGOC_LOG_LEVEL_DEBUG => debug!("{}", log_line),
bindings::MONGOC_LOG_LEVEL_TRACE => trace!("{}", log_line),
_ => panic!("Unknown mongoc log level")
}
}
pub struct CommandAndFindOptions {
pub query_flags: flags::Flags<flags::QueryFlag>,
pub skip: u32,

Loading…
Cancel
Save