From bcf986ff1f250f852fea3666d53175ada4a90820 Mon Sep 17 00:00:00 2001 From: Jet Pham <55770902+jetpham@users.noreply.github.com> Date: Sat, 7 Jun 2025 23:33:24 -0700 Subject: [PATCH] feat: move logs to new module --- deploy.sh | 8 +++----- src/logging.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 34 ++-------------------------------- 3 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 src/logging.rs diff --git a/deploy.sh b/deploy.sh index 77aa09c..7996e93 100755 --- a/deploy.sh +++ b/deploy.sh @@ -30,7 +30,7 @@ WorkingDirectory=/home/noisebridge/noisebell Environment=DISCORD_TOKEN=${DISCORD_TOKEN} Environment=DISCORD_CHANNEL_ID=${DISCORD_CHANNEL_ID} ExecStart=/home/noisebridge/noisebell/noisebell -Restart=always +Restart=on-failure RestartSec=10 [Install] @@ -38,14 +38,12 @@ WantedBy=multi-user.target EOL echo "Copying to Raspberry Pi..." -# Stop the service if it's running -ssh noisebridge@noisebell.local "sudo systemctl stop noisebell || true" -sleep 1 # Copy files -scp target/aarch64-unknown-linux-gnu/release/noisebell noisebridge@noisebell.local:~/noisebell/ +ssh noisebridge@noisebell.local "mkdir -p ~/noisebell" && scp target/aarch64-unknown-linux-gnu/release/noisebell noisebridge@noisebell.local:~/noisebell/ scp noisebell.service noisebridge@noisebell.local:~/noisebell/ echo "Setting up service..." +# Deploy service ssh noisebridge@noisebell.local "sudo cp ~/noisebell/noisebell.service /etc/systemd/system/ && \ sudo systemctl daemon-reload && \ sudo systemctl enable noisebell && \ diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 0000000..7ad5cf8 --- /dev/null +++ b/src/logging.rs @@ -0,0 +1,39 @@ +use std::fs; +use anyhow::Result; +use tracing_appender::rolling::{RollingFileAppender, Rotation}; +use tracing_subscriber::filter::LevelFilter; +use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt}; + +const LOG_DIR: &str = "logs"; +const LOG_PREFIX: &str = "noisebell"; +const LOG_SUFFIX: &str = "log"; +const MAX_LOG_FILES: usize = 7; + +pub fn init() -> Result<()> { + tracing::info!("creating logs directory"); + fs::create_dir_all(LOG_DIR)?; + + tracing::info!("initializing logging"); + let file_appender = RollingFileAppender::builder() + .rotation(Rotation::DAILY) + .filename_prefix(LOG_PREFIX) + .filename_suffix(LOG_SUFFIX) + .max_log_files(MAX_LOG_FILES) + .build(LOG_DIR)?; + + let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); + + // Only show our logs and hide hyper logs + let filter = tracing_subscriber::filter::Targets::new() + .with_target("noisebell", LevelFilter::INFO) + .with_target("hyper", LevelFilter::WARN) + .with_target("hyper_util", LevelFilter::WARN); + + tracing_subscriber::registry() + .with(filter) + .with(fmt::Layer::default().with_writer(std::io::stdout)) + .with(fmt::Layer::default().with_writer(non_blocking)) + .init(); + + Ok(()) +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9c2fa04..fb496bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,50 +1,20 @@ mod gpio; mod discord; +mod logging; -use std::fs; use std::time::Duration; use std::sync::Arc; use anyhow::Result; use tracing::{error, info}; -use tracing_appender::rolling::{RollingFileAppender, Rotation}; -use tracing_subscriber::filter::LevelFilter; -use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt}; const DEFAULT_GPIO_PIN: u8 = 17; const DEFAULT_POLL_INTERVAL_MS: u64 = 100; const DEFAULT_DEBOUNCE_DELAY_SECS: u64 = 5; -const LOG_DIR: &str = "logs"; -const LOG_PREFIX: &str = "noisebell"; -const LOG_SUFFIX: &str = "log"; -const MAX_LOG_FILES: usize = 7; #[tokio::main] async fn main() -> Result<()> { - info!("creating logs directory"); - fs::create_dir_all(LOG_DIR)?; - - info!("initializing logging"); - let file_appender = RollingFileAppender::builder() - .rotation(Rotation::DAILY) - .filename_prefix(LOG_PREFIX) - .filename_suffix(LOG_SUFFIX) - .max_log_files(MAX_LOG_FILES) - .build(LOG_DIR)?; - - let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); - - // Only show our logs and hide hyper logs - let filter = tracing_subscriber::filter::Targets::new() - .with_target("noisebell", LevelFilter::INFO) - .with_target("hyper", LevelFilter::WARN) - .with_target("hyper_util", LevelFilter::WARN); - - tracing_subscriber::registry() - .with(filter) - .with(fmt::Layer::default().with_writer(std::io::stdout)) - .with(fmt::Layer::default().with_writer(non_blocking)) - .init(); + logging::init()?; info!("initializing Discord client"); let discord_client = discord::DiscordClient::new().await?;