0.1.0-0-pre.4
This commit is contained in:
31
crates/common/game-logging-lib/src/runtime.rs
Normal file
31
crates/common/game-logging-lib/src/runtime.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
// file: crates/common/game-logging-lib/src/runtime.rs
|
||||
// version: 1
|
||||
|
||||
/// Error returned when the process-global tracing subscriber is already configured.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct LoggingInitError;
|
||||
|
||||
impl std::fmt::Display for LoggingInitError {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
return formatter.write_str("the process-global tracing subscriber is already configured");
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for LoggingInitError {}
|
||||
|
||||
/// Guard keeping the non-blocking tracing writer alive for the application lifetime.
|
||||
pub struct LoggingWorkerGuard {
|
||||
_worker_guard: tracing_appender::non_blocking::WorkerGuard,
|
||||
}
|
||||
|
||||
/// Initializes a process-global non-blocking tracing subscriber writing formatted events to standard error.
|
||||
///
|
||||
/// The returned guard must remain alive for as long as events may still be emitted.
|
||||
pub fn init_console_tracing() -> std::result::Result<LoggingWorkerGuard, LoggingInitError> {
|
||||
let (writer, worker_guard) = tracing_appender::non_blocking(std::io::stderr());
|
||||
let subscriber = tracing_subscriber::fmt().with_target(true).with_writer(writer).finish();
|
||||
if tracing::subscriber::set_global_default(subscriber).is_err() {
|
||||
return std::result::Result::Err(LoggingInitError);
|
||||
}
|
||||
return std::result::Result::Ok(LoggingWorkerGuard { _worker_guard: worker_guard });
|
||||
}
|
||||
Reference in New Issue
Block a user