logging.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. use std::fs::File;
  2. use std::io::{BufRead, BufReader};
  3. use std::path::{Path, PathBuf};
  4. use tracing_appender::non_blocking::WorkerGuard;
  5. use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
  6. const MAX_LOG_AGE_DAYS: u64 = 7;
  7. const TAIL_LINES: usize = 1000;
  8. static LOG_PATH: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
  9. pub fn init(log_dir: &Path) -> WorkerGuard {
  10. std::fs::create_dir_all(log_dir).expect("failed to create log directory");
  11. cleanup(log_dir);
  12. let timestamp = chrono::Local::now().format("%Y-%m-%d_%H-%M-%S");
  13. let filename = format!("opencode-desktop_{timestamp}.log");
  14. let log_path = log_dir.join(&filename);
  15. LOG_PATH
  16. .set(log_path.clone())
  17. .expect("logging already initialized");
  18. let file = File::create(&log_path).expect("failed to create log file");
  19. let (non_blocking, guard) = tracing_appender::non_blocking(file);
  20. let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
  21. if cfg!(debug_assertions) {
  22. EnvFilter::new("opencode_lib=debug,opencode_desktop=debug,sidecar=debug")
  23. } else {
  24. EnvFilter::new("opencode_lib=info,opencode_desktop=info,sidecar=info")
  25. }
  26. });
  27. tracing_subscriber::registry()
  28. .with(filter)
  29. .with(fmt::layer().with_writer(std::io::stderr))
  30. .with(fmt::layer().with_writer(non_blocking).with_ansi(false))
  31. .init();
  32. guard
  33. }
  34. pub fn tail() -> String {
  35. let Some(path) = LOG_PATH.get() else {
  36. return String::new();
  37. };
  38. let Ok(file) = File::open(path) else {
  39. return String::new();
  40. };
  41. let lines: Vec<String> = BufReader::new(file).lines().map_while(Result::ok).collect();
  42. let start = lines.len().saturating_sub(TAIL_LINES);
  43. lines[start..].join("\n")
  44. }
  45. fn cleanup(log_dir: &Path) {
  46. let cutoff = std::time::SystemTime::now()
  47. - std::time::Duration::from_secs(MAX_LOG_AGE_DAYS * 24 * 60 * 60);
  48. let Ok(entries) = std::fs::read_dir(log_dir) else {
  49. return;
  50. };
  51. for entry in entries.flatten() {
  52. if let Ok(meta) = entry.metadata()
  53. && let Ok(modified) = meta.modified()
  54. && modified < cutoff
  55. {
  56. let _ = std::fs::remove_file(entry.path());
  57. }
  58. }
  59. }