Use macros over functions for error logging

This commit is contained in:
Neshura 2024-05-06 20:51:31 +02:00
parent 1cd30b1145
commit 36b59240d9
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
4 changed files with 107 additions and 33 deletions

View file

@ -12,8 +12,34 @@ use std::sync::Arc;
use tokio::sync::RwLock;
use tokio::time::sleep;
use crate::fetchers::Fetcher;
use systemd_journal_logger::connected_to_journal;
pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
macro_rules! info {
($msg:tt) => {
match connected_to_journal() {
true => log::info!("[INFO] {}", $msg),
false => println!("[INFO] {}", $msg),
}
};
}
macro_rules! warn {
($msg:tt) => {
match connected_to_journal() {
true => log::warn!("[WARN] {}", $msg),
false => println!("[WARN] {}", $msg),
}
};
}
macro_rules! error {
($msg:tt) => {
match connected_to_journal() {
true => log::error!("[ERROR] {}", $msg),
false => eprintln!("[ERROR] {}", $msg),
}
};
}
let mut last_reload: DateTime<Utc>;
let mut lemmy: Lemmy;
let mut login_error: bool;
@ -47,7 +73,7 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
}
let info_msg = "Bot init successful, starting normal operations".to_owned();
write_info(info_msg);
info!(info_msg);
loop {
idle(&data).await;
@ -56,8 +82,8 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
let mut write = data.write().await;
write.start = Utc::now();
if write.start - last_reload >= Duration::seconds(write.config.config_reload_seconds as i64) {
let message = "Config reloaded".to_owned();
info!(message);
write.config = Config::load();
let message = "Config reloaded".to_owned();
write_info(message);
@ -65,9 +91,9 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
}
{
let read = data.read().await;
if login_error {
lemmy = match lemmy::login(&read.config).await {
let info_msg = "Login invalid, refreshing session";
info!(info_msg);
lemmy = match lemmy::login(&config).await {
Ok(data) => data,
Err(_) => continue,
};
@ -83,9 +109,9 @@ pub(crate) async fn run(data: Arc<RwLock<SharedData>>) {
Err(_) => {
login_error = true;
continue;
}
};
let message = "Communities reloaded".to_owned();
let message = "Communities reloaded".to_owned();
info!(message);
last_reload = Utc::now();
write_info(message);
last_reload = Utc::now();
}
@ -127,7 +153,7 @@ async fn idle(data: &Arc<RwLock<SharedData>>) {
Ok(_) => {}
Err(e) => {
let err_msg = format!("{e}");
write_error(err_msg);
error!(err_msg);
}
}
};
@ -143,6 +169,11 @@ async fn handle_series(series: &SeriesConfig, communities: &HashMap<String, Comm
Ok(data) => data,
Err(_) => return Err(()),
};
if post_list.is_empty() && Utc::now().minute() % 10 == 0 {
let info_msg = "No Updates found";
info!(info_msg);
}
for post_info in post_list.clone().iter() {
// todo .clone() likely not needed
@ -190,7 +221,7 @@ async fn handle_series(series: &SeriesConfig, communities: &HashMap<String, Comm
post_lemmy_info.title.as_str(),
post_series_config.name.as_str()
);
write_info(info);
info!(info);
let post_id = lemmy.post(post_data).await?;
{
@ -199,7 +230,7 @@ async fn handle_series(series: &SeriesConfig, communities: &HashMap<String, Comm
&& !read
.config
.protected_communities
.contains(&post_series_config.name)
info!(info);
{
let info = format!(
"Pinning '{}' to {}",
@ -226,12 +257,13 @@ async fn handle_series(series: &SeriesConfig, communities: &HashMap<String, Comm
);
write_warn(message);
}
warn!(message);
}
let read = data.read().await;
if post_series_config.pin_settings.pin_new_post_local {
let info = format!("Pinning '{}' to Instance", post_lemmy_info.title);
write_info(info);
info!(info);
let pinned_posts = lemmy.get_local_pinned().await?;
if !pinned_posts.is_empty() {
for pinned_post in pinned_posts {