Skip to content

Commit 73967a5

Browse files
committed
save all logs to dedicated logging directory
1 parent 330c613 commit 73967a5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

apps/desktop/src-tauri/src/main.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use std::sync::Arc;
55

66
use cap_desktop_lib::DynLoggingLayer;
7+
use dirs;
8+
use tracing_appender;
79
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
810

911
fn main() {
@@ -46,9 +48,37 @@ fn main() {
4648

4749
(sentry_client, _guard)
4850
});
51+
// Keep the guard alive for the duration of the program
52+
std::mem::forget(_guard);
4953

5054
let (layer, handle) = tracing_subscriber::reload::Layer::new(None::<DynLoggingLayer>);
5155

56+
let logs_dir = {
57+
#[cfg(target_os = "macos")]
58+
let path = dirs::home_dir()
59+
.unwrap()
60+
.join("Library/Logs")
61+
.join("so.cap.desktop");
62+
63+
#[cfg(not(target_os = "macos"))]
64+
let path = dirs::data_local_dir()
65+
.unwrap()
66+
.join("so.cap.desktop")
67+
.join("logs");
68+
69+
path
70+
};
71+
72+
// Ensure logs directory exists
73+
std::fs::create_dir_all(&logs_dir).unwrap_or_else(|e| {
74+
eprintln!("Failed to create logs directory: {}", e);
75+
});
76+
77+
let file_appender = tracing_appender::rolling::daily(&logs_dir, "cap-desktop.log");
78+
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
79+
// Keep the guard alive for the duration of the program
80+
std::mem::forget(_guard);
81+
5282
let registry = tracing_subscriber::registry().with(tracing_subscriber::filter::filter_fn(
5383
(|v| v.target().starts_with("cap_")) as fn(&tracing::Metadata) -> bool,
5484
));
@@ -60,6 +90,12 @@ fn main() {
6090
.with_ansi(true)
6191
.with_target(true),
6292
)
93+
.with(
94+
tracing_subscriber::fmt::layer()
95+
.with_ansi(false)
96+
.with_target(true)
97+
.with_writer(non_blocking),
98+
)
6399
.init();
64100

65101
#[cfg(debug_assertions)]

0 commit comments

Comments
 (0)