Added support in CInitOptions to pass logging/filter modules to VSLogger.

This commit is contained in:
angelortiz1007 2019-08-29 13:46:56 -05:00
parent 068997cd30
commit 49934c0335
4 changed files with 53 additions and 7 deletions

View file

@ -15,8 +15,8 @@ bench = false
[dependencies]
simpleservo = { path = "../api" }
log = "0.4"
env_logger = "0.6"
lazy_static = "1"
env_logger = "0.6"
backtrace = "0.3"
[target.'cfg(target_os = "windows")'.dependencies]

View file

@ -21,6 +21,7 @@ use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::{c_char, c_void};
use std::panic::{self, UnwindSafe};
use std::slice;
use std::sync::RwLock;
extern "C" fn default_panic_handler(msg: *const c_char) {
@ -222,6 +223,8 @@ pub struct CInitOptions {
pub density: f32,
pub vr_pointer: *mut c_void,
pub enable_subpixel_text_antialiasing: bool,
pub vslogger_mod_list: *const *const c_char,
pub vslogger_mod_size: u32,
}
/// The returned string is not freed. This will leak.
@ -231,13 +234,22 @@ pub extern "C" fn servo_version() -> *const c_char {
}
#[cfg(target_os = "windows")]
fn init_logger() {
fn init_logger(modules: &[*const c_char]) {
use crate::vslogger::LOG_MODULE_FILTERS;
use log::LevelFilter;
use std::sync::Once;
use vslogger::VSLogger;
static LOGGER: VSLogger = VSLogger;
static LOGGER_INIT: Once = Once::new();
if !modules.is_empty() {
*LOG_MODULE_FILTERS.lock().unwrap() = modules
.iter()
.map(|modules| unsafe { CStr::from_ptr(*modules).to_string_lossy().into_owned() })
.collect::<Vec<_>>();
}
LOGGER_INIT.call_once(|| {
log::set_logger(&LOGGER)
.map(|_| log::set_max_level(LevelFilter::Debug))
@ -246,7 +258,7 @@ fn init_logger() {
}
#[cfg(not(target_os = "windows"))]
fn init_logger() {
fn init_logger(_modules: &[*const c_char]) {
crate::env_logger::init();
}
@ -258,7 +270,13 @@ unsafe fn init(
wakeup: extern "C" fn(),
callbacks: CHostCallbacks,
) {
init_logger();
let logger_modules = if opts.vslogger_mod_list.is_null() {
&[]
} else {
slice::from_raw_parts(opts.vslogger_mod_list, opts.vslogger_mod_size as usize)
};
init_logger(logger_modules);
if let Err(reason) = redirect_stdout_stderr() {
warn!("Error redirecting stdout/stderr: {}", reason);
@ -310,7 +328,6 @@ pub extern "C" fn init_with_egl(
callbacks: CHostCallbacks,
) {
catch_any_panic(|| {
init_logger();
let gl = gl_glue::egl::init().unwrap();
unsafe {
init(
@ -333,7 +350,6 @@ pub extern "C" fn init_with_gl(
callbacks: CHostCallbacks,
) {
catch_any_panic(|| {
init_logger();
let gl = gl_glue::gl::init().unwrap();
unsafe { init(opts, gl, None, None, wakeup, callbacks) }
});

View file

@ -3,6 +3,11 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use log::{self, Level, Metadata, Record};
use std::sync::{Arc, Mutex};
lazy_static! {
pub static ref LOG_MODULE_FILTERS: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(vec![]));
}
extern "C" {
fn OutputDebugStringA(s: *const u8);
@ -12,7 +17,10 @@ pub struct VSLogger;
impl log::Log for VSLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Warn
let modules = LOG_MODULE_FILTERS.lock().unwrap();
let is_module_enabled =
modules.contains(&String::from(metadata.target())) || modules.is_empty();
return metadata.level() <= Level::Warn && is_module_enabled;
}
fn log(&self, record: &Record) {

View file

@ -69,6 +69,28 @@ Servo::Servo(hstring url, GLsizei width, GLsizei height, float dpi,
o.enable_subpixel_text_antialiasing = false;
o.vr_pointer = NULL;
// 7 filter modules.
/* Sample list of servo modules to filter.
static char *pfilters[] = {
"servo",
"simpleservo",
"simpleservo::jniapi",
"simpleservo::gl_glue::egl",
// Show JS errors by default.
"script::dom::bindings::error",
// Show GL errors by default.
"canvas::webgl_thread",
"compositing::compositor",
"constellation::constellation",
};
*/
// Example Call when *pfilters[] is used:
// o.vslogger_mod_list = pfilters; // servo log modules
// o.vslogger_mod_size = sizeof(pfilters) / sizeof(pfilters[0]) -1; // Important: Number of modules in pfilters
o.vslogger_mod_list = NULL;
o.vslogger_mod_size = 0;
sServo = this; // FIXME;
capi::CHostCallbacks c;