From d30e0b270ec0317a25e7c3d9f5ecc01424d0ec24 Mon Sep 17 00:00:00 2001 From: Arshdeep54 Date: Wed, 26 Feb 2025 16:47:18 +0530 Subject: [PATCH] add debug flag in logging Signed-off-by: Arshdeep54 --- sample.config.toml | 4 ++++ src/lib/config.rs | 10 ++++++++++ src/lib/logger.rs | 20 ++++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/sample.config.toml b/sample.config.toml index 55644cd..d861a3d 100644 --- a/sample.config.toml +++ b/sample.config.toml @@ -22,3 +22,7 @@ token = 'secret_token' # app(https://slack.com/apps/A0F7XDUAZ-incoming-webhooks) # and paste the hook URL here. You can customize the icon and name as you like. slack = 'https://hooks.slack.com/services/ABCDEFGHI/ABCDEFGHI/abcdefghijklmnopqrstuvwx' + +# Logging +[logging] +debug=true \ No newline at end of file diff --git a/src/lib/config.rs b/src/lib/config.rs index fcc23d2..edb5713 100644 --- a/src/lib/config.rs +++ b/src/lib/config.rs @@ -16,11 +16,17 @@ pub struct NotifiersConf { pub slack: String, } +#[derive(Deserialize, Clone)] +pub struct LoggingConf { + pub debug: bool, +} + #[derive(Deserialize, Clone)] pub struct Config { pub hostname: String, pub keyhouse: KeyhouseConf, pub notifiers: NotifiersConf, + pub logging: LoggingConf, } pub fn read_config() -> Result { @@ -47,6 +53,9 @@ pub fn set_config_value(key: &str, val: &str) -> Result<()> { "notifiers.slack" => { doc["notifiers"]["slack"] = value(val); } + "logging.debug" => { + doc["logging"]["debug"] = value(val); + } _ => { return Err("Invalid Key passed".into()); } @@ -65,6 +74,7 @@ pub fn get_config_value(key: &str) -> Result { "keyhouse.base_url" => doc["keyhouse"]["base_url"].as_str(), "keyhouse.token" => doc["keyhouse"]["token"].as_str(), "notifiers.slack" => doc["notifiers"]["slack"].as_str(), + "logging.debug" => doc["logging"]["debug"].as_str(), _ => { return Err("Invalid Key passed".into()); } diff --git a/src/lib/logger.rs b/src/lib/logger.rs index 3bb3ac8..00fe88e 100644 --- a/src/lib/logger.rs +++ b/src/lib/logger.rs @@ -3,6 +3,7 @@ use std::io::Write; use std::time::{SystemTime, UNIX_EPOCH}; use std::io::Result; use chrono::{DateTime, Utc}; +use crate::config::{read_config, Config}; pub fn log(filepath: &str, status: &str, message: &str) -> Result<()> { let start = SystemTime::now(); @@ -22,6 +23,18 @@ pub fn log(filepath: &str, status: &str, message: &str) -> Result<()> { } pub fn logln(message: &str) { + let config = match read_config(){ + Ok(config) => config, + Err(_) => { + log("/opt/watchdog/custom-logs/watchdog.logs", "FAILURE", "Failed to read config").expect("Failed to log"); + return; + }, + }; + let debug=get_debug(&config); + if debug==false { + log("/opt/watchdog/custom-logs/watchdog.logs", "FAILURE", "debug false in logln").expect("Failed to log"); + return; + } let start = SystemTime::now(); let since_the_epoch = start.duration_since(UNIX_EPOCH).expect("Time went backwards"); let timestamp = since_the_epoch.as_secs(); @@ -35,6 +48,9 @@ pub fn logln(message: &str) { .create(true) .open(filepath).expect("Failed to open log file"); - file.write_all(log_message.as_bytes()).expect("Failed to write to log file"); - } \ No newline at end of file +} + +pub fn get_debug(config: &Config) -> bool { + config.logging.debug +} \ No newline at end of file