Skip to content

Commit

Permalink
logging improvements with tracing-subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Mar 14, 2024
1 parent 5e0590b commit e278749
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 54 deletions.
2 changes: 1 addition & 1 deletion braid-offline/src/bin/braid-offline-retrack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tracing_futures::Instrument;

fn main() -> anyhow::Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "braid_offline=info,flydra2=info,error");
std::env::set_var("RUST_LOG", "braid_offline=info,flydra2=info,warn");
}

// console_subscriber::init();
Expand Down
7 changes: 5 additions & 2 deletions braid/braid-run/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ include_dir = { version = "0.7.3", optional = true }
axum-token-auth = "0.1.0"
http-body = "1.0.0"
base64 = "0.12"
cookie_store = "0.20.0"
cookie = "0.18.0"
tracing-subscriber = "0.3.18"
shellexpand = "2.0"

braid = {path=".."}
braid-config-data = {path="../../braid-config-data"}
bui-backend-session-types = { path = "../../bui-backend-session/types" }
bui-backend-session = {path="../../bui-backend-session"}
ci2-remote-control = {path = "../../ci2-remote-control"}
datetime-conversion = {path = "../../datetime-conversion"}
env-tracing-logger = {path = "../../env-tracing-logger" }
event-stream-types = { path = "../../event-stream-types" }
flydra-feature-detector-types = {path = "../../flydra-feature-detector/flydra-feature-detector-types", default-features = false}
flydra-mvg = {path="../../flydra-mvg"}
Expand All @@ -63,8 +68,6 @@ flydra2 = {path="../../flydra2", default-features = false, features=["braid"]}
mvg = {path="../../mvg"}
rust-cam-bui-types = {path="../../rust-cam-bui-types"}
strand-cam-storetype = {path = "../../strand-cam-storetype"}
cookie_store = "0.20.0"
cookie = "0.18.0"

[features]
default = ["bundle_files"]
Expand Down
34 changes: 27 additions & 7 deletions braid/braid-run/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg_attr(feature = "backtrace", feature(error_generic_member_access))]

use anyhow::Result;
use anyhow::{Context, Result};
use clap::Parser;
use tracing::debug;

Expand All @@ -19,6 +19,9 @@ mod multicam_http_session_handler;
struct BraidRunCliArgs {
/// Input directory
config_file: std::path::PathBuf,
/// Flag if logging to console should be disabled.
#[arg(short, long)]
disable_console: bool,
}

