big heckin reworkerino to use the standard #line directive as per spec

This commit is contained in:
Noah Santschi-Cooney 2022-04-03 21:31:18 +01:00
parent cb7c9b8b49
commit d3365c3bff
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48
52 changed files with 574 additions and 398 deletions

43
server/logging/src/lib.rs Normal file
View file

@ -0,0 +1,43 @@
use rand::{rngs, Rng};
use slog::slog_o;
use slog_scope::GlobalLoggerGuard;
use slog_term::{FullFormat, PlainSyncDecorator};
use std::{cell::RefCell, sync::Arc};
use std::io::Stderr;
use lazy_static::lazy_static;
use slog::*;
use slog_atomic::*;
fn new_trace_id() -> String {
let rng = CURRENT_RNG.with(|rng| rng.borrow_mut().gen::<[u8; 4]>());
return format!("{:04x}", u32::from_be_bytes(rng));
}
pub fn slog_with_trace_id<F: FnOnce()>(f: F) {
slog_scope::scope(&slog_scope::logger().new(slog_o!("trace" => new_trace_id())), f)
}
pub fn set_logger_with_level(level: Level) -> GlobalLoggerGuard {
let drain = Arc::new(logger_base(level).fuse());
DRAIN_SWITCH.ctrl().set(drain.clone());
slog_scope::set_global_logger(Logger::root(drain, o!()))
}
fn logger_base(level: Level) -> LevelFilter<Fuse<FullFormat<PlainSyncDecorator<Stderr>>>> {
let plain = slog_term::PlainSyncDecorator::new(std::io::stderr());
let drain = slog_term::FullFormat::new(plain).build().fuse();
drain.filter_level(level)
}
thread_local! {
static CURRENT_RNG: RefCell<rngs::ThreadRng> = RefCell::new(rngs::ThreadRng::default());
}
lazy_static! {
static ref DRAIN_SWITCH: AtomicSwitch<()> = {
let logger = logger_base(Level::Info).fuse();
AtomicSwitch::new(logger)
};
}