Skip to content

Commit

Permalink
refactor: split logger config and service
Browse files Browse the repository at this point in the history
  • Loading branch information
yangby-cryptape committed Aug 11, 2020
1 parent 057b7e2 commit 424562b
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 112 deletions.
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
# Members are ordered by dependencies. Crates at top has fewer dependencies.
"util/build-info",
"util/logger",
"util/logger-config",
"util/logger-service",
"util/memory-tracker",
"util",
Expand Down
1 change: 1 addition & 0 deletions ckb-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ckb-verification = { path = "../verification" }
base64 = "0.10.1"
tempfile = "3.0"
rayon = "1.0"
sentry = "0.16.0"

[features]
deadlock_detection = ["ckb-util/deadlock_detection"]
Expand Down
6 changes: 5 additions & 1 deletion ckb-bin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
mod helper;
mod setup_guard;
mod subcommand;

use ckb_app_config::{cli, ExitCode, Setup};
use ckb_build_info::Version;

use setup_guard::SetupGuard;

pub(crate) const LOG_TARGET_MAIN: &str = "main";
pub(crate) const LOG_TARGET_SENTRY: &str = "sentry";

pub fn run_app(version: Version) -> Result<(), ExitCode> {
// Always print backtrace on panic.
Expand All @@ -31,7 +35,7 @@ pub fn run_app(version: Version) -> Result<(), ExitCode> {
}

let setup = Setup::from_matches(&app_matches)?;
let _guard = setup.setup_app(&version)?;
let _guard = SetupGuard::from_setup(&setup, &version)?;

match app_matches.subcommand() {
(cli::CMD_RUN, Some(matches)) => subcommand::run(setup.run(&matches)?, version),
Expand Down
51 changes: 51 additions & 0 deletions ckb-bin/src/setup_guard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use ckb_app_config::{ExitCode, Setup};
use ckb_build_info::Version;
use ckb_logger::info_target;
use ckb_logger_service::{self, LoggerInitGuard};

pub struct SetupGuard {
_logger_guard: LoggerInitGuard,
_sentry_guard: Option<sentry::internals::ClientInitGuard>,
}

impl SetupGuard {
pub(crate) fn from_setup(setup: &Setup, version: &Version) -> Result<Self, ExitCode> {
// Initialization of logger must do before sentry, since `logger::init()` and
// `sentry_config::init()` both registers custom panic hooks, but `logger::init()`
// replaces all hooks previously registered.
let mut logger_config = setup.config.logger().to_owned();
if logger_config.emit_sentry_breadcrumbs.is_none() {
logger_config.emit_sentry_breadcrumbs = Some(setup.is_sentry_enabled);
}
let logger_guard = ckb_logger_service::init(logger_config)?;

let sentry_guard = if setup.is_sentry_enabled {
let sentry_config = setup.config.sentry();

info_target!(
crate::LOG_TARGET_SENTRY,
"**Notice**: \
The ckb process will send stack trace to sentry on Rust panics. \
This is enabled by default before mainnet, which can be opted out by setting \
the option `dsn` to empty in the config file. The DSN is now {}",
sentry_config.dsn
);

let guard = sentry_config.init(&version);

sentry::configure_scope(|scope| {
scope.set_tag("subcommand", &setup.subcommand_name);
});

Some(guard)
} else {
info_target!(crate::LOG_TARGET_SENTRY, "sentry is disabled");
None
};

Ok(Self {
_logger_guard: logger_guard,
_sentry_guard: sentry_guard,
})
}
}
1 change: 1 addition & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
parking_lot = "=0.7.1"
linked-hash-map = "0.5"
regex = "1.1.6"

[dev-dependencies]
ckb-fixed-hash = { path = "fixed-hash" }
Expand Down
2 changes: 1 addition & 1 deletion util/app-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde_plain = "0.3.0"
toml = "0.5"
path-clean = "0.1.0"
ckb-logger = { path = "../../util/logger" }
ckb-logger-service = { path = "../../util/logger-service" }
ckb-logger-config = { path = "../../util/logger-config" }
sentry = "0.16.0"
ckb-chain-spec = {path = "../../spec"}
ckb-jsonrpc-types = {path = "../jsonrpc-types"}
Expand Down
2 changes: 1 addition & 1 deletion util/app-config/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};

use ckb_chain_spec::ChainSpec;
use ckb_logger_service::Config as LogConfig;
use ckb_logger_config::Config as LogConfig;
use ckb_resource::Resource;

use super::configs::*;
Expand Down
52 changes: 0 additions & 52 deletions util/app-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,17 @@ pub use args::{
pub use configs::*;
pub use exit_code::ExitCode;

use ckb_build_info::Version;
use ckb_chain_spec::{consensus::Consensus, ChainSpec};
use ckb_jsonrpc_types::ScriptHashType;
use ckb_logger::info_target;
use ckb_logger_service::LoggerInitGuard;
use clap::{value_t, ArgMatches, ErrorKind};
use std::path::PathBuf;

pub(crate) const LOG_TARGET_SENTRY: &str = "sentry";

pub struct Setup {
pub subcommand_name: String,
pub config: AppConfig,
pub is_sentry_enabled: bool,
}

pub struct SetupGuard {
#[allow(dead_code)]
logger_guard: LoggerInitGuard,
#[allow(dead_code)]
sentry_guard: Option<sentry::internals::ClientInitGuard>,
}

impl Setup {
pub fn from_matches<'m>(matches: &ArgMatches<'m>) -> Result<Setup, ExitCode> {
let subcommand_name = match matches.subcommand_name() {
Expand All @@ -57,46 +45,6 @@ impl Setup {
})
}

pub fn setup_app(&self, version: &Version) -> Result<SetupGuard, ExitCode> {
// Initialization of logger must do before sentry, since `logger::init()` and
// `sentry_config::init()` both registers custom panic hooks, but `logger::init()`
// replaces all hooks previously registered.
let mut logger_config = self.config.logger().to_owned();
if logger_config.emit_sentry_breadcrumbs.is_none() {
logger_config.emit_sentry_breadcrumbs = Some(self.is_sentry_enabled);
}
let logger_guard = ckb_logger_service::init(logger_config)?;

let sentry_guard = if self.is_sentry_enabled {
let sentry_config = self.config.sentry();

info_target!(
crate::LOG_TARGET_SENTRY,
"**Notice**: \
The ckb process will send stack trace to sentry on Rust panics. \
This is enabled by default before mainnet, which can be opted out by setting \
the option `dsn` to empty in the config file. The DSN is now {}",
sentry_config.dsn
);

let guard = sentry_config.init(&version);

sentry::configure_scope(|scope| {
scope.set_tag("subcommand", &self.subcommand_name);
});

Some(guard)
} else {
info_target!(crate::LOG_TARGET_SENTRY, "sentry is disabled");
None
};

Ok(SetupGuard {
logger_guard,
sentry_guard,
})
}

pub fn run<'m>(self, matches: &ArgMatches<'m>) -> Result<RunArgs, ExitCode> {
let consensus = self.consensus()?;
let config = self.config.into_ckb()?;
Expand Down
9 changes: 9 additions & 0 deletions util/logger-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "ckb-logger-config"
version = "0.36.0-pre"
license = "MIT"
authors = ["Nervos <dev@nervos.org>"]
edition = "2018"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
38 changes: 38 additions & 0 deletions util/logger-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::{collections::HashMap, path::PathBuf};

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Config {
pub filter: Option<String>,
pub color: bool,
#[serde(skip)]
pub file: PathBuf,
#[serde(skip)]
pub log_dir: PathBuf,
pub log_to_file: bool,
pub log_to_stdout: bool,
pub emit_sentry_breadcrumbs: Option<bool>,
#[serde(default)]
pub extra: HashMap<String, ExtraLoggerConfig>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExtraLoggerConfig {
pub filter: String,
}

impl Default for Config {
fn default() -> Self {
Config {
filter: None,
color: !cfg!(windows),
file: Default::default(),
log_dir: Default::default(),
log_to_file: false,
log_to_stdout: true,
emit_sentry_breadcrumbs: None,
extra: Default::default(),
}
}
}
2 changes: 1 addition & 1 deletion util/logger-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ edition = "2018"

[dependencies]
ckb-util = { path = ".." }
ckb-logger-config = { path = "../logger-config" }
ansi_term = "0.12"
log = "0.4"
env_logger = "0.6"
lazy_static = "1.3"
regex = "1.1.6"
chrono = "0.4"
serde = { version = "1.0", features = ["derive"] }
crossbeam-channel = "0.3"
backtrace = "0.3"
sentry = "0.16.0"
Loading

0 comments on commit 424562b

Please sign in to comment.