about summary refs log tree commit diff
path: root/kittybox-rs/src
diff options
context:
space:
mode:
Diffstat (limited to 'kittybox-rs/src')
-rw-r--r--kittybox-rs/src/main.rs42
1 files changed, 36 insertions, 6 deletions
diff --git a/kittybox-rs/src/main.rs b/kittybox-rs/src/main.rs
index 8c32556..6389489 100644
--- a/kittybox-rs/src/main.rs
+++ b/kittybox-rs/src/main.rs
@@ -258,17 +258,42 @@ where
 async fn main() {
     use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
 
-    Registry::default()
+    let tracing_registry = Registry::default()
         .with(EnvFilter::from_default_env())
-        //.with(tracing_subscriber::fmt::layer().json())
         .with(
             #[cfg(debug_assertions)]
-            tracing_tree::HierarchicalLayer::new(2),
+            tracing_tree::HierarchicalLayer::new(2)
+                .with_bracketed_fields(true)
+                .with_indent_lines(true)
+                .with_verbose_exit(true),
             #[cfg(not(debug_assertions))]
             tracing_subscriber::fmt::layer().json()
-        )
-        .init();
-    //let _ = tracing_log::LogTracer::init();
+                .with_ansi(std::io::IsTerminal::is_terminal(&std::io::stdout().lock()))
+        );
+    // In debug builds, also log to JSON, but to file.
+    #[cfg(debug_assertions)]
+    let tracing_registry = tracing_registry.with(
+        tracing_subscriber::fmt::layer()
+            .json()
+            .with_writer({
+                let instant = std::time::SystemTime::now()
+                        .duration_since(std::time::UNIX_EPOCH)
+                        .unwrap();
+                move || std::fs::OpenOptions::new()
+                    .append(true)
+                    .create(true)
+                    .open(
+                        format!(
+                            "{}.log.json",
+                            instant
+                            .as_secs_f64()
+                            .to_string()
+                            .replace('.', "_")
+                        )
+                    ).unwrap()
+            })
+    );
+    tracing_registry.init();
 
     tracing::info!("Starting the kittybox server...");
 
@@ -433,6 +458,11 @@ async fn main() {
 
             1
         }
+        _ = cancellation_token.cancelled() => {
+            tracing::info!("Signal caught from watchdog.");
+
+            0
+        }
         _ = shutdown_signal => {
             tracing::info!("Shutdown requested by signal.");
             cancellation_token.cancel();