downcasting log hooks

master
Avril 4 years ago
parent 521d281743
commit 740967756b
Signed by: flanchan
GPG Key ID: 284488987C31F630

7
Cargo.lock generated

@ -395,6 +395,7 @@ dependencies = [
"generational-arena",
"lazy_static",
"libc",
"mopa",
"notify",
"once_cell",
"recolored",
@ -461,6 +462,12 @@ dependencies = [
"ws2_32-sys",
]
[[package]]
name = "mopa"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a785740271256c230f57462d3b83e52f998433a7062fc18f96d5999474a9f915"
[[package]]
name = "net2"
version = "0.2.34"

@ -40,6 +40,7 @@ chrono = "0.4"
libc = "0.2"
cfg-if = "0.1"
generational-arena = "0.2"
mopa = "0.2"
[build-dependencies]
rustc_version = "0.2"

@ -1,4 +1,3 @@
extern crate rustc_version;
use rustc_version::{version, version_meta, Channel};

@ -1,11 +1,12 @@
//! Extra logging types for log files and such
use super::*;
use generational_arena::Index;
use mopa::*;
use std::marker::{Sync, Send};
/// A logging hook
pub trait Hook: Sync + Send
pub trait Hook: Sync + Send + Any
{
/// The name of this hook object
fn name(&self) -> &str {"(unnamed)"}
@ -16,6 +17,7 @@ pub trait Hook: Sync + Send
/// Fired when hook is removed
fn finalise(&mut self, _idx: Index) {}
}
mopafy!(Hook);
impl std::fmt::Debug for dyn Hook
{

@ -97,7 +97,7 @@ impl Logger
idx
}
/// Try to remove and return a logging hook.
/// Try to remove and return the removed logging hook.
///
/// # Returns
/// `None` if `hook` is not present or its `RwLock` was poisoned, otherwise, returns boxed trait object of the hook.
@ -114,6 +114,53 @@ impl Logger
.flatten()
}
/// Try to remove, downcast, and return the removed logging hook. Assume the downcasting will not fail.
///
/// # Returns
/// `None` if [remove_hook]`remove_hook` returns `None`, `Some(H)` if it is removed successfully **and also** downcasted successfully.
///
/// Function will also return `None` if removal was successful but downcasting was not.
///
/// If this it not desireable, check [remove_hook_try_downcasted]`remove_hook_try_downcasted`.
pub fn remove_hook_assume_downcasted<H: custom::Hook, I: Into<Index>>(&self, hook: I) -> Option<H>
{
if let Some(bx) = self.remove_hook(hook) {
bx.downcast().ok().map(|x| *x)
} else {
None
}
}
/// Remove, downcast, and return the removed logging hook.
///
/// # Returns
/// `None` if [remove_hook]`remove_hook` returns `None`, `Some(H)` if it is removed successfully **and also** downcasted successfully.
///
/// # Panics
///
/// If downcasting fails
pub fn remove_hook_downcasted<H: custom::Hook, I: Into<Index>>(&self, hook: I) -> Option<H>
{
if let Some(bx) = self.remove_hook(hook) {
Some(*bx.downcast().expect("Downcast failed on removed hook"))
} else {
None
}
}
/// Try to remove, downcast, and return the removed logging hook.
///
/// # Returns
/// `Err(false)` if [remove_hook]`remove_hook` returns `None`, `Ok(H)` if it is removed successfully **and also** downcasted successfully. `Err(true)` if removal was successful but not downcasting.
pub fn remove_hook_try_downcasted<H: custom::Hook, I: Into<Index>>(&self, hook: I) -> Result<H, bool>
{
if let Some(bx) = self.remove_hook(hook) {
bx.downcast().map_err(|_| true).map(|x| *x)
} else {
Err(false)
}
}
/// Print a line to this logger.
/// You should usually print using the macros defined here instead of calling this directly.
pub fn println<W,L,D,T>(&self, mut to: W, level: L, what: D, trace: T) -> io::Result<()>

@ -127,7 +127,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
debug!("Logger initialised"); //TODO: Parse config first
print_stats();
debug!("{:?}",sys::user::get_users());
//debug!("{:?}",sys::user::get_users());
#[cfg(feature="watcher")]
{

Loading…
Cancel
Save