This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
LaoLittle
committed
Jan 14, 2023
1 parent
a0a62be
commit 7f4d965
Showing
5 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# 日志输出最大等级 | ||
# 等级由低至高分别为: Error, Warn, Info, Debug, Tracing | ||
max_level = 'Info' | ||
# 日志时间的输出格式 | ||
# 详见'https://time-rs.github.io/book/api/format-description.html' | ||
time_format = '[year]-[month]-[day] [hour]:[minute]:[second]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
pub static DEFAULT_CONFIG: &[u8] = include_bytes!("../../default_config/log.toml"); | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct LogConfig { | ||
pub max_level: Level, | ||
pub time_format: String, | ||
} | ||
|
||
impl Default for LogConfig { | ||
fn default() -> Self { | ||
Self { | ||
max_level: Level::Info, | ||
time_format: "[year]-[month]-[day] [hour]:[minute]:[second]".into(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub enum Level { | ||
Trace, | ||
Debug, | ||
Info, | ||
Warn, | ||
Error, | ||
} | ||
|
||
impl Level { | ||
pub fn as_tracing_level(&self) -> tracing::Level { | ||
match self { | ||
Level::Trace => tracing::Level::TRACE, | ||
Level::Debug => tracing::Level::DEBUG, | ||
Level::Info => tracing::Level::INFO, | ||
Level::Warn => tracing::Level::WARN, | ||
Level::Error => tracing::Level::ERROR, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::config::service_config_dir_path; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fs; | ||
use std::marker::PhantomData; | ||
use std::path::PathBuf; | ||
use tracing::error; | ||
|
||
pub struct ServiceConfig<T> { | ||
path: PathBuf, | ||
service_name: &'static str, | ||
default_config: &'static [u8], | ||
_mark: PhantomData<T>, | ||
} | ||
|
||
impl<T> ServiceConfig<T> | ||
where | ||
for<'a> T: Serialize + Deserialize<'a>, | ||
T: Default, | ||
{ | ||
pub fn new(name: &'static str, default: &'static [u8]) -> Self { | ||
Self { | ||
path: service_config_dir_path().join(format!("{name}.toml")), | ||
default_config: default, | ||
service_name: name, | ||
_mark: PhantomData, | ||
} | ||
} | ||
|
||
pub fn read(&self) -> T { | ||
if self.path.is_file() { | ||
match fs::read(&self.path) { | ||
Ok(file) => toml::from_slice(&file).unwrap_or_else(|e| { | ||
error!("{e}"); | ||
let mut path = self.path.clone(); | ||
path.pop(); | ||
let mut name = self.service_name.to_owned(); | ||
name.push_str(".toml.bak"); | ||
path.push(name); | ||
let _ = fs::copy(&self.path, path); | ||
self.write_default() | ||
}), | ||
Err(e) => { | ||
error!("{e}"); | ||
self.write_default() | ||
} | ||
} | ||
} else { | ||
self.write_default() | ||
} | ||
} | ||
|
||
fn write_default(&self) -> T { | ||
let default = T::default(); | ||
let _ = fs::write(&self.path, self.default_config); | ||
default | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#![feature(once_cell)] | ||
#![feature(string_leak)] | ||
|
||
use dashmap::DashMap; | ||
use ricq::msg::elem::Text; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters