use crate::*;
impl<F, T> FileLoggerFuncTrait<T> for F
where
F: Fn(T) -> String + Send + Sync,
T: AsRef<str>,
{
}
impl Default for FileLogger {
#[inline(always)]
fn default() -> Self {
Self {
path: DEFAULT_LOG_DIR.to_owned(),
limit_file_size: DEFAULT_LOG_FILE_SIZE,
trace_dir: TRACE_DIR.to_owned(),
debug_dir: DEBUG_DIR.to_owned(),
info_dir: INFO_DIR.to_owned(),
warn_dir: WARN_DIR.to_owned(),
error_dir: ERROR_DIR.to_owned(),
}
}
}
impl FileLogger {
#[inline(always)]
pub fn new<P: AsRef<str>>(path: P, limit_file_size: usize) -> Self {
Self {
path: path.as_ref().to_owned(),
limit_file_size,
trace_dir: TRACE_DIR.to_owned(),
debug_dir: DEBUG_DIR.to_owned(),
info_dir: INFO_DIR.to_owned(),
warn_dir: WARN_DIR.to_owned(),
error_dir: ERROR_DIR.to_owned(),
}
}
#[inline(always)]
pub fn get_path(&self) -> &String {
&self.path
}
#[inline(always)]
pub fn get_limit_file_size(&self) -> &usize {
&self.limit_file_size
}
#[inline(always)]
pub fn get_trace_dir(&self) -> &String {
&self.trace_dir
}
#[inline(always)]
pub fn get_debug_dir(&self) -> &String {
&self.debug_dir
}
#[inline(always)]
pub fn get_info_dir(&self) -> &String {
&self.info_dir
}
#[inline(always)]
pub fn get_warn_dir(&self) -> &String {
&self.warn_dir
}
#[inline(always)]
pub fn get_error_dir(&self) -> &String {
&self.error_dir
}
#[inline(always)]
pub fn set_path<P: AsRef<str>>(&mut self, path: P) -> &mut Self {
self.path = path.as_ref().to_owned();
self
}
#[inline(always)]
pub fn set_limit_file_size(&mut self, limit_file_size: usize) -> &mut Self {
self.limit_file_size = limit_file_size;
self
}
#[inline(always)]
pub fn set_trace_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
self.trace_dir = dir.as_ref().to_owned();
self
}
#[inline(always)]
pub fn set_debug_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
self.debug_dir = dir.as_ref().to_owned();
self
}
#[inline(always)]
pub fn set_info_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
self.info_dir = dir.as_ref().to_owned();
self
}
#[inline(always)]
pub fn set_warn_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
self.warn_dir = dir.as_ref().to_owned();
self
}
#[inline(always)]
pub fn set_error_dir<P: AsRef<str>>(&mut self, dir: P) -> &mut Self {
self.error_dir = dir.as_ref().to_owned();
self
}
#[inline(always)]
pub fn is_enable(&self) -> bool {
self.limit_file_size != DISABLE_LOG_FILE_SIZE
}
#[inline(always)]
pub fn is_disable(&self) -> bool {
!self.is_enable()
}
fn write_sync<T, L>(&self, data: T, func: L, dir: &str) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
if self.is_disable() {
return self;
}
let out: String = func(data);
let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
let _ = append_to_file(&path, out.as_bytes());
self
}
async fn write_async<T, L>(&self, data: T, func: L, dir: &str) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
if self.is_disable() {
return self;
}
let out: String = func(data);
let path: String = get_log_path(dir, &self.path, &self.limit_file_size);
let _ = async_append_to_file(&path, out.as_bytes()).await;
self
}
pub fn trace<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_sync(data, func, &self.trace_dir)
}
pub async fn async_trace<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_async(data, func, &self.trace_dir).await
}
pub fn debug<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_sync(data, func, &self.debug_dir)
}
pub async fn async_debug<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_async(data, func, &self.debug_dir).await
}
pub fn info<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_sync(data, func, &self.info_dir)
}
pub async fn async_info<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_async(data, func, &self.info_dir).await
}
pub fn warn<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_sync(data, func, &self.warn_dir)
}
pub async fn async_warn<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_async(data, func, &self.warn_dir).await
}
pub fn error<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_sync(data, func, &self.error_dir)
}
pub async fn async_error<T, L>(&self, data: T, func: L) -> &Self
where
T: AsRef<str>,
L: FileLoggerFuncTrait<T>,
{
self.write_async(data, func, &self.error_dir).await
}
}