Add functionality to initialize a logger from ruby
This commit is contained in:
parent
4fde23bc1b
commit
fc6cf0c709
4 changed files with 74 additions and 8 deletions
|
@ -20,6 +20,7 @@ crate-type = ["dylib"]
|
|||
ruru = "0.9"
|
||||
lazy_static = "0.2"
|
||||
log = "0.3"
|
||||
env_logger = "0.3"
|
||||
toml = "0.2"
|
||||
|
||||
[dependencies.libimagerror]
|
||||
|
|
|
@ -20,7 +20,9 @@
|
|||
|
||||
use std::error::Error;
|
||||
|
||||
use ruru::{Class, RString, NilClass, VM, Object};
|
||||
use ruru::{Class, Boolean, RString, NilClass, VM, Object};
|
||||
|
||||
use libimagrt::logger::ImagLogger;
|
||||
|
||||
class!(Imag);
|
||||
|
||||
|
@ -28,6 +30,65 @@ methods!(
|
|||
Imag,
|
||||
itself,
|
||||
|
||||
fn r_initialize_logger(debug: Boolean, verbose: Boolean, colored: Boolean) -> NilClass {
|
||||
use std::env::var as env_var;
|
||||
use env_logger;
|
||||
use log;
|
||||
use log::{Log, LogLevel, LogRecord, LogMetadata};
|
||||
use log::LogLevelFilter;
|
||||
|
||||
let debug = match debug {
|
||||
Ok(d) => d.to_bool(),
|
||||
Err(ref e) => {
|
||||
VM::raise(e.to_exception(), e.description());
|
||||
return NilClass::new();
|
||||
},
|
||||
};
|
||||
|
||||
let verbose = match verbose {
|
||||
Ok(v) => v.to_bool(),
|
||||
Err(ref e) => {
|
||||
VM::raise(e.to_exception(), e.description());
|
||||
return NilClass::new();
|
||||
},
|
||||
};
|
||||
|
||||
let colored = match colored {
|
||||
Ok(c) => c.to_bool(),
|
||||
Err(ref e) => {
|
||||
VM::raise(e.to_exception(), e.description());
|
||||
return NilClass::new();
|
||||
},
|
||||
};
|
||||
|
||||
if env_var("IMAG_LOG_ENV").is_ok() {
|
||||
env_logger::init().unwrap();
|
||||
} else {
|
||||
let lvl = if debug {
|
||||
LogLevelFilter::Debug
|
||||
} else if verbose {
|
||||
LogLevelFilter::Info
|
||||
} else {
|
||||
LogLevelFilter::Warn
|
||||
};
|
||||
|
||||
log::set_logger(|max_log_lvl| {
|
||||
max_log_lvl.set(lvl);
|
||||
debug!("Init logger with {}", lvl);
|
||||
let lgr = ImagLogger::new(lvl.to_log_level().unwrap())
|
||||
.with_color(colored)
|
||||
.with_prefix("[imag][ruby]".to_owned());
|
||||
Box::new(lgr)
|
||||
})
|
||||
.map_err(|_| {
|
||||
panic!("Could not setup logger");
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
|
||||
NilClass::new()
|
||||
}
|
||||
|
||||
fn r_log_trace(l: RString) -> NilClass {
|
||||
match l {
|
||||
Err(ref e) => VM::raise(e.to_exception(), e.description()),
|
||||
|
@ -73,12 +134,13 @@ methods!(
|
|||
pub fn setup() -> Class {
|
||||
let mut class = Class::new("Imag", None);
|
||||
class.define(|itself| {
|
||||
itself.def_self("trace", r_log_trace);
|
||||
itself.def_self("dbg", r_log_debug);
|
||||
itself.def_self("debug", r_log_debug);
|
||||
itself.def_self("info", r_log_info);
|
||||
itself.def_self("warn", r_log_warn);
|
||||
itself.def_self("error", r_log_error);
|
||||
itself.def_self("init_logger", r_initialize_logger);
|
||||
itself.def_self("trace", r_log_trace);
|
||||
itself.def_self("dbg", r_log_debug);
|
||||
itself.def_self("debug", r_log_debug);
|
||||
itself.def_self("info", r_log_info);
|
||||
itself.def_self("warn", r_log_warn);
|
||||
itself.def_self("error", r_log_error);
|
||||
});
|
||||
class
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#[macro_use] extern crate ruru;
|
||||
#[macro_use] extern crate lazy_static;
|
||||
#[macro_use] extern crate log;
|
||||
extern crate env_logger;
|
||||
extern crate toml;
|
||||
|
||||
#[macro_use] extern crate libimagerror;
|
||||
|
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
require 'fiddle'
|
||||
|
||||
lib = Fiddle::dlopen '../target/release/liblibimagruby.so'
|
||||
lib = Fiddle::dlopen '../target/debug/liblibimagruby.so'
|
||||
Fiddle::Function::new(lib['imag_ruby_initialize'], [], Fiddle::TYPE_VOIDP).call
|
||||
|
||||
works = (not RStoreId.new_baseless("test").nil?)
|
||||
Imag.init_logger true, true, true
|
||||
Imag.info "Hello from Ruby"
|
||||
|
||||
puts "Works: #{works}"
|
||||
|
||||
|
|
Loading…
Reference in a new issue