Skip to content

Commit

Permalink
implement Visual Studio logs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Jul 3, 2019
1 parent 661170c commit 0d96bbd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
28 changes: 27 additions & 1 deletion ports/libsimpleservo/capi/src/lib.rs
Expand Up @@ -5,6 +5,10 @@
#[macro_use]
extern crate log;

#[cfg(target_os = "windows")]
mod vslogger;

#[cfg(not(target_os = "windows"))]
use env_logger;
use simpleservo::{self, gl_glue, ServoGlue, SERVO};
use simpleservo::{Coordinates, EventLoopWaker, HostTrait, InitOptions, VRInitOptions};
Expand Down Expand Up @@ -67,13 +71,33 @@ pub extern "C" fn servo_version() -> *const c_char {
ptr
}

#[cfg(target_os = "windows")]
fn init_logger() {
use log::LevelFilter;
use std::sync::Once;
use vslogger::VSLogger;

static LOGGER: VSLogger = VSLogger;
static LOGGER_INIT: Once = Once::new();
LOGGER_INIT.call_once(|| {
log::set_logger(&LOGGER)
.map(|_| log::set_max_level(LevelFilter::Debug))
.unwrap();
});
}

#[cfg(not(target_os = "windows"))]
fn init_logger() {
crate::env_logger::init();
}

fn init(
opts: CInitOptions,
gl: gl_glue::ServoGl,
wakeup: extern "C" fn(),
callbacks: CHostCallbacks,
) {
crate::env_logger::init();
init_logger();

let args = if !opts.args.is_null() {
let args = unsafe { CStr::from_ptr(opts.args) };
Expand Down Expand Up @@ -117,6 +141,7 @@ pub extern "C" fn init_with_egl(
wakeup: extern "C" fn(),
callbacks: CHostCallbacks,
) {
init_logger();
let gl = gl_glue::egl::init().unwrap();
init(opts, gl, wakeup, callbacks)
}
Expand All @@ -128,6 +153,7 @@ pub extern "C" fn init_with_gl(
wakeup: extern "C" fn(),
callbacks: CHostCallbacks,
) {
init_logger();
let gl = gl_glue::gl::init().unwrap();
init(opts, gl, wakeup, callbacks)
}
Expand Down
28 changes: 28 additions & 0 deletions ports/libsimpleservo/capi/src/vslogger.rs
@@ -0,0 +1,28 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use log::{self, Level, Metadata, Record};

extern "C" {
fn OutputDebugStringA(s: *const u8);
}

pub struct VSLogger;

impl log::Log for VSLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Warn
}

fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let log = format!("RUST: {} - {}\r\n\0", record.level(), record.args());
unsafe {
OutputDebugStringA(log.as_ptr());
};
}
}

fn flush(&self) {}
}

0 comments on commit 0d96bbd

Please sign in to comment.