feat: move logs to new module
This commit is contained in:
parent
2a7e30708a
commit
bcf986ff1f
3 changed files with 44 additions and 37 deletions
|
|
@ -30,7 +30,7 @@ WorkingDirectory=/home/noisebridge/noisebell
|
||||||
Environment=DISCORD_TOKEN=${DISCORD_TOKEN}
|
Environment=DISCORD_TOKEN=${DISCORD_TOKEN}
|
||||||
Environment=DISCORD_CHANNEL_ID=${DISCORD_CHANNEL_ID}
|
Environment=DISCORD_CHANNEL_ID=${DISCORD_CHANNEL_ID}
|
||||||
ExecStart=/home/noisebridge/noisebell/noisebell
|
ExecStart=/home/noisebridge/noisebell/noisebell
|
||||||
Restart=always
|
Restart=on-failure
|
||||||
RestartSec=10
|
RestartSec=10
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
|
|
@ -38,14 +38,12 @@ WantedBy=multi-user.target
|
||||||
EOL
|
EOL
|
||||||
|
|
||||||
echo "Copying to Raspberry Pi..."
|
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
|
# 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/
|
scp noisebell.service noisebridge@noisebell.local:~/noisebell/
|
||||||
|
|
||||||
echo "Setting up service..."
|
echo "Setting up service..."
|
||||||
|
# Deploy service
|
||||||
ssh noisebridge@noisebell.local "sudo cp ~/noisebell/noisebell.service /etc/systemd/system/ && \
|
ssh noisebridge@noisebell.local "sudo cp ~/noisebell/noisebell.service /etc/systemd/system/ && \
|
||||||
sudo systemctl daemon-reload && \
|
sudo systemctl daemon-reload && \
|
||||||
sudo systemctl enable noisebell && \
|
sudo systemctl enable noisebell && \
|
||||||
|
|
|
||||||
39
src/logging.rs
Normal file
39
src/logging.rs
Normal file
|
|
@ -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(())
|
||||||
|
}
|
||||||
34
src/main.rs
34
src/main.rs
|
|
@ -1,50 +1,20 @@
|
||||||
mod gpio;
|
mod gpio;
|
||||||
mod discord;
|
mod discord;
|
||||||
|
mod logging;
|
||||||
|
|
||||||
use std::fs;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use tracing::{error, info};
|
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_GPIO_PIN: u8 = 17;
|
||||||
const DEFAULT_POLL_INTERVAL_MS: u64 = 100;
|
const DEFAULT_POLL_INTERVAL_MS: u64 = 100;
|
||||||
const DEFAULT_DEBOUNCE_DELAY_SECS: u64 = 5;
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
info!("creating logs directory");
|
logging::init()?;
|
||||||
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();
|
|
||||||
|
|
||||||
info!("initializing Discord client");
|
info!("initializing Discord client");
|
||||||
let discord_client = discord::DiscordClient::new().await?;
|
let discord_client = discord::DiscordClient::new().await?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue