feat(asb): add trace flag for stdout (#366)

* fix(asb): include trace option in raw arguments

* feat(asb): Add --trace flag
This commit is contained in:
Mohan 2025-05-31 13:09:46 +02:00 committed by GitHub
parent 274c630aba
commit a33ebfbb3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 79 additions and 16 deletions

View file

@ -9,6 +9,7 @@ import { getDataDir, initializeContext } from "renderer/rpc";
import { relaunch } from "@tauri-apps/plugin-process";
import RotateLeftIcon from "@material-ui/icons/RotateLeft";
import { revealItemInDir } from "@tauri-apps/plugin-opener";
import { TauriContextStatusEvent } from "models/tauriModel";
const useStyles = makeStyles((theme) => ({
actionsOuter: {
@ -24,13 +25,13 @@ export default function DaemonControlBox() {
// The daemon can be manually started if it has failed or if it has not been started yet
const canContextBeManuallyStarted = useAppSelector(
(s) => s.rpc.status?.type === "Failed" || s.rpc.status === null,
(s) => s.rpc.status === TauriContextStatusEvent.Failed || s.rpc.status === null,
);
const isContextInitializing = useAppSelector(
(s) => s.rpc.status?.type === "Initializing",
(s) => s.rpc.status === TauriContextStatusEvent.Initializing,
);
const stringifiedDaemonStatus = useAppSelector((s) => s.rpc.status?.type ?? "not started");
const stringifiedDaemonStatus = useAppSelector((s) => s.rpc.status ?? "not started");
return (
<InfoBox

View file

@ -20,6 +20,7 @@ where
let args = RawArguments::from_clap(&matches);
let json = args.json;
let trace = args.trace;
let testnet = args.testnet;
let config = args.config;
let command: RawCommand = args.cmd;
@ -28,6 +29,7 @@ where
RawCommand::Start { resume_only } => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Start { resume_only },
@ -35,6 +37,7 @@ where
RawCommand::History { only_unfinished } => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::History { only_unfinished },
@ -46,6 +49,7 @@ where
} => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Logs {
@ -57,6 +61,7 @@ where
RawCommand::WithdrawBtc { amount, address } => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::WithdrawBtc {
@ -67,6 +72,7 @@ where
RawCommand::Balance => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Balance,
@ -74,6 +80,7 @@ where
RawCommand::Config => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Config,
@ -81,6 +88,7 @@ where
RawCommand::ExportBitcoinWallet => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::ExportBitcoinWallet,
@ -91,6 +99,7 @@ where
}) => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Redeem {
@ -104,6 +113,7 @@ where
}) => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Cancel { swap_id },
@ -113,6 +123,7 @@ where
}) => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Refund { swap_id },
@ -122,6 +133,7 @@ where
}) => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::Punish { swap_id },
@ -129,6 +141,7 @@ where
RawCommand::ManualRecovery(ManualRecovery::SafelyAbort { swap_id }) => Arguments {
testnet,
json,
trace,
config_path: config_path(config, testnet)?,
env_config: env_config(testnet),
cmd: Command::SafelyAbort { swap_id },
@ -171,6 +184,7 @@ pub struct BitcoinAddressNetworkMismatch {
pub struct Arguments {
pub testnet: bool,
pub json: bool,
pub trace: bool,
pub config_path: PathBuf,
pub env_config: env::Config,
pub cmd: Command,
@ -232,6 +246,9 @@ pub struct RawArguments {
)]
pub json: bool,
#[structopt(long = "trace", help = "Also output verbose tracing logs to stdout")]
pub trace: bool,
#[structopt(
short,
long = "disable-timestamp",
@ -381,6 +398,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::Start { resume_only: false },
@ -398,6 +416,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::History {
@ -417,6 +436,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::Balance,
@ -438,6 +458,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::WithdrawBtc {
@ -465,6 +486,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::Cancel {
@ -490,6 +512,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::Refund {
@ -515,6 +538,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::Punish {
@ -540,6 +564,7 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::SafelyAbort {
@ -559,6 +584,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::Start { resume_only: false },
@ -576,6 +602,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::History {
@ -595,6 +622,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::Balance,
@ -618,6 +646,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::WithdrawBtc {
@ -645,6 +674,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::Cancel {
@ -671,6 +701,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::Refund {
@ -697,6 +728,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::Punish {
@ -723,6 +755,7 @@ mod tests {
let expected_args = Arguments {
testnet: true,
json: false,
trace: false,
config_path: default_testnet_conf_path,
env_config: testnet_env_config,
cmd: Command::SafelyAbort {
@ -742,6 +775,25 @@ mod tests {
let expected_args = Arguments {
testnet: false,
json: false,
trace: false,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::Start { resume_only: false },
};
let args = parse_args(raw_ars).unwrap();
assert_eq!(expected_args, args);
}
#[test]
fn ensure_trace_mapping() {
let default_mainnet_conf_path = env::Mainnet::getConfigFileDefaults().unwrap().config_path;
let mainnet_env_config = env::Mainnet::get_config();
let raw_ars = vec![BINARY_NAME, "--trace", "start"];
let expected_args = Arguments {
testnet: false,
json: false,
trace: true,
config_path: default_mainnet_conf_path,
env_config: mainnet_env_config,
cmd: Command::Start { resume_only: false },

View file

@ -52,6 +52,7 @@ pub async fn main() -> Result<()> {
let Arguments {
testnet,
json,
trace,
config_path,
env_config,
cmd,
@ -84,7 +85,7 @@ pub async fn main() -> Result<()> {
// Initialize tracing
let format = if json { Format::Json } else { Format::Raw };
let log_dir = config.data.dir.join("logs");
common::tracing_util::init(LevelFilter::DEBUG, format, log_dir, None)
common::tracing_util::init(LevelFilter::DEBUG, format, log_dir, None, trace)
.expect("initialize tracing");
tracing::info!(
binary = "asb",

View file

@ -299,6 +299,7 @@ impl ContextBuilder {
format,
data_dir.join("logs"),
self.tauri_handle.clone(),
false,
);
tracing::info!(
binary = "cli",

View file

@ -24,12 +24,14 @@ pub enum Format {
/// Initialize tracing and enable logging messages according to these options.
/// Besides printing to `stdout`, this will append to a log file.
/// Said file will contain JSON-formatted logs of all levels,
/// disregarding the arguments to this function.
/// disregarding the arguments to this function. When `trace_stdout` is `true`,
/// all tracing logs are also emitted to stdout.
pub fn init(
level_filter: LevelFilter,
format: Format,
dir: impl AsRef<Path>,
tauri_handle: Option<TauriHandle>,
trace_stdout: bool,
) -> Result<()> {
let ALL_CRATES: Vec<&str> = vec![
"swap",
@ -95,23 +97,29 @@ pub fn init(
.json()
.with_filter(env_filter(level_filter, ALL_CRATES.clone())?);
// We only log the bare minimum to the terminal
// Crates: swap, asb
// Level: Passed in
let env_filtered = env_filter(level_filter, OUR_CRATES.clone())?;
// Apply the environment filter and box the layer for the terminal
// If trace_stdout is true, we log all messages to the terminal
// Otherwise, we only log the bare minimum
let terminal_layer_env_filter = match trace_stdout {
true => env_filter(LevelFilter::TRACE, ALL_CRATES.clone())?,
false => env_filter(level_filter, OUR_CRATES.clone())?,
};
let final_terminal_layer = match format {
Format::Json => terminal_layer.json().with_filter(env_filtered).boxed(),
Format::Raw => terminal_layer.with_filter(env_filtered).boxed(),
Format::Json => terminal_layer
.json()
.with_filter(terminal_layer_env_filter)
.boxed(),
Format::Raw => terminal_layer
.with_filter(terminal_layer_env_filter)
.boxed(),
};
tracing_subscriber::registry()
let subscriber = tracing_subscriber::registry()
.with(file_layer)
.with(tracing_file_layer)
.with(final_terminal_layer)
.with(tauri_layer)
.try_init()?;
.with(tauri_layer);
subscriber.try_init()?;
// Now we can use the tracing macros to log messages
tracing::info!(%level_filter, logs_dir=%dir.as_ref().display(), "Initialized tracing. General logs will be written to swap-all.log, and verbose logs to tracing*.log");