From 5a449ed1c783f3ad061d409aef8f065179ed739f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 20 Jan 2017 18:41:00 +0100 Subject: [PATCH] Add ruby->imag logging functionality --- libimagruby/src/imag.rs | 85 +++++++++++++++++++++++++++++++++++++++++ libimagruby/src/lib.rs | 1 + 2 files changed, 86 insertions(+) create mode 100644 libimagruby/src/imag.rs diff --git a/libimagruby/src/imag.rs b/libimagruby/src/imag.rs new file mode 100644 index 00000000..2e7f8c27 --- /dev/null +++ b/libimagruby/src/imag.rs @@ -0,0 +1,85 @@ +// +// imag - the personal information management suite for the commandline +// Copyright (C) 2015, 2016 Matthias Beyer and contributors +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; version +// 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// + + +use std::error::Error; + +use ruru::{Class, RString, NilClass, VM, Object}; + +class!(Imag); + +methods!( + Imag, + itself, + + fn r_log_trace(l: RString) -> NilClass { + match l { + Err(ref e) => VM::raise(e.to_exception(), e.description()), + Ok(s) => trace!("{}", s.to_string()), + } + NilClass::new() + } + + fn r_log_debug(l: RString) -> NilClass { + match l { + Err(ref e) => VM::raise(e.to_exception(), e.description()), + Ok(s) => debug!("{}", s.to_string()), + } + NilClass::new() + } + + fn r_log_info(l: RString) -> NilClass { + match l { + Err(ref e) => VM::raise(e.to_exception(), e.description()), + Ok(s) => info!("{}", s.to_string()), + } + NilClass::new() + } + + fn r_log_warn(l: RString) -> NilClass { + match l { + Err(ref e) => VM::raise(e.to_exception(), e.description()), + Ok(s) => warn!("{}", s.to_string()), + } + NilClass::new() + } + + fn r_log_error(l: RString) -> NilClass { + match l { + Err(ref e) => VM::raise(e.to_exception(), e.description()), + Ok(s) => error!("{}", s.to_string()), + } + NilClass::new() + } + +); + +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); + }); + class +} + diff --git a/libimagruby/src/lib.rs b/libimagruby/src/lib.rs index 49ee78c4..6d25bbb0 100644 --- a/libimagruby/src/lib.rs +++ b/libimagruby/src/lib.rs @@ -27,6 +27,7 @@ extern crate libimagstore; extern crate libimagrt; #[macro_use] extern crate libimagutil; +pub mod imag; pub mod store; pub mod toml_utils; pub mod ruby_utils;