fn compute_strand_cam_args(
Expand All @@ -39,8 +42,6 @@ fn launch_strand_cam(
camera: &BraidCameraConfig,
mainbrain_internal_addr: &MainbrainBuiLocation,
) -> Result<()> {
use anyhow::Context;

// On initial startup strand cam queries for
// [flydra_types::RemoteCameraInfoResponse] and thus we do not need to
// provide much info.
Expand Down Expand Up @@ -83,10 +84,29 @@ async fn main() -> Result<()> {
braid_start("run")?;

let args = BraidRunCliArgs::parse();
debug!("{:?}", args);

let cfg = parse_config_file(&args.config_file)?;
debug!("{:?}", cfg);
let cfg = parse_config_file(&args.config_file).with_context(|| {
format!(
"when parsing configuration file {}",
args.config_file.display()
)
})?;

let log_file_name = format!(
"~/.braid-{}.log",
std::time::SystemTime::UNIX_EPOCH
.elapsed()
.unwrap()
.as_micros(),
);
let log_file_name = std::path::PathBuf::from(shellexpand::full(&log_file_name)?.to_string());
// TODO: delete log files older than, e.g. one week.

let _guard = env_tracing_logger::initiate_logging(Some(&log_file_name), args.disable_console)
.map_err(|e| anyhow::anyhow!("error initiating logging: {e}"))?;

let version = format!("{} (git {})", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"));
tracing::info!("{} {}", "run", version);
tracing::debug!("{:?}", cfg);

let camera_configs = cfg
.cameras
Expand Down
8 changes: 7 additions & 1 deletion braid/src/bin/braid-default-config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use braid::{braid_start, BraidConfig};
use braid::braid_start;
use braid_config_data::BraidConfig;
use clap::Parser;

/// show the default configuration in TOML format
Expand All @@ -10,6 +11,11 @@ struct BraidDefaultConfigCliArgs {}
fn main() -> Result<()> {
braid_start("default-config")?;

env_tracing_logger::init();

let version = format!("{} (git {})", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"));
tracing::info!("{} {}", env!("CARGO_PKG_NAME"), version);

let args = BraidDefaultConfigCliArgs::parse();
tracing::debug!("{:?}", args);

Expand Down
5 changes: 5 additions & 0 deletions braid/src/bin/braid-show-config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ struct BraidShowConfigCliArgs {
fn main() -> Result<()> {
braid_start("show-config").with_context(|| format!("launching show-config command"))?;

env_tracing_logger::init();

let version = format!("{} (git {})", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"));
tracing::info!("{} {}", env!("CARGO_PKG_NAME"), version);

let args = BraidShowConfigCliArgs::parse();
tracing::debug!("{:?}", args);

Expand Down
1 change: 0 additions & 1 deletion braid/src/bin/braid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ struct BraidLauncherCliArgs {

fn main() -> Result<()> {
env_tracing_logger::init();
// braid::braid_start("braid")?;

// TODO: In case of no command given (or a query command), iterate all dirs
// on environment path, collect braid-* executables, show these as possible
Expand Down
11 changes: 2 additions & 9 deletions braid/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
use anyhow::Result;

pub use braid_config_data::BraidConfig;

pub fn braid_start(name: &str) -> Result<()> {
pub fn braid_start(_name: &str) -> Result<()> {
dotenv::dotenv().ok();

if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "braid=info,flydra2=info,braid_run=info,strand_cam=info,flydra_feature_detector=info,rt_image_viewer=info,flydra1_triggerbox=info,error");
std::env::set_var("RUST_LOG", "braid=info,flydra2=info,braid_run=info,strand_cam=info,flydra_feature_detector=info,rt_image_viewer=info,flydra1_triggerbox=info,warn");
}

env_tracing_logger::init();

let version = format!("{} (git {})", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"));
tracing::info!("{} {}", name, version);
Ok(())
}
2 changes: 1 addition & 1 deletion ci2-cli/src/ci2-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn record(mut mymod: &backend::WrappedModule, recargs: Record) -> ci2::Result<()

fn main() -> anyhow::Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "ci2=info,error");
std::env::set_var("RUST_LOG", "ci2=info,warn");
}

env_logger::init();
Expand Down
2 changes: 1 addition & 1 deletion env-tracing-logger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ rust-version = "1.60"

[dependencies]
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.16", features = ["env-filter", "time"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
50 changes: 29 additions & 21 deletions env-tracing-logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use tracing::subscriber::SetGlobalDefaultError;
use tracing_subscriber::{
fmt::{self, time},
prelude::*,
EnvFilter,
};
use tracing_subscriber::{fmt, layer::SubscriberExt};

struct Guard {}

Expand All @@ -12,23 +7,36 @@ impl Drop for Guard {
}

pub fn init() -> impl Drop {
init_result()
.map_err(|e| e.1)
.expect("Could not set global default")
initiate_logging::<&str>(None, false).unwrap()
}

fn init_result() -> Result<impl Drop, (impl Drop, tracing::subscriber::SetGlobalDefaultError)> {
let evt_fmt = tracing_subscriber::fmt::format()
.with_timer(time::Uptime::default())
.compact();
let fmt_layer = fmt::layer().event_format(evt_fmt);
/// Start logging to file and console, both optional.
pub fn initiate_logging<P: AsRef<std::path::Path>>(
path: Option<P>,
disable_console: bool,
) -> Result<impl Drop, Box<dyn std::error::Error + Send + Sync + 'static>> {
let file_layer = if let Some(path) = path {
let file = std::fs::File::create(path)?;
let file_writer = std::sync::Mutex::new(file);
Some(
tracing_subscriber::fmt::layer()
.with_writer(file_writer)
.with_ansi(false),
)
} else {
None
};

tracing_subscriber::registry()
.with(fmt_layer)
.with(EnvFilter::from_default_env())
.init();
let console_layer = if disable_console {
None
} else {
Some(fmt::layer().with_timer(tracing_subscriber::fmt::time::Uptime::default()))
};

let _guard = Guard {};

Ok::<_, (Guard, SetGlobalDefaultError)>(_guard)
let collector = tracing_subscriber::registry()
.with(file_layer)
.with(console_layer)
.with(tracing_subscriber::filter::EnvFilter::from_default_env());
tracing::subscriber::set_global_default(collector)?;
Ok(Guard {})
}
2 changes: 1 addition & 1 deletion fly-eye/src/bin/fly-eye-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn fly_eye_cli(input_image: PathBuf) -> anyhow::Result<()> {

fn main() -> anyhow::Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "fly_eye=info,error");
std::env::set_var("RUST_LOG", "fly_eye=info,warn");
}

env_logger::init();
Expand Down
2 changes: 2 additions & 0 deletions flydra-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ pub struct BraidCameraConfig {
/// in which certain characters are not allowed.)
pub name: String,
/// Filename of vendor-specific camera settings file.
///
/// Can contain shell variables such as `~`, `$A`, or `${B}`.
pub camera_settings_filename: Option<std::path::PathBuf>,
/// The pixel format to use.
pub pixel_format: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion fmf/fmf-cli/src/fmf-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ fn export_y4m(x: ExportY4m) -> Result<()> {

fn main() -> Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "fmf=info,error");
std::env::set_var("RUST_LOG", "fmf=info,warn");
}

env_logger::init();
Expand Down
2 changes: 1 addition & 1 deletion freemovr-calibration/freemovr-calibration-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn main() -> anyhow::Result<()> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var(
"RUST_LOG",
"freemovr_calibration=info,freemovr_calibration_cli=info,error",
"freemovr_calibration=info,freemovr_calibration_cli=info,warn",
);
}

Expand Down
10 changes: 5 additions & 5 deletions strand-cam/src/cli_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{run_app, BraidArgs, StandaloneArgs, StandaloneOrBraid, StrandCamArgs

use crate::APP_INFO;

use anyhow::Result;
use anyhow::{Context, Result};

pub fn cli_main<M, C, G>(
mymod: ci2_async::ThreadedAsyncCameraModule<M, C, G>,
Expand All @@ -25,13 +25,12 @@ where
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var(
"RUST_LOG",
"strand_cam=info,flydra_feature_detector=info,rt_image_viewer=info,error",
"strand_cam=info,flydra_feature_detector=info,rt_image_viewer=info,warn",
);
}

env_tracing_logger::init();
let args = parse_args(app_name).with_context(|| format!("parsing args"))?;

let args = parse_args(app_name)?;
run_app(mymod, args, app_name)
}

Expand Down Expand Up @@ -214,7 +213,8 @@ fn parse_args(app_name: &str) -> anyhow::Result<StrandCamArgs> {
};

let secret = matches
.get_one::<String>("strand_cam_cookie_secret").cloned()
.get_one::<String>("strand_cam_cookie_secret")
.cloned()
.clone();

let mkv_filename_template = matches
Expand Down
Loading

0 comments on commit e278749

Please sign in to comment.