2021-03-28 23:40:53 +00:00
|
|
|
#[cfg(not(feature = "tracy"))] use std::fs;
|
|
|
|
use std::path::Path;
|
2019-12-18 19:35:17 +00:00
|
|
|
|
2020-12-06 14:41:59 +00:00
|
|
|
use termcolor::{ColorChoice, StandardStream};
|
2021-03-28 23:40:53 +00:00
|
|
|
use tracing::info;
|
|
|
|
use tracing_appender::non_blocking::WorkerGuard;
|
|
|
|
use tracing_subscriber::{
|
|
|
|
filter::LevelFilter, fmt::writer::MakeWriter, 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
|
|
|
|
2021-03-28 23:40:53 +00:00
|
|
|
/// Initialise tracing and logging for the logs_path.
|
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:
|
2021-02-18 00:01:57 +00:00
|
|
|
/// - warn for `prometheus_hyper`, `dot_vox`, `gfx_device_gl::factory,
|
2020-06-22 13:34:35 +00:00
|
|
|
/// `gfx_device_gl::shade` trace for `veloren_voxygen`, info for everything
|
|
|
|
/// else
|
2021-02-18 00:01:57 +00:00
|
|
|
/// `RUST_LOG="prometheus_hyper=warn,dot_vox::parser=warn,gfx_device_gl::
|
2020-06-22 13:34:35 +00:00
|
|
|
/// factory=warn,gfx_device_gl::shade=warn,veloren_voxygen=trace,info"`
|
|
|
|
///
|
|
|
|
/// By default a few directives are set to `warn` by default, until explicitly
|
2021-02-18 00:01:57 +00:00
|
|
|
/// overwritten! e.g. `RUST_LOG="gfx_device_gl=debug"`
|
2021-03-28 23:40:53 +00:00
|
|
|
pub fn init<W2>(log_path_file: Option<(&Path, &str)>, terminal: W2) -> Vec<impl Drop>
|
|
|
|
where
|
|
|
|
W2: MakeWriter + 'static,
|
|
|
|
<W2 as MakeWriter>::Writer: Send + Sync,
|
|
|
|
{
|
2020-06-21 10:22:26 +00:00
|
|
|
// To hold the guards that we create, they will cause the logs to be
|
|
|
|
// flushed when they're dropped.
|
2021-03-28 23:40:53 +00:00
|
|
|
let mut _guards: Vec<WorkerGuard> = 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())
|
2021-04-08 21:52:12 +00:00
|
|
|
.add_directive("veloren_common::trade=info".parse().unwrap())
|
2021-03-28 23:40:53 +00:00
|
|
|
.add_directive("veloren_world::sim=info".parse().unwrap())
|
|
|
|
.add_directive("veloren_world::civ=info".parse().unwrap())
|
|
|
|
.add_directive("hyper=info".parse().unwrap())
|
|
|
|
.add_directive("prometheus_hyper=info".parse().unwrap())
|
|
|
|
.add_directive("mio::pool=info".parse().unwrap())
|
2021-03-13 06:48:30 +00:00
|
|
|
.add_directive("mio::sys::windows=info".parse().unwrap())
|
2021-03-11 00:08:12 +00:00
|
|
|
.add_directive("h2=info".parse().unwrap())
|
|
|
|
.add_directive("tokio_util=info".parse().unwrap())
|
2021-03-13 06:48:30 +00:00
|
|
|
.add_directive("rustls=info".parse().unwrap())
|
2021-02-10 10:37:42 +00:00
|
|
|
.add_directive("veloren_network_protocol=info".parse().unwrap())
|
2021-05-17 17:32:26 +00:00
|
|
|
.add_directive("quinn_proto::connection=info".parse().unwrap())
|
2020-10-12 22:06:24 +00:00
|
|
|
.add_directive(
|
|
|
|
"veloren_server::persistence::character=info"
|
|
|
|
.parse()
|
|
|
|
.unwrap(),
|
|
|
|
)
|
Added non-admin moderators and timed bans.
The security model has been updated to reflect this change (for example,
moderators cannot revert a ban by an administrator). Ban history is
also now recorded in the ban file, and much more information about the
ban is stored (whitelists and administrators also have extra
information).
To support the new information without losing important information,
this commit also introduces a new migration path for editable settings
(both from legacy to the new format, and between versions). Examples
of how to do this correctly, and migrate to new versions of a settings
file, are in the settings/ subdirectory.
As part of this effort, editable settings have been revamped to
guarantee atomic saves (due to the increased amount of information in
each file), some latent bugs in networking were fixed, and server-cli
has been updated to go through StructOpt for both calls through TUI
and argv, greatly simplifying parsing logic.
2021-05-08 18:22:21 +00:00
|
|
|
.add_directive("veloren_server::settings=info".parse().unwrap())
|
2020-08-30 18:02:37 +00:00
|
|
|
.add_directive(LevelFilter::INFO.into())
|
|
|
|
};
|
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
|
|
|
|
2021-03-28 23:40:53 +00:00
|
|
|
let registry = registry();
|
|
|
|
#[cfg(not(feature = "tracy"))]
|
|
|
|
let mut file_setup = false;
|
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
let file_setup = false;
|
2020-08-26 06:11:56 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
2021-03-28 23:40:53 +00:00
|
|
|
let _terminal = terminal;
|
2020-08-26 06:11:56 +00:00
|
|
|
|
2020-06-21 10:22:26 +00:00
|
|
|
// Create the terminal writer layer.
|
2021-03-28 23:40:53 +00:00
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
let registry = registry.with(tracing_tracy::TracyLayer::new().with_stackdepth(0));
|
|
|
|
#[cfg(not(feature = "tracy"))]
|
|
|
|
let registry = {
|
|
|
|
let (non_blocking, _stdio_guard) = tracing_appender::non_blocking(terminal.make_writer());
|
|
|
|
_guards.push(_stdio_guard);
|
|
|
|
registry.with(tracing_subscriber::fmt::layer().with_writer(non_blocking))
|
|
|
|
};
|
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.
|
2021-03-28 23:40:53 +00:00
|
|
|
#[cfg(not(feature = "tracy"))]
|
|
|
|
if let Some((path, file)) = log_path_file {
|
|
|
|
match fs::create_dir_all(path) {
|
|
|
|
Ok(_) => {
|
|
|
|
let file_appender = tracing_appender::rolling::daily(path, file);
|
|
|
|
let (non_blocking_file, _file_guard) =
|
|
|
|
tracing_appender::non_blocking(file_appender);
|
|
|
|
_guards.push(_file_guard);
|
|
|
|
file_setup = true;
|
|
|
|
registry
|
|
|
|
.with(tracing_subscriber::fmt::layer().with_writer(non_blocking_file))
|
|
|
|
.with(filter)
|
|
|
|
.init();
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(
|
|
|
|
?e,
|
|
|
|
"Failed to create log file!. Falling back to terminal logging only.",
|
|
|
|
);
|
|
|
|
registry.with(filter).init();
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
registry.with(filter).init();
|
|
|
|
}
|
|
|
|
#[cfg(feature = "tracy")]
|
|
|
|
registry.with(filter).init();
|
2019-11-23 14:34:03 +00:00
|
|
|
|
2021-03-28 23:40:53 +00:00
|
|
|
if file_setup {
|
|
|
|
let (path, file) = log_path_file.unwrap();
|
|
|
|
info!(?path, ?file, "Setup terminal and file logging.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if tracing::level_enabled!(tracing::Level::TRACE) {
|
|
|
|
info!("Tracing Level: TRACE");
|
|
|
|
} else if tracing::level_enabled!(tracing::Level::DEBUG) {
|
|
|
|
info!("Tracing Level: DEBUG");
|
2020-06-21 10:22:26 +00:00
|
|
|
};
|
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
|
|
|
}
|
2021-03-28 23:40:53 +00:00
|
|
|
|
|
|
|
pub fn init_stdout(log_path_file: Option<(&Path, &str)>) -> Vec<impl Drop> {
|
|
|
|
init(log_path_file, || StandardStream::stdout(ColorChoice::Auto))
|
|
|
|
}
|