Skip to content

Commit

Permalink
feat(tracing): add initial tracing to ~/.local/share/ducker/ducker.log
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpsoane committed Jul 14, 2024
1 parent 9548535 commit 7222510
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 8 deletions.
116 changes: 114 additions & 2 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dirs-next = "2.0.0"
dyn-clone = "1.0.17"
futures = "0.3.30"
itertools = "0.13.0"
lazy_static = "1.5.0"
ratatui = { version = "0.27.0", features = [
"serde",
"unstable-rendered-line-info",
Expand All @@ -38,6 +39,9 @@ tokio = { version = "1.38.0", features = [
"macros",
"process",
] }
tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tui-big-text = "0.4.5"


Expand Down
3 changes: 0 additions & 3 deletions ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ The "issues" listed here are the aspirational set of features I have in mind, so

Feel free to raise issues in github.

- Option to control container log time ranges
- Add tracing of some sort
- Automated test strategy; perhaps obviously there aren't any tests in the repo. This is due to the way in which the project started, but probably needs to change sooner rather than later!
- Add volumes as a first class citizen
- Add networks as a first class citizen
- Modals should use a general purpose trait object of some sort - preferably only one modal field per page, in a similar way to pages in the page manager
- Support for "forms" of scrolling stateful widgets (TODO - look out for ratatui libraries that already support this)
- Run Images - Given an image, run a new container; provide a form/modal to allow user to configure the container
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl Default for Theme {
}
}

fn get_app_config_path() -> Result<std::path::PathBuf> {
pub fn get_app_config_path() -> Result<std::path::PathBuf> {
let path = if cfg!(target_os = "macos") {
dirs_next::home_dir().map(|h| h.join(".config"))
} else {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod events;
pub mod pages;
pub mod state;
pub mod terminal;
pub mod tracing;
pub mod traits;
pub mod ui;
pub mod widgets;
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use color_eyre::eyre::Context;
use ducker::{
config::Config,
docker::util::new_local_docker_connection,
events,
events::{EventLoop, Key, Message},
events::{self, EventLoop, Key, Message},
state, terminal,
tracing::initialize_logging,
ui::App,
};

Expand All @@ -28,6 +28,8 @@ struct Args {

#[tokio::main]
async fn main() -> color_eyre::Result<()> {
initialize_logging().context("failed to initialise logging")?;
color_eyre::install()?;
let args = Args::parse();
let config = Config::new(&args.export_default_config, args.docker_path)?;

Expand Down
84 changes: 84 additions & 0 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::path::PathBuf;

use color_eyre::eyre::Result;
use lazy_static::lazy_static;
use tracing::info;
use tracing_error::ErrorLayer;
use tracing_subscriber::{self, layer::SubscriberExt, util::SubscriberInitExt};

use crate::config::get_app_config_path;

// taken form https://ratatui.rs/recipes/apps/log-with-tracing/

lazy_static! {
pub static ref PROJECT_NAME: String = env!("CARGO_CRATE_NAME").to_uppercase().to_string();
pub static ref LOG_ENV: String = format!("{}_LOGLEVEL", PROJECT_NAME.clone());
pub static ref LOG_FILE: String = format!("{}.log", env!("CARGO_PKG_NAME"));
}

fn project_directory() -> Option<PathBuf> {
dirs_next::data_dir().map(|data_dir| data_dir.join(env!("CARGO_CRATE_NAME")))
}

pub fn get_log_dir() -> PathBuf {
if let Some(p) = project_directory() {
p
} else if let Ok(p) = get_app_config_path() {
p
} else {
PathBuf::from(".").join(".data")
}
}

pub fn initialize_logging() -> Result<()> {
let directory = get_log_dir();
std::fs::create_dir_all(directory.clone())?;
let log_path = directory.join(LOG_FILE.clone());
let log_file = std::fs::File::create(log_path)?;
std::env::set_var(
"RUST_LOG",
std::env::var("RUST_LOG")
.or_else(|_| std::env::var(LOG_ENV.clone()))
.unwrap_or_else(|_| format!("{}=info", env!("CARGO_CRATE_NAME"))),
);
let file_subscriber = tracing_subscriber::fmt::layer()
.with_file(true)
.with_line_number(true)
.with_writer(log_file)
.with_target(false)
.with_ansi(false);
tracing_subscriber::registry()
.with(file_subscriber)
.with(ErrorLayer::default())
.with(tracing_subscriber::filter::EnvFilter::from_default_env())
.init();

info!("logging initialised");
Ok(())
}

/// Similar to the `std::dbg!` macro, but generates `tracing` events rather
/// than printing to stdout.
///
/// By default, the verbosity level for the generated events is `DEBUG`, but
/// this can be customized.
#[macro_export]
macro_rules! trace_dbg {
(target: $target:expr, level: $level:expr, $ex:expr) => {{
match $ex {
value => {
tracing::event!(target: $target, $level, ?value, stringify!($ex));
value
}
}
}};
(level: $level:expr, $ex:expr) => {
trace_dbg!(target: module_path!(), level: $level, $ex)
};
(target: $target:expr, $ex:expr) => {
trace_dbg!(target: $target, level: tracing::Level::DEBUG, $ex)
};
($ex:expr) => {
trace_dbg!(level: tracing::Level::DEBUG, $ex)
};
}

0 comments on commit 7222510

Please sign in to comment.