move tracing code to own crate

This commit is contained in:
Marcel Märtens 2021-03-29 01:40:53 +02:00
parent 3c19713e63
commit ccb78eeb5c
18 changed files with 144 additions and 183 deletions

25
Cargo.lock generated
View File

@ -5342,11 +5342,11 @@ dependencies = [
"termcolor",
"tokio",
"tracing",
"tracing-subscriber",
"vek",
"veloren-common",
"veloren-common-base",
"veloren-common-ecs",
"veloren-common-frontend",
"veloren-common-net",
"veloren-common-sys",
"veloren-network",
@ -5411,6 +5411,19 @@ dependencies = [
"veloren-common-base",
]
[[package]]
name = "veloren-common-frontend"
version = "0.9.0"
dependencies = [
"termcolor",
"tracing",
"tracing-appender",
"tracing-log",
"tracing-subscriber",
"tracing-tracy",
"veloren-common-base",
]
[[package]]
name = "veloren-common-net"
version = "0.9.0"
@ -5575,14 +5588,12 @@ dependencies = [
"ron",
"serde",
"signal-hook 0.3.7",
"termcolor",
"tokio",
"tracing",
"tracing-subscriber",
"tracing-tracy",
"tui",
"veloren-common",
"veloren-common-base",
"veloren-common-frontend",
"veloren-common-net",
"veloren-server",
]
@ -5634,19 +5645,15 @@ dependencies = [
"serde",
"specs",
"specs-idvs",
"termcolor",
"tokio",
"tracing",
"tracing-appender",
"tracing-log",
"tracing-subscriber",
"tracing-tracy",
"treeculler",
"vek",
"veloren-client",
"veloren-common",
"veloren-common-base",
"veloren-common-ecs",
"veloren-common-frontend",
"veloren-common-net",
"veloren-common-sys",
"veloren-server",

View File

@ -7,6 +7,7 @@ members = [
"common/ecs",
"common/net",
"common/sys",
"common/frontend",
"client",
"plugin/api",
"plugin/derive",

View File

@ -5,10 +5,10 @@ authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>"]
edition = "2018"
[features]
tracy = ["common/tracy", "common-base/tracy", "common-sys/tracy", "common-net/tracy"]
tracy = ["common/tracy", "common-base/tracy", "common-sys/tracy", "common-net/tracy", "common-frontend/tracy"]
simd = ["vek/platform_intrinsics"]
plugins = ["common-sys/plugins"]
bin_bot = ["common-ecs", "serde", "ron", "clap", "rustyline", "termcolor", "tracing-subscriber", "async-channel"]
bin_bot = ["common-ecs", "serde", "ron", "clap", "rustyline", "common-frontend", "async-channel"]
default = ["simd"]
@ -40,13 +40,13 @@ clap = { version = "2.33", optional = true }
rustyline = { version = "8.0.0", optional = true }
## logging
termcolor = { version = "1.1", optional = true }
tracing-subscriber = {version = "0.2.3", default-features = false, features = ["env-filter", "fmt", "chrono", "ansi", "smallvec", "tracing-log"], optional = true }
common-frontend = { package = "veloren-common-frontend", path = "../common/frontend", optional = true }
[dev-dependencies]
tracing-subscriber = { version = "0.2.3", default-features = false, features = ["fmt", "chrono", "ansi", "smallvec"] }
[[example]]
name = "chat-cli"
required-features = ["bin_bot"]
[[bin]]
name = "bot"

View File

@ -27,7 +27,7 @@ fn read_input() -> String {
fn main() {
// Initialize logging.
tracing_subscriber::fmt::init();
common_frontend::init_stdout(None);
info!("Starting chat-cli...");

View File

@ -24,7 +24,7 @@ pub struct BotCreds {
}
pub fn main() {
tui::init_logging();
common_frontend::init_stdout(None);
let settings = Settings::load();
info!("Settings: {:?}", settings);

View File

@ -2,21 +2,6 @@ use clap::{App, AppSettings, Arg, SubCommand};
use std::{thread, time::Duration};
use tracing::error;
pub fn init_logging() {
use termcolor::{ColorChoice, StandardStream};
use tracing::Level;
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
const RUST_LOG_ENV: &str = "RUST_LOG";
let filter = EnvFilter::from_env(RUST_LOG_ENV).add_directive(LevelFilter::INFO.into());
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::ERROR)
.with_env_filter(filter);
subscriber
.with_writer(|| StandardStream::stdout(ColorChoice::Auto))
.init();
}
pub enum Cmd {
Register {
prefix: String,

View File

@ -0,0 +1,22 @@
[package]
authors = ["Marcel Märtens <marcel.cochem@googlemail.com>", "Imbris <imbrisf@gmail.com>"]
edition = "2018"
name = "veloren-common-frontend"
description = "common stuff that is used by server-cli and voxygen directly"
version = "0.9.0"
[features]
tracy = ["common-base/tracy", "tracing-tracy"]
[dependencies]
common-base = { package = "veloren-common-base", path = "../base" }
# Logging
termcolor = "1.1"
tracing = { version = "0.1", default-features = false }
tracing-appender = "0.1"
tracing-log = "0.1.1"
tracing-subscriber = { version = "0.2.3", default-features = false, features = ["env-filter", "fmt", "chrono", "ansi", "smallvec", "tracing-log"]}
# Tracy
tracing-tracy = { version = "0.6.0", optional = true }

View File

@ -1,14 +1,16 @@
use std::fs;
use crate::settings::Settings;
#[cfg(not(feature = "tracy"))] use std::fs;
use std::path::Path;
use termcolor::{ColorChoice, StandardStream};
use tracing::{debug, error, info, trace};
use tracing_subscriber::{filter::LevelFilter, prelude::*, registry, EnvFilter};
use tracing::info;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{
filter::LevelFilter, fmt::writer::MakeWriter, prelude::*, registry, EnvFilter,
};
const RUST_LOG_ENV: &str = "RUST_LOG";
/// Initialise tracing and logging for the settings.
/// Initialise tracing and logging for the logs_path.
///
/// 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.
@ -30,10 +32,14 @@ const RUST_LOG_ENV: &str = "RUST_LOG";
///
/// By default a few directives are set to `warn` by default, until explicitly
/// overwritten! e.g. `RUST_LOG="gfx_device_gl=debug"`
pub fn init(settings: &Settings) -> Vec<impl Drop> {
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,
{
// To hold the guards that we create, they will cause the logs to be
// flushed when they're dropped.
let mut _guards = vec![];
let mut _guards: Vec<WorkerGuard> = vec![];
// 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
@ -42,7 +48,11 @@ pub fn init(settings: &Settings) -> Vec<impl Drop> {
let base_exceptions = |env: EnvFilter| {
env.add_directive("dot_vox::parser=warn".parse().unwrap())
.add_directive("gfx_device_gl=warn".parse().unwrap())
.add_directive("prometheus_hyper=warn".parse().unwrap())
.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())
.add_directive("mio::sys::windows=info".parse().unwrap())
.add_directive("h2=info".parse().unwrap())
.add_directive("tokio_util=info".parse().unwrap())
@ -56,7 +66,6 @@ pub fn init(settings: &Settings) -> Vec<impl Drop> {
.add_directive(LevelFilter::INFO.into())
};
#[cfg(not(feature = "tracy"))]
let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) {
Some(Ok(env)) => {
let mut filter = base_exceptions(EnvFilter::new(""));
@ -71,63 +80,68 @@ pub fn init(settings: &Settings) -> Vec<impl Drop> {
_ => base_exceptions(EnvFilter::from_env(RUST_LOG_ENV)),
};
let registry = registry();
#[cfg(not(feature = "tracy"))]
let mut file_setup = false;
#[cfg(feature = "tracy")]
let filter = base_exceptions(EnvFilter::new("")).add_directive(LevelFilter::INFO.into());
let file_setup = false;
#[cfg(feature = "tracy")]
let _terminal = terminal;
// Create the terminal writer layer.
let (non_blocking, _stdio_guard) =
tracing_appender::non_blocking(StandardStream::stdout(ColorChoice::Auto));
_guards.push(_stdio_guard);
#[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))
};
// Try to create the log file's parent folders.
let log_folders_created = fs::create_dir_all(&settings.log.logs_path);
const LOG_FILENAME: &str = "voxygen.log";
#[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();
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 =
tracing_appender::rolling::daily(&settings.log.logs_path, LOG_FILENAME);
let (non_blocking_file, _file_guard) = tracing_appender::non_blocking(file_appender);
_guards.push(_file_guard);
#[cfg(not(feature = "tracy"))]
registry()
.with(tracing_subscriber::fmt::layer().with_writer(non_blocking))
.with(tracing_subscriber::fmt::layer().with_writer(non_blocking_file))
.with(filter)
.init();
#[cfg(feature = "tracy")]
registry()
// NOTE: collecting stacks has a significant overhead (x6 overhead of
// starting/stopping a span through the layer interface)
.with(tracing_tracy::TracyLayer::new().with_stackdepth(0))
.with(filter)
.init();
let logdir = &settings.log.logs_path;
info!(?logdir, "Setup terminal and file logging.");
},
// Otherwise just add a terminal writer and init it.
Err(e) => {
error!(
?e,
"Failed to create log file!. Falling back to terminal logging only.",
);
#[cfg(not(feature = "tracy"))]
registry()
.with(tracing_subscriber::fmt::layer().with_writer(non_blocking))
.with(filter);
#[cfg(feature = "tracy")]
registry()
.with(tracing_tracy::TracyLayer::new().with_stackdepth(0))
.with(filter)
.init();
info!("Setup terminal logging.");
},
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");
};
debug!("Tracing is successfully set to DEBUG or TRACE");
trace!("Tracing is successfully set to TRACE");
// Return the guards
_guards
}
pub fn init_stdout(log_path_file: Option<(&Path, &str)>) -> Vec<impl Drop> {
init(log_path_file, || StandardStream::stdout(ColorChoice::Auto))
}

View File

@ -7,7 +7,7 @@ edition = "2018"
[features]
worldgen = ["server/worldgen"]
default = ["worldgen"]
tracy = ["common/tracy", "tracing-tracy", "server/tracy", "common-net/tracy"]
tracy = ["common/tracy", "server/tracy", "common-net/tracy", "common-frontend/tracy"]
plugins = ["server/plugins"]
[dependencies]
@ -15,6 +15,7 @@ server = { package = "veloren-server", path = "../server", default-features = fa
common = { package = "veloren-common", path = "../common" }
common-base = { package = "veloren-common-base", path = "../common/base" }
common-net = { package = "veloren-common-net", path = "../common/net" }
common-frontend = { package = "veloren-common-frontend", path = "../common/frontend" }
tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] }
ansi-parser = "0.7"
@ -22,15 +23,10 @@ clap = "2.33"
crossterm = "0.19"
lazy_static = "1"
signal-hook = "0.3.6"
termcolor = "1.1"
tracing = { version = "0.1", default-features = false }
tracing-subscriber = { version = "0.2.3", default-features = false, features = ["env-filter", "fmt", "chrono", "ansi", "smallvec"] }
ron = {version = "0.6", default-features = false}
serde = {version = "1.0", features = [ "rc", "derive" ]}
# Tracy
tracing-tracy = { version = "0.6.0", optional = true }
[dependencies.tui]
git = "https://github.com/fdehau/tui-rs.git"
branch="paragraph-scroll"

View File

@ -1,68 +0,0 @@
use crate::tuilog::TuiLog;
use termcolor::{ColorChoice, StandardStream};
use tracing_subscriber::{filter::LevelFilter, EnvFilter, FmtSubscriber};
#[cfg(feature = "tracy")]
use tracing_subscriber::{layer::SubscriberExt, prelude::*};
const RUST_LOG_ENV: &str = "RUST_LOG";
lazy_static::lazy_static! {
pub static ref LOG: TuiLog<'static> = TuiLog::default();
}
pub fn init(basic: bool) {
// Init logging
let base_exceptions = |env: EnvFilter| {
env.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())
.add_directive("mio::sys::windows=info".parse().unwrap())
.add_directive("h2=info".parse().unwrap())
.add_directive("tokio_util=info".parse().unwrap())
.add_directive("rustls=info".parse().unwrap())
.add_directive("veloren_network_protocol=info".parse().unwrap())
.add_directive(
"veloren_server::persistence::character=info"
.parse()
.unwrap(),
)
.add_directive(LevelFilter::INFO.into())
};
let filter = match std::env::var_os(RUST_LOG_ENV).map(|s| s.into_string()) {
Some(Ok(env)) => {
let mut filter = base_exceptions(EnvFilter::new(""));
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
},
_ => base_exceptions(EnvFilter::from_env(RUST_LOG_ENV)),
};
#[cfg(feature = "tracy")]
tracing_subscriber::registry()
.with(filter)
.with(tracing_tracy::TracyLayer::new().with_stackdepth(0))
.init();
#[cfg(not(feature = "tracy"))]
// TODO: when tracing gets per Layer filters re-enable this when the tracy feature is being
// used (and do the same in voxygen)
{
let subscriber = FmtSubscriber::builder().with_env_filter(filter);
if basic {
subscriber
.with_writer(|| StandardStream::stdout(ColorChoice::Auto))
.init();
} else {
subscriber.with_writer(|| LOG.clone()).init();
}
}
}

View File

@ -6,13 +6,13 @@ mod admin;
/// `server-cli` interface commands not to be confused with the commands sent
/// from the client to the server
mod cmd;
mod logging;
mod settings;
mod shutdown_coordinator;
mod tui_runner;
mod tuilog;
use crate::{cmd::Message, shutdown_coordinator::ShutdownCoordinator, tui_runner::Tui};
use crate::{
cmd::Message, shutdown_coordinator::ShutdownCoordinator, tui_runner::Tui, tuilog::TuiLog,
};
use clap::{App, Arg, SubCommand};
use common::clock::Clock;
use common_base::span;
@ -25,6 +25,9 @@ use std::{
};
use tracing::{info, trace};
lazy_static::lazy_static! {
pub static ref LOG: TuiLog<'static> = TuiLog::default();
}
const TPS: u64 = 30;
#[allow(clippy::unnecessary_wraps)]
@ -83,7 +86,11 @@ fn main() -> io::Result<()> {
#[cfg(any(target_os = "linux", target_os = "macos"))]
let _ = signal_hook::flag::register(signal_hook::consts::SIGUSR1, Arc::clone(&sigusr1_signal));
logging::init(basic);
let (_guards, _guards2) = if basic {
(vec![], common_frontend::init_stdout(None))
} else {
(common_frontend::init(None, || LOG.clone()), vec![])
};
// Load settings
let settings = settings::Settings::load();

View File

@ -1,4 +1,4 @@
use crate::{cmd, logging::LOG, Message};
use crate::{cmd, Message, LOG};
use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture},
execute,

View File

@ -13,7 +13,7 @@ gl = ["gfx_device_gl", "gfx_gl"]
hot-anim = ["anim/use-dyn-lib"]
singleplayer = ["server"]
simd = ["vek/platform_intrinsics"]
tracy = ["tracing-tracy", "common/tracy", "common-ecs/tracy", "common-net/tracy", "common-sys/tracy", "client/tracy"]
tracy = ["common/tracy", "common-ecs/tracy", "common-frontend/tracy", "common-net/tracy", "common-sys/tracy", "client/tracy"]
plugins = ["client/plugins"]
default = ["gl", "singleplayer", "native-dialog", "plugins", "simd"]
@ -23,6 +23,7 @@ client = {package = "veloren-client", path = "../client"}
common = {package = "veloren-common", path = "../common"}
common-base = {package = "veloren-common-base", path = "../common/base"}
common-ecs = {package = "veloren-common-ecs", path = "../common/ecs"}
common-frontend = {package = "veloren-common-frontend", path = "../common/frontend"}
common-net = {package = "veloren-common-net", path = "../common/net"}
common-sys = {package = "veloren-common-sys", path = "../common/sys"}
@ -90,14 +91,7 @@ inline_tweak = "1.0.2"
itertools = "0.10.0"
# Tracy
tracing-tracy = { version = "0.6.0", optional = true }
# Logging
termcolor = "1.1"
tracing = "0.1"
tracing-appender = "0.1"
tracing-log = "0.1.1"
tracing-subscriber = {version = "0.2.3", default-features = false, features = ["env-filter", "fmt", "chrono", "ansi", "smallvec", "tracing-log"]}
[target.'cfg(target_os = "macos")'.dependencies]
dispatch = "0.1.4"

View File

@ -641,6 +641,8 @@ impl Show {
self.settings(true)
},
};
#[cfg(not(feature = "singleplayer"))]
let _global_state = global_state;
}
// TODO: Add self updating key-bindings element
@ -683,6 +685,8 @@ impl Show {
#[cfg(feature = "singleplayer")]
global_state.pause();
}
#[cfg(not(feature = "singleplayer"))]
let _global_state = global_state;
}
fn open_setting_tab(&mut self, tab: SettingsTab) {

View File

@ -14,7 +14,6 @@ pub mod error;
pub mod hud;
pub mod i18n;
pub mod key_state;
pub mod logging;
pub mod menu;
pub mod mesh;
pub mod profile;

View File

@ -6,7 +6,6 @@
use veloren_voxygen::{
audio::AudioFrontend,
i18n::{self, i18n_asset_key, Localization},
logging,
profile::Profile,
run,
settings::{get_fps, AudioOutput, Settings},
@ -34,7 +33,8 @@ fn main() {
}
// Init logging and hold the guards.
let _guards = logging::init(&settings);
const LOG_FILENAME: &str = "voxygen.log";
let _guards = common_frontend::init_stdout(Some((&settings.log.logs_path, LOG_FILENAME)));
if let Some(path) = veloren_voxygen::settings::voxygen_data_dir().parent() {
info!("Using userdata dir at: {}", path.display());

View File

@ -29,6 +29,7 @@ pub enum Msg {
pub enum ClientConnArgs {
Host(String),
#[allow(dead_code)] //singleplayer
Resolved(ConnectionArgs),
}

View File

@ -11,10 +11,9 @@ use crate::{
window::Event,
Direction, GlobalState, PlayState, PlayStateResult,
};
use client::{
addr::ConnectionArgs,
error::{InitProtocolError, NetworkConnectError, NetworkError},
};
#[cfg(feature = "singleplayer")]
use client::addr::ConnectionArgs;
use client::error::{InitProtocolError, NetworkConnectError, NetworkError};
use client_init::{ClientConnArgs, ClientInit, Error as InitError, Msg as InitMsg};
use common::{assets::AssetExt, comp};
use common_base::span;