write log files to disk daily and on panic

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-10-18 16:07:01 -04:00
parent 4f86e9604f
commit d3b4a9efc1
4 changed files with 74 additions and 1 deletions

22
Cargo.lock generated
View File

@@ -917,6 +917,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@@ -2568,6 +2577,7 @@ dependencies = [
"tempfile",
"tokio",
"tracing",
"tracing-appender",
"tracing-subscriber",
"tracing-wasm",
"uuid",
@@ -4427,6 +4437,18 @@ dependencies = [
"tracing-core",
]
[[package]]
name = "tracing-appender"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf"
dependencies = [
"crossbeam-channel",
"thiserror",
"time",
"tracing-subscriber",
]
[[package]]
name = "tracing-attributes"
version = "0.1.27"

View File

@@ -44,6 +44,7 @@ bitflags = "2.5.0"
uuid = { version = "1.10.0", features = ["v4"] }
indexmap = "2.6.0"
dirs = "5.0.1"
tracing-appender = "0.2.3"
[dev-dependencies]
tempfile = "3.13.0"

View File

@@ -11,8 +11,57 @@ use notedeck::Damus;
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() {
#[allow(unused_variables)] // need guard to live for lifetime of program
let (maybe_non_blocking, maybe_guard) =
if let Ok(log_path) = notedeck::DataPaths::Log.get_path() {
// Setup logging to file
use std::panic;
use tracing::error;
use tracing_appender::{
non_blocking,
rolling::{RollingFileAppender, Rotation},
};
let file_appender = RollingFileAppender::new(
Rotation::DAILY,
log_path,
format!("notedeck-{}.log", env!("CARGO_PKG_VERSION")),
);
panic::set_hook(Box::new(|panic_info| {
error!("Notedeck panicked: {:?}", panic_info);
}));
let (non_blocking, _guard) = non_blocking(file_appender);
(Some(non_blocking), Some(_guard))
} else {
(None, None)
};
// Log to stdout (if you run with `RUST_LOG=debug`).
tracing_subscriber::fmt::init();
if let Some(non_blocking_writer) = maybe_non_blocking {
use tracing::Level;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};
let console_layer = fmt::layer().with_target(true).with_writer(std::io::stdout);
// Create the file layer (writes to the file)
let file_layer = fmt::layer()
.with_ansi(false)
.with_writer(non_blocking_writer);
// Set up the subscriber to combine both layers
tracing_subscriber::registry()
.with(console_layer)
.with(file_layer)
.with(tracing_subscriber::filter::LevelFilter::from_level(
Level::INFO,
)) // Set log level
.init();
} else {
tracing_subscriber::fmt::init();
}
let _res = eframe::run_native(
"Damus NoteDeck",

View File

@@ -7,6 +7,7 @@ mod abbrev;
pub mod account_manager;
mod actionbar;
pub mod app_creation;
mod app_size_handler;
mod app_style;
mod args;
mod colors;