logging: Move all facilities to one file
This commit is contained in:
		
							
								
								
									
										23
									
								
								src/data.rs
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								src/data.rs
									
									
									
									
									
								
							@ -19,17 +19,17 @@ use ::keyboard::{
 | 
			
		||||
};
 | 
			
		||||
use ::layout;
 | 
			
		||||
use ::layout::ArrangementKind;
 | 
			
		||||
use ::logging::PrintWarnings;
 | 
			
		||||
use ::resources;
 | 
			
		||||
use ::util::c::as_str;
 | 
			
		||||
use ::util::hash_map_map;
 | 
			
		||||
use ::xdg;
 | 
			
		||||
 | 
			
		||||
// traits, derives
 | 
			
		||||
use serde::Deserialize;
 | 
			
		||||
use std::io::BufReader;
 | 
			
		||||
use std::iter::FromIterator;
 | 
			
		||||
use serde::Deserialize;
 | 
			
		||||
use util::WarningHandler;
 | 
			
		||||
 | 
			
		||||
use ::logging::WarningHandler;
 | 
			
		||||
 | 
			
		||||
/// Gathers stuff defined in C or called by C
 | 
			
		||||
pub mod c {
 | 
			
		||||
@ -152,14 +152,6 @@ fn list_layout_sources(
 | 
			
		||||
    ret
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct PrintWarnings;
 | 
			
		||||
 | 
			
		||||
impl WarningHandler for PrintWarnings {
 | 
			
		||||
    fn handle(&mut self, warning: &str) {
 | 
			
		||||
        println!("{}", warning);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn load_layout_data(source: DataSource)
 | 
			
		||||
    -> Result<::layout::LayoutData, LoadError>
 | 
			
		||||
{
 | 
			
		||||
@ -672,14 +664,7 @@ mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
    
 | 
			
		||||
    use std::error::Error as ErrorTrait;
 | 
			
		||||
 | 
			
		||||
    struct PanicWarn;
 | 
			
		||||
    
 | 
			
		||||
    impl WarningHandler for PanicWarn {
 | 
			
		||||
        fn handle(&mut self, warning: &str) {
 | 
			
		||||
            panic!("{}", warning);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    use ::logging::PanicWarn;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_parse_path() {
 | 
			
		||||
 | 
			
		||||
@ -24,6 +24,7 @@ mod keyboard;
 | 
			
		||||
mod layout;
 | 
			
		||||
mod locale;
 | 
			
		||||
mod locale_config;
 | 
			
		||||
mod logging;
 | 
			
		||||
mod outputs;
 | 
			
		||||
mod popover;
 | 
			
		||||
mod resources;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										43
									
								
								src/logging.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/logging.rs
									
									
									
									
									
										Normal 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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -21,7 +21,7 @@
 | 
			
		||||
use std::env;
 | 
			
		||||
 | 
			
		||||
use glib::object::ObjectExt;
 | 
			
		||||
use util::Warn;
 | 
			
		||||
use logging::Warn;
 | 
			
		||||
 | 
			
		||||
/// Gathers stuff defined in C or called by C
 | 
			
		||||
pub mod c {
 | 
			
		||||
 | 
			
		||||
@ -3,7 +3,7 @@
 | 
			
		||||
use ::data::Layout;
 | 
			
		||||
use xkbcommon::xkb;
 | 
			
		||||
 | 
			
		||||
use ::util::WarningHandler;
 | 
			
		||||
use ::logging::WarningHandler;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
pub struct CountAndPrint(u32);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								src/util.rs
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/util.rs
									
									
									
									
									
								
							@ -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)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user