Merge remote-tracking branch 'origin/dev' into next

This commit is contained in:
FabianLars
2023-04-26 16:56:03 +02:00
76 changed files with 1846 additions and 914 deletions
+48 -3
View File
@@ -21,6 +21,7 @@ use tauri::{
};
pub use fern;
use time::OffsetDateTime;
#[cfg(target_os = "ios")]
mod ios {
@@ -56,6 +57,7 @@ mod ios {
const DEFAULT_MAX_FILE_SIZE: u128 = 40000;
const DEFAULT_ROTATION_STRATEGY: RotationStrategy = RotationStrategy::KeepOne;
const DEFAULT_TIMEZONE_STRATEGY: TimezoneStrategy = TimezoneStrategy::UseUtc;
const DEFAULT_LOG_TARGETS: [LogTarget; 2] = [LogTarget::Stdout, LogTarget::LogDir];
/// An enum representing the available verbosity levels of the logger.
@@ -115,6 +117,23 @@ pub enum RotationStrategy {
KeepOne,
}
#[derive(Debug, Clone)]
pub enum TimezoneStrategy {
UseUtc,
UseLocal,
}
impl TimezoneStrategy {
pub fn get_now(&self) -> OffsetDateTime {
match self {
TimezoneStrategy::UseUtc => OffsetDateTime::now_utc(),
TimezoneStrategy::UseLocal => {
OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc())
} // Fallback to UTC since Rust cannot determine local timezone
}
}
}
#[derive(Debug, Serialize, Clone)]
struct RecordPayload {
message: String,
@@ -177,6 +196,7 @@ fn log(
pub struct Builder {
dispatch: fern::Dispatch,
rotation_strategy: RotationStrategy,
timezone_strategy: TimezoneStrategy,
max_file_size: u128,
targets: Vec<LogTarget>,
}
@@ -194,7 +214,7 @@ impl Default for Builder {
#[cfg(desktop)]
format_args!(
"{}[{}][{}] {}",
time::OffsetDateTime::now_utc().format(&format).unwrap(),
DEFAULT_TIMEZONE_STRATEGY.get_now().format(&format).unwrap(),
record.target(),
record.level(),
message
@@ -204,6 +224,7 @@ impl Default for Builder {
Self {
dispatch,
rotation_strategy: DEFAULT_ROTATION_STRATEGY,
timezone_strategy: DEFAULT_TIMEZONE_STRATEGY,
max_file_size: DEFAULT_MAX_FILE_SIZE,
targets: DEFAULT_LOG_TARGETS.into(),
}
@@ -220,6 +241,24 @@ impl Builder {
self
}
pub fn timezone_strategy(mut self, timezone_strategy: TimezoneStrategy) -> Self {
self.timezone_strategy = timezone_strategy.clone();
let format =
time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]")
.unwrap();
self.dispatch = fern::Dispatch::new().format(move |out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
timezone_strategy.get_now().format(&format).unwrap(),
record.target(),
record.level(),
message
))
});
self
}
pub fn max_file_size(mut self, max_file_size: u128) -> Self {
self.max_file_size = max_file_size;
self
@@ -266,10 +305,12 @@ impl Builder {
let format =
time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]")
.unwrap();
let timezone_strategy = self.timezone_strategy.clone();
self.format(move |out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
time::OffsetDateTime::now_utc().format(&format).unwrap(),
timezone_strategy.get_now().format(&format).unwrap(),
record.target(),
colors.color(record.level()),
message
@@ -319,6 +360,7 @@ impl Builder {
&path,
app_name,
&self.rotation_strategy,
&self.timezone_strategy,
self.max_file_size,
)?)?
.into()
@@ -336,6 +378,7 @@ impl Builder {
&path,
app_name,
&self.rotation_strategy,
&self.timezone_strategy,
self.max_file_size,
)?)?
.into()
@@ -370,6 +413,7 @@ fn get_log_file_path(
dir: &impl AsRef<Path>,
app_name: &str,
rotation_strategy: &RotationStrategy,
timezone_strategy: &TimezoneStrategy,
max_file_size: u128,
) -> plugin::Result<PathBuf> {
let path = dir.as_ref().join(format!("{app_name}.log"));
@@ -382,7 +426,8 @@ fn get_log_file_path(
let to = dir.as_ref().join(format!(
"{}_{}.log",
app_name,
time::OffsetDateTime::now_utc()
timezone_strategy
.get_now()
.format(
&time::format_description::parse(
"[year]-[month]-[day]_[hour]-[minute]-[second]"