From cff01a5b4f59a0c397443783af0b73b9dc5a3e7d Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Wed, 22 Feb 2023 23:05:45 +0100 Subject: [PATCH] Configure log to file feature at runtime instead of feature --- Cargo.toml | 1 - crates/bevy_internal/Cargo.toml | 1 - crates/bevy_log/Cargo.toml | 2 +- crates/bevy_log/src/lib.rs | 58 +++++++++++++++++---------------- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e98c7a1cc15d9..b0920d9ecd0fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,7 +74,6 @@ trace_chrome = ["trace", "bevy_internal/trace_chrome"] trace_tracy = ["trace", "bevy_internal/trace_tracy"] trace = ["bevy_internal/trace"] wgpu_trace = ["bevy_internal/wgpu_trace"] -log_to_file = ["bevy_internal/log_to_file"] # Image format support for texture loading (PNG and HDR are enabled by default) hdr = ["bevy_internal/hdr"] diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index 2e7dcd23c1794..47d220c36f4e9 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -22,7 +22,6 @@ trace_chrome = ["bevy_log/tracing-chrome"] trace_tracy = ["bevy_render?/tracing-tracy", "bevy_log/tracing-tracy"] wgpu_trace = ["bevy_render/wgpu_trace"] debug_asset_server = ["bevy_asset/debug_asset_server"] -log_to_file = ["bevy_log/tracing-appender"] # Image format support for texture loading (PNG and HDR are enabled by default) hdr = ["bevy_render/hdr"] diff --git a/crates/bevy_log/Cargo.toml b/crates/bevy_log/Cargo.toml index 7c9bc8ea635a2..0922efab4be91 100644 --- a/crates/bevy_log/Cargo.toml +++ b/crates/bevy_log/Cargo.toml @@ -24,7 +24,7 @@ tracing-chrome = { version = "0.4.0", optional = true } tracing-tracy = { version = "0.10.0", optional = true } tracing-log = "0.1.2" tracing-error = { version = "0.2.0", optional = true } -tracing-appender = { version = "0.2.2", optional = true } +tracing-appender = { version = "0.2.2" } [target.'cfg(target_os = "android")'.dependencies] android_log-sys = "0.2.0" diff --git a/crates/bevy_log/src/lib.rs b/crates/bevy_log/src/lib.rs index f13dc34f22891..127861f98e5e4 100644 --- a/crates/bevy_log/src/lib.rs +++ b/crates/bevy_log/src/lib.rs @@ -13,7 +13,7 @@ #[cfg(feature = "trace")] use std::panic; -#[cfg(feature = "tracing-appender")] + use std::path::PathBuf; #[cfg(target_os = "android")] @@ -93,7 +93,6 @@ use tracing_subscriber::{prelude::*, registry::Registry, EnvFilter}; pub struct LogPlugin; /// Enum to control how often a new log file will be created -#[cfg(feature = "tracing-appender")] #[derive(Debug, Clone)] pub enum Rolling { /// Creates a new file every minute and appends the date to the file name @@ -109,12 +108,10 @@ pub enum Rolling { Never, } -#[cfg(feature = "tracing-appender")] #[derive(Resource)] struct GuardRes(tracing_appender::non_blocking::WorkerGuard); /// Settings to control the `log_to_file` feature -#[cfg(feature = "tracing-appender")] #[derive(Debug, Clone)] pub struct FileAppenderSettings { /// Controls how often a new file will be created @@ -149,8 +146,7 @@ pub struct LogSettings { pub level: Level, /// ConfigureFileLogging - #[cfg(feature = "tracing-appender")] - pub file_appender: FileAppenderSettings, + pub file_appender: Option, } impl Default for LogSettings { @@ -158,8 +154,7 @@ impl Default for LogSettings { Self { filter: "wgpu=error".to_string(), level: Level::INFO, - #[cfg(feature = "tracing-appender")] - file_appender: FileAppenderSettings::default(), + file_appender: None, } } } @@ -225,32 +220,39 @@ impl Plugin for LogPlugin { let subscriber = subscriber.with(fmt_layer); - #[cfg(feature = "tracing-appender")] let subscriber = { let file_output = { let settings = app.world.get_resource_or_insert_with(LogSettings::default); settings.file_appender.clone() }; - let file_appender = tracing_appender::rolling::RollingFileAppender::new( - match file_output.rolling { - Rolling::Minutely => tracing_appender::rolling::Rotation::MINUTELY, - Rolling::Hourly => tracing_appender::rolling::Rotation::HOURLY, - Rolling::Daily => tracing_appender::rolling::Rotation::DAILY, - Rolling::Never => tracing_appender::rolling::Rotation::NEVER, - }, - file_output.path, - file_output.prefix, - ); - - let (non_blocking, worker_guard) = tracing_appender::non_blocking(file_appender); - let file_fmt_layer = tracing_subscriber::fmt::Layer::default() - .with_ansi(false) - .with_writer(non_blocking); - // We need to keep this somewhere so it doesn't get dropped. If it gets dropped then it will silently stop writing to the file - app.insert_resource(GuardRes(worker_guard)); - - subscriber.with(file_fmt_layer) + let layer = if let Some(file_output) = file_output { + let file_appender = tracing_appender::rolling::RollingFileAppender::new( + match file_output.rolling { + Rolling::Minutely => tracing_appender::rolling::Rotation::MINUTELY, + Rolling::Hourly => tracing_appender::rolling::Rotation::HOURLY, + Rolling::Daily => tracing_appender::rolling::Rotation::DAILY, + Rolling::Never => tracing_appender::rolling::Rotation::NEVER, + }, + file_output.path, + file_output.prefix, + ); + + let (non_blocking, worker_guard) = + tracing_appender::non_blocking(file_appender); + // We need to keep this somewhere so it doesn't get dropped. If it gets dropped then it will silently stop writing to the file + app.insert_resource(GuardRes(worker_guard)); + + let file_fmt_layer = tracing_subscriber::fmt::Layer::default() + .with_ansi(false) + .with_writer(non_blocking); + + Some(file_fmt_layer) + } else { + None + }; + + subscriber.with(layer) }; #[cfg(feature = "tracing-chrome")]