51 lines
2.2 KiB
Rust
51 lines
2.2 KiB
Rust
// file: crates/common/game-logging-lib/src/runtime.rs
|
|
// version: 2
|
|
|
|
/// Error returned when the process-global tracing subscriber cannot be 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 could not be configured");
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for LoggingInitError {}
|
|
|
|
/// Guard keeping platform tracing resources alive for the application lifetime.
|
|
pub struct LoggingWorkerGuard {
|
|
#[cfg(not(target_os = "android"))]
|
|
_worker_guard: tracing_appender::non_blocking::WorkerGuard,
|
|
}
|
|
|
|
/// Initializes process-global tracing for the current platform.
|
|
///
|
|
/// Native desktop/Tauri processes write formatted events to standard error.
|
|
/// Android processes write directly to logcat through the Android NDK logging API.
|
|
///
|
|
/// 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> {
|
|
#[cfg(target_os = "android")]
|
|
{
|
|
let layer = match tracing_android::layer("games.sasedev") {
|
|
std::result::Result::Ok(value) => value,
|
|
std::result::Result::Err(_) => return std::result::Result::Err(LoggingInitError),
|
|
};
|
|
let subscriber = tracing_subscriber::layer::SubscriberExt::with(tracing_subscriber::registry(), layer);
|
|
if tracing::subscriber::set_global_default(subscriber).is_err() {
|
|
return std::result::Result::Err(LoggingInitError);
|
|
}
|
|
return std::result::Result::Ok(LoggingWorkerGuard {});
|
|
}
|
|
#[cfg(not(target_os = "android"))]
|
|
{
|
|
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 });
|
|
}
|
|
}
|