2020-03-05 19:26:07 +00:00
|
|
|
use std::fs;
|
2019-11-23 14:34:03 +00:00
|
|
|
|
2019-12-18 19:35:17 +00:00
|
|
|
use crate::settings::Settings;
|
|
|
|
|
2020-08-30 18:02:37 +00:00
|
|
|
use tracing::{debug, error, info, trace};
|
2020-06-21 10:22:26 +00:00
|
|
|
use tracing_subscriber::{filter::LevelFilter, prelude::*, registry, EnvFilter};
|
2019-11-23 14:34:03 +00:00
|
|
|
|
2020-06-22 13:34:35 +00:00
|
|
|
const RUST_LOG_ENV: &str = "RUST_LOG";
|
2019-11-23 14:34:03 +00:00
|
|
|
|
2020-06-22 13:34:35 +00:00
|
|
|
/// Initialise tracing and logging for the settings.
|
2020-06-21 10:22:26 +00:00
|
|
|
///
|
|
|
|
/// This function will attempt to set up both a file and a terminal logger,
|
|
|
|
/// falling back to just a terminal logger if the file is unable to be created.
|
|
|
|
///
|
2020-06-22 13:34:35 +00:00
|
|
|
/// The logging level is by default set to `INFO`, to change this for any
|
|
|
|
/// particular crate or module you must use the `RUST_LOG` environment
|
2020-06-21 10:22:26 +00:00
|
|
|
/// variable.
|
|
|
|
///
|
|
|
|
/// For example to set this crate's debug level to `TRACE` you would need the
|
|
|
|
/// following in your environment.
|
2020-06-22 13:34:35 +00:00
|
|
|
/// `RUST_LOG="veloren_voxygen=trace"`
|
2020-06-21 10:22:26 +00:00
|
|
|
///
|
2020-06-22 13:34:35 +00:00
|
|
|
/// more complex tracing can be done by concatenating with a `,` as seperator:
|
|
|
|
/// - warn for `uvth`, `tiny_http`, `dot_vox`, `gfx_device_gl::factory,
|
|
|
|
/// `gfx_device_gl::shade` trace for `veloren_voxygen`, info for everything
|
|
|
|
/// else
|
|
|
|
/// `RUST_LOG="uvth=warn,tiny_http=warn,dot_vox::parser=warn,gfx_device_gl::
|
|
|
|
/// factory=warn,gfx_device_gl::shade=warn,veloren_voxygen=trace,info"`
|
|
|
|
///
|
|
|
|
/// By default a few directives are set to `warn` by default, until explicitly
|
|
|
|
/// overwritten! e.g. `RUST_LOG="uvth=debug"`
|
2020-06-21 10:22:26 +00:00
|
|
|
pub fn init(settings: &Settings) -> Vec<impl Drop> {
|
|
|
|
// To hold the guards that we create, they will cause the logs to be
|
|
|
|
// flushed when they're dropped.
|
|
|
|
let mut _guards = vec![];
|
2019-11-23 14:34:03 +00:00
|
|
|
|
2020-06-21 10:22:26 +00:00
|
|
|
// We will do lower logging than the default (INFO) by INCLUSION. This
|
|
|
|
// means that if you need lower level logging for a specific module, then
|
|
|
|
// put it in the environment in the correct format i.e. DEBUG logging for
|
2020-06-22 13:34:35 +00:00
|
|
|
// this crate would be veloren_voxygen=debug.
|
2020-08-30 18:02:37 +00:00
|
|
|
let base_exceptions = |env: EnvFilter| {
|
|
|
|
env.add_directive("dot_vox::parser=warn".parse().unwrap())
|
|
|
|
.add_directive("gfx_device_gl=warn".parse().unwrap())
|
|
|
|
.add_directive("uvth=warn".parse().unwrap())
|
|
|
|
.add_directive("tiny_http=warn".parse().unwrap())
|
|
|
|
.add_directive("mio::sys::windows=debug".parse().unwrap())
|
|
|
|
.add_directive(LevelFilter::INFO.into())
|
|
|
|
};
|
2020-06-22 13:34:35 +00:00
|
|
|
|
2020-08-26 06:11:56 +00:00
|
|
|
#[cfg(not(feature = "tracy"))]
|
2020-06-22 13:34:35 +00:00
|
|
|
let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) {
|
|
|
|
Some(Ok(env)) => {
|
2020-08-30 18:02:37 +00:00
|
|
|
let mut filter = base_exceptions(EnvFilter::new(""));
|
2020-06-22 13:34:35 +00:00
|
|
|
for s in env.split(',').into_iter() {
|
|
|
|
match s.parse() {
|
|
|
|
Ok(d) => filter = filter.add_directive(d),
|
|
|
|
Err(err) => println!("WARN ignoring log directive: `{}`: {}", s, err),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
filter
|
|
|
|
},
|
2020-08-30 18:02:37 +00:00
|
|
|
_ => base_exceptions(EnvFilter::from_env(RUST_LOG_ENV)),
|
2020-06-22 13:34:35 +00:00
|
|
|
};
|
2019-12-18 19:35:17 +00:00
|
|
|
|
2020-08-26 06:11:56 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
let filter = EnvFilter::new("dot_vox::parser=warn")
|
|
|
|
.add_directive("gfx_device_gl=warn".parse().unwrap())
|
|
|
|
.add_directive("uvth=warn".parse().unwrap())
|
|
|
|
.add_directive("tiny_http=warn".parse().unwrap())
|
|
|
|
.add_directive(LevelFilter::TRACE.into());
|
|
|
|
|
2020-06-21 10:22:26 +00:00
|
|
|
// Create the terminal writer layer.
|
|
|
|
let (non_blocking, _stdio_guard) = tracing_appender::non_blocking(std::io::stdout());
|
|
|
|
_guards.push(_stdio_guard);
|
2020-03-05 19:26:07 +00:00
|
|
|
|
2020-06-21 10:22:26 +00:00
|
|
|
// Try to create the log file's parent folders.
|
|
|
|
let log_folders_created = fs::create_dir_all(&settings.log.logs_path);
|
2020-06-21 21:47:49 +00:00
|
|
|
const LOG_FILENAME: &str = "voxygen.log";
|
2019-11-23 14:34:03 +00:00
|
|
|
|
2020-06-21 10:22:26 +00:00
|
|
|
match log_folders_created {
|
|
|
|
// If the parent folders were created then attach both a terminal and a
|
|
|
|
// file writer to the registry and init it.
|
|
|
|
Ok(_) => {
|
|
|
|
let file_appender =
|
2020-06-21 21:47:49 +00:00
|
|
|
tracing_appender::rolling::daily(&settings.log.logs_path, LOG_FILENAME);
|
2020-06-21 10:22:26 +00:00
|
|
|
let (non_blocking_file, _file_guard) = tracing_appender::non_blocking(file_appender);
|
|
|
|
_guards.push(_file_guard);
|
2020-08-26 06:11:56 +00:00
|
|
|
#[cfg(not(feature = "tracy"))]
|
|
|
|
registry()
|
2020-06-21 10:22:26 +00:00
|
|
|
.with(tracing_subscriber::fmt::layer().with_writer(non_blocking))
|
|
|
|
.with(tracing_subscriber::fmt::layer().with_writer(non_blocking_file))
|
2020-08-26 06:11:56 +00:00
|
|
|
.with(filter)
|
|
|
|
.init();
|
2020-08-25 08:18:41 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
2020-08-26 06:11:56 +00:00
|
|
|
registry()
|
|
|
|
.with(tracing_tracy::TracyLayer::new())
|
|
|
|
.with(filter)
|
|
|
|
.init();
|
2020-06-21 21:47:49 +00:00
|
|
|
let logdir = &settings.log.logs_path;
|
|
|
|
info!(?logdir, "Setup terminal and file logging.");
|
2020-06-21 10:22:26 +00:00
|
|
|
},
|
|
|
|
// Otherwise just add a terminal writer and init it.
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
2020-06-21 21:47:49 +00:00
|
|
|
?e,
|
|
|
|
"Failed to create log file!. Falling back to terminal logging only.",
|
2020-06-21 10:22:26 +00:00
|
|
|
);
|
2020-08-26 06:11:56 +00:00
|
|
|
#[cfg(not(feature = "tracy"))]
|
|
|
|
registry()
|
2020-06-21 10:22:26 +00:00
|
|
|
.with(tracing_subscriber::fmt::layer().with_writer(non_blocking))
|
2020-08-25 08:18:41 +00:00
|
|
|
.with(filter);
|
|
|
|
#[cfg(feature = "tracy")]
|
2020-08-26 06:11:56 +00:00
|
|
|
registry()
|
|
|
|
.with(tracing_tracy::TracyLayer::new())
|
|
|
|
.with(filter)
|
|
|
|
.init();
|
2020-06-21 10:22:26 +00:00
|
|
|
info!("Setup terminal logging.");
|
|
|
|
},
|
|
|
|
};
|
2020-08-30 18:02:37 +00:00
|
|
|
debug!("Tracing is successfully set to DEBUG or TRACE");
|
|
|
|
trace!("Tracing is successfully set to TRACE");
|
2019-11-23 14:34:03 +00:00
|
|
|
|
2020-06-21 10:22:26 +00:00
|
|
|
// Return the guards
|
|
|
|
_guards
|
2019-11-23 14:34:03 +00:00
|
|
|
}
|