introduce logging to important functions

This commit is contained in:
mykola2312 2024-02-26 22:46:08 +02:00
parent 8ea0c31ff6
commit c3a97c74e3
6 changed files with 20 additions and 18 deletions

View file

@ -106,6 +106,7 @@ async fn bot_download(bot: Bot, msg: Message, url: String) -> HandlerResult {
let output_path = match download(url.as_str()).await { let output_path = match download(url.as_str()).await {
Ok(path) => path, Ok(path) => path,
Err(e) => { Err(e) => {
event!(Level::ERROR, "{}", e.to_string());
bot.send_message(msg.chat.id, e.to_string()).await?; bot.send_message(msg.chat.id, e.to_string()).await?;
return Ok(()); return Ok(());
} }

View file

@ -17,7 +17,9 @@ pub fn log_init() {
.with_writer(io::stderr) .with_writer(io::stderr)
.with_filter(LevelFilter::ERROR); .with_filter(LevelFilter::ERROR);
let file_layer = fmt::layer().with_writer(log_appender); let file_layer = fmt::layer()
.with_writer(log_appender)
.with_filter(LevelFilter::INFO);
tracing_subscriber::registry() tracing_subscriber::registry()
.with(stderr_layer) .with(stderr_layer)

View file

@ -16,12 +16,3 @@ pub fn make_database_url() -> String {
let path = Path::new(VAR_LIB).join("mk-dl-bot.db"); let path = Path::new(VAR_LIB).join("mk-dl-bot.db");
format!("sqlite://{}", path.as_os_str().to_str().unwrap()).to_string() format!("sqlite://{}", path.as_os_str().to_str().unwrap()).to_string()
} }
pub fn make_log_path() -> String {
Path::new(VAR_LOG)
.join("mk-dl-bot.log")
.as_os_str()
.to_str()
.unwrap()
.to_string()
}

View file

@ -1,8 +1,6 @@
use std::fmt; use std::fmt;
use std::fs; use std::fs;
use std::path::Path; use tracing::{event, Level};
use teloxide::types::Message;
use self::spawn::SpawnError; use self::spawn::SpawnError;
use self::yt_dlp::{YtDlp, YtDlpError, YtDlpFormat, YtDlpInfo}; use self::yt_dlp::{YtDlp, YtDlpError, YtDlpFormat, YtDlpInfo};
@ -60,11 +58,15 @@ fn file_exists(path: &str) -> bool {
pub fn delete_if_exists(path: &str) { pub fn delete_if_exists(path: &str) {
if file_exists(path) { if file_exists(path) {
fs::remove_file(path); if let Err(e) = fs::remove_file(path) {
event!(Level::ERROR, "{}", e);
}
} }
} }
pub async fn download(url: &str) -> Result<String, DownloadError> { pub async fn download(url: &str) -> Result<String, DownloadError> {
event!(Level::INFO, "url {}", url);
let info = YtDlp::load_info(url).await?; let info = YtDlp::load_info(url).await?;
let av = match info.best_av_format() { let av = match info.best_av_format() {
Some(av) => av, Some(av) => av,

View file

@ -36,8 +36,7 @@ impl fmt::Display for SpawnError {
/* !!! The argument list could be exploited in a way to inject malicious arguments !!! /* !!! The argument list could be exploited in a way to inject malicious arguments !!!
!!! and alter the way program executes and/or gain access to system !!! */ !!! and alter the way program executes and/or gain access to system !!! */
pub async fn spawn(program: &str, args: &[&str]) -> Result<Output, SpawnError> pub async fn spawn(program: &str, args: &[&str]) -> Result<Output, SpawnError> {
{
{ {
let cmd_args = args.join(" "); let cmd_args = args.join(" ");
event!(Level::INFO, "{} {}", program, cmd_args); event!(Level::INFO, "{} {}", program, cmd_args);

View file

@ -4,6 +4,7 @@ use ordered_float::OrderedFloat;
use serde::Deserialize; use serde::Deserialize;
use serde_json; use serde_json;
use std::fs; use std::fs;
use tracing::{event, Level};
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct YtDlpFormat { pub struct YtDlpFormat {
@ -106,7 +107,10 @@ impl YtDlpInfo {
match format { match format {
Some(vf) => Some(vf.format), Some(vf) => Some(vf.format),
None => None, None => {
event!(Level::ERROR, "no av format for {}", self.id);
None
}
} }
} }
@ -124,7 +128,10 @@ impl YtDlpInfo {
match format { match format {
Some(af) => Some(af.format), Some(af) => Some(af.format),
None => None, None => {
event!(Level::ERROR, "no audio format for {}", self.id);
None
}
} }
} }
} }