logging: Move all facilities to one file

This commit is contained in:
Dorota Czaplejewicz
2019-12-16 19:15:40 +00:00
parent 1237537005
commit e2b89e85f7
6 changed files with 50 additions and 42 deletions

View File

@ -19,17 +19,17 @@ use ::keyboard::{
}; };
use ::layout; use ::layout;
use ::layout::ArrangementKind; use ::layout::ArrangementKind;
use ::logging::PrintWarnings;
use ::resources; use ::resources;
use ::util::c::as_str; use ::util::c::as_str;
use ::util::hash_map_map; use ::util::hash_map_map;
use ::xdg; use ::xdg;
// traits, derives // traits, derives
use serde::Deserialize;
use std::io::BufReader; use std::io::BufReader;
use std::iter::FromIterator; use std::iter::FromIterator;
use serde::Deserialize; use ::logging::WarningHandler;
use util::WarningHandler;
/// Gathers stuff defined in C or called by C /// Gathers stuff defined in C or called by C
pub mod c { pub mod c {
@ -152,14 +152,6 @@ fn list_layout_sources(
ret ret
} }
struct PrintWarnings;
impl WarningHandler for PrintWarnings {
fn handle(&mut self, warning: &str) {
println!("{}", warning);
}
}
fn load_layout_data(source: DataSource) fn load_layout_data(source: DataSource)
-> Result<::layout::LayoutData, LoadError> -> Result<::layout::LayoutData, LoadError>
{ {
@ -672,14 +664,7 @@ mod tests {
use super::*; use super::*;
use std::error::Error as ErrorTrait; use std::error::Error as ErrorTrait;
use ::logging::PanicWarn;
struct PanicWarn;
impl WarningHandler for PanicWarn {
fn handle(&mut self, warning: &str) {
panic!("{}", warning);
}
}
#[test] #[test]
fn test_parse_path() { fn test_parse_path() {

View File

@ -24,6 +24,7 @@ mod keyboard;
mod layout; mod layout;
mod locale; mod locale;
mod locale_config; mod locale_config;
mod logging;
mod outputs; mod outputs;
mod popover; mod popover;
mod resources; mod resources;

43
src/logging.rs Normal file
View File

@ -0,0 +1,43 @@
/*! Logging library */
use std::error::Error;
/// Sugar for logging errors in results
pub trait Warn {
type Value;
fn ok_warn(self, msg: &str) -> Option<Self::Value>;
}
impl<T, E: Error> Warn for Result<T, E> {
type Value = T;
fn ok_warn(self, msg: &str) -> Option<T> {
self.map_err(|e| {
eprintln!("{}: {}", msg, e);
e
}).ok()
}
}
pub trait WarningHandler {
/// Handle a warning
fn handle(&mut self, warning: &str);
}
/// Prints warnings to stderr
pub struct PrintWarnings;
impl WarningHandler for PrintWarnings {
fn handle(&mut self, warning: &str) {
eprintln!("{}", warning);
}
}
/// Warning handler that will panic at any warning.
/// Don't use except in tests
pub struct PanicWarn;
impl WarningHandler for PanicWarn {
fn handle(&mut self, warning: &str) {
panic!("{}", warning);
}
}

View File

@ -21,7 +21,7 @@
use std::env; use std::env;
use glib::object::ObjectExt; use glib::object::ObjectExt;
use util::Warn; use logging::Warn;
/// Gathers stuff defined in C or called by C /// Gathers stuff defined in C or called by C
pub mod c { pub mod c {

View File

@ -3,7 +3,7 @@
use ::data::Layout; use ::data::Layout;
use xkbcommon::xkb; use xkbcommon::xkb;
use ::util::WarningHandler; use ::logging::WarningHandler;
pub struct CountAndPrint(u32); pub struct CountAndPrint(u32);

View File

@ -189,27 +189,6 @@ impl<T> Borrow<Rc<T>> for Pointer<T> {
} }
} }
/// Sugar for logging errors in results
pub trait Warn {
type Value;
fn ok_warn(self, msg: &str) -> Option<Self::Value>;
}
impl<T, E: std::error::Error> Warn for Result<T, E> {
type Value = T;
fn ok_warn(self, msg: &str) -> Option<T> {
self.map_err(|e| {
eprintln!("{}: {}", msg, e);
e
}).ok()
}
}
pub trait WarningHandler {
/// Handle a warning
fn handle(&mut self, warning: &str);
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;