Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure log to file feature at runtime instead of feature #3

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_log/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
58 changes: 30 additions & 28 deletions crates/bevy_log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#[cfg(feature = "trace")]
use std::panic;
#[cfg(feature = "tracing-appender")]

use std::path::PathBuf;

#[cfg(target_os = "android")]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -149,17 +146,15 @@ pub struct LogSettings {
pub level: Level,

/// ConfigureFileLogging
#[cfg(feature = "tracing-appender")]
pub file_appender: FileAppenderSettings,
pub file_appender: Option<FileAppenderSettings>,
}

impl Default for LogSettings {
fn default() -> Self {
Self {
filter: "wgpu=error".to_string(),
level: Level::INFO,
#[cfg(feature = "tracing-appender")]
file_appender: FileAppenderSettings::default(),
file_appender: None,
}
}
}
Expand Down Expand Up @@ -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")]
Expand Down