use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Log<F, L> {
pub file: F, pub line: L, pub time: String, pub msg: String, }
#[macro_export]
macro_rules! json_logger {
($msg:expr) => {{
use chrono::Local;
use std::fs::OpenOptions;
use std::io::Write;
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
let log_entry = $crate::tiny_macros::Log {
file: file!(),
line: line!(),
time: timestamp,
msg: $msg.to_string(),
};
let json_log = serde_json::to_string_pretty(&log_entry).unwrap();
let bin_name = env!("CARGO_PKG_NAME");
let log_dir = dirs::cache_dir()
.unwrap_or(dirs::data_dir().unwrap())
.join(bin_name);
if !log_dir.exists() {
std::fs::create_dir_all(&log_dir).unwrap();
}
let log_file = &log_dir.join("log.json");
if !log_file.exists() {
std::fs::write(log_file, "").unwrap();
}
let log_file_path = log_file;
let mut file = OpenOptions::new()
.create(true) .append(true) .open(log_file_path)
.unwrap();
writeln!(file, "{}", json_log).unwrap(); }};
}
#[macro_export]
#[cfg(feature = "debug")]
macro_rules! dprintln {
($($arg:tt)*) => (println!($($arg)*));
}
#[macro_export]
#[cfg(not(feature = "debug"))]
macro_rules! dprintln {
($($arg:tt)*) => {};
}
#[macro_export]
#[cfg(feature = "debug")]
macro_rules! dprint {
($($arg:tt)*) => (print!($($arg)*));
}
#[macro_export]
#[cfg(not(feature = "debug"))]
macro_rules! dprint {
($($arg:tt)*) => {};
}
#[macro_export]
macro_rules! verbose {
($verbose_flag:expr, $($arg:tt)*) => {
if $verbose_flag {
println!($($arg)*);
}
};
}
#[macro_export]
macro_rules! dev_debug {
($verbose_flag:expr, $($arg:tt)*) => {
if $verbose_flag {
println!("[{}:{}] {}", file!(), line!(), format!($($arg)*));
}
};
}
#[macro_export]
macro_rules! show_error(
($($args:tt)+) => ({
eprint!("{}: ", env!("CARGO_PKG_NAME"));
eprintln!($($args)+);
})
);
#[macro_export]
macro_rules! prompt_yes {
($($arg:tt)*) => {{
use std::io::{self, Write};
print!("{}: ", env!("CARGO_PKG_NAME"));
print!($($arg)*);
print!(" "); io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
matches!(input.trim().to_lowercase().as_str(), "yes" | "y")
}};
}