向nonebot对齐了一下日志输出

This commit is contained in:
Bluemangoo 2026-07-05 18:39:57 +08:00
parent 355cb36025
commit 1a645f9177
Signed by: Bluemangoo
GPG Key ID: F2F7E46880A1C4CF
10 changed files with 260 additions and 169 deletions

178
Cargo.lock generated
View File

@ -338,6 +338,16 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colored"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
dependencies = [
"lazy_static",
"windows-sys 0.52.0",
]
[[package]]
name = "combine"
version = "4.6.7"
@ -454,12 +464,6 @@ dependencies = [
"typenum",
]
[[package]]
name = "deranged"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
[[package]]
name = "displaydoc"
version = "0.2.6"
@ -498,12 +502,15 @@ version = "0.1.0"
dependencies = [
"anyhow",
"assets-updater",
"chrono",
"common",
"fern",
"futures",
"log",
"mimalloc",
"reqwest",
"serde",
"serde_json",
"simplelog",
"tokio",
]
@ -579,6 +586,16 @@ dependencies = [
"simd-adler32",
]
[[package]]
name = "fern"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4316185f709b23713e41e3195f90edef7fb00c3ed4adc79769cf09cc762a3b29"
dependencies = [
"colored",
"log",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
@ -616,6 +633,21 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]]
name = "futures"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b147ee9d1f6d097cef9ce628cd2ee62288d963e16fb287bd9286455b241382d"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.32"
@ -623,6 +655,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@ -631,6 +664,34 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d"
[[package]]
name = "futures-executor"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf29c38818342a3b26b5b923639e7b1f4a61fc5e76102d4b1981c6dc7a7579d"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cecba35d7ad927e23624b22ad55235f2239cfa44fd10428eecbeba6d6a717718"
[[package]]
name = "futures-macro"
version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835b70203e41293343137df5c0664546da5745f82ec9b84d40be8336958447b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.32"
@ -649,8 +710,13 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"slab",
]
@ -1144,6 +1210,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]]
name = "lebe"
version = "0.5.3"
@ -1166,6 +1238,15 @@ dependencies = [
"cc",
]
[[package]]
name = "libmimalloc-sys"
version = "0.1.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9"
dependencies = [
"cc",
]
[[package]]
name = "libyaml-rs"
version = "0.3.0"
@ -1221,6 +1302,15 @@ version = "2.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88904434abc2901f197fe8cc55f0445e7ded921dba5911dad2e2b39b48e663c4"
[[package]]
name = "mimalloc"
version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d4139bb28d14ad1facf21d5eb8825051b326e172d216b39f6d31df53cc97862"
dependencies = [
"libmimalloc-sys",
]
[[package]]
name = "mime"
version = "0.3.17"
@ -1298,12 +1388,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
[[package]]
name = "num-derive"
version = "0.4.2"
@ -1344,15 +1428,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "once_cell"
version = "1.21.4"
@ -1411,12 +1486,6 @@ dependencies = [
"zerovec",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
@ -2011,17 +2080,6 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
[[package]]
name = "simplelog"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0"
dependencies = [
"log",
"termcolor",
"time",
]
[[package]]
name = "slab"
version = "0.4.12"
@ -2121,21 +2179,11 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "test-main"
version = "0.1.0"
dependencies = [
"entry",
"simplelog",
]
[[package]]
@ -2172,38 +2220,6 @@ dependencies = [
"zune-jpeg",
]
[[package]]
name = "time"
version = "0.3.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711a53c2d47bbd818258c498c8dbfe186a2526c631495cfe7e078567f86b8469"
dependencies = [
"deranged",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde_core",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109"
[[package]]
name = "time-macros"
version = "0.2.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71c652a3727a9cbb9a02f707f530b618ce00d0ccd762009c8c23bd191df3c17d"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "tinystr"
version = "0.8.3"

View File

@ -116,7 +116,7 @@ impl AssetExecutionContext {
.default_headers(headers)
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(180))
.pool_max_idle_per_host(100)
.pool_max_idle_per_host(4)
.tcp_keepalive(Duration::from_secs(30));
if let Some(proxy) = &app_config.execution.proxy

View File

@ -2,8 +2,12 @@
resolver = "3"
members = ["entry", "common", "assets-updater", "test-main"]
#[profile.dev]
#codegen-units = 4
[profile.minimum]
inherits = "release"
lto = true
opt-level = 'z'
codegen-units = 1
strip = true
[workspace.dependencies]
anyhow = "1.0.102"
@ -25,4 +29,6 @@ cridecoder = "0.1.1"
thiserror = "2.0.18"
cipher = "0.4.4"
twox-hash = "2.1.2"
fern = "0.7"
futures = "0.3.32"
mimalloc = { version = "0.1", default-features = false }

View File

@ -11,10 +11,13 @@ crate-type = ["rlib", "cdylib"]
common = { path = "../common" }
assets-updater = { path = "../assets-updater" }
tokio = { workspace = true, features = ["macros", "rt", "rt-multi-thread", "sync"] }
tokio = { workspace = true, features = ["macros", "rt", "sync", ] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
log = { workspace = true }
anyhow = { workspace = true }
simplelog = "0.12.2"
fern = { workspace = true, features = ["colored"] }
futures = { workspace = true }
mimalloc = { workspace = true, default-features = false }
chrono = { workspace = true }

View File

@ -20,6 +20,7 @@ impl Config {
pub struct OptConfig {
pub proxy: Option<String>,
pub ghp: Option<String>,
pub log_level: Option<String>,
}
impl OptConfig {
@ -27,6 +28,7 @@ impl OptConfig {
Self {
proxy: None,
ghp: None,
log_level: None,
}
}
}

View File

@ -3,15 +3,19 @@ use crate::{ending_point, OPT_CONFIG, STORE, UA};
use anyhow::anyhow;
use reqwest::Client;
use serde::Deserialize;
use std::sync::OnceLock;
const COMMIT_URL: &str = "https://api.github.com/repos/kotori8823/sekai-master-db/commits?path=events.json&per_page=1";
const COMMIT_URL: &str =
"https://api.github.com/repos/kotori8823/sekai-master-db/commits?path=events.json&per_page=1";
#[derive(Debug, Deserialize)]
struct CommitResponse {
sha: String,
}
fn http_client() -> anyhow::Result<Client> {
static HTTP_CLIENT: OnceLock<Client> = OnceLock::new();
fn new_http_client() -> anyhow::Result<Client> {
let mut builder = Client::builder();
if let Ok(opt_conf) = &OPT_CONFIG.read() {
if let Some(proxy) = &opt_conf.proxy {
@ -32,6 +36,17 @@ fn http_client() -> anyhow::Result<Client> {
Ok(builder.build()?)
}
fn http_client() -> anyhow::Result<&'static Client> {
let is_init = { HTTP_CLIENT.get().is_some() };
if !is_init {
let client = new_http_client()?;
HTTP_CLIENT
.set(client)
.map_err(|_| anyhow!("Failed to set HTTP client"))?;
}
Ok(HTTP_CLIENT.get().unwrap())
}
pub async fn fetch_last_hash() -> anyhow::Result<String> {
let resp = http_client()?.get(COMMIT_URL).send().await?;
let text = resp.text().await?;
@ -46,30 +61,24 @@ pub fn raw_url(path: &str) -> String {
pub async fn sync_data() -> anyhow::Result<()> {
let client = http_client()?;
let events_cards = &client
.get(raw_url("eventCards.json"))
.send()
.await?
.text()
.await?;
let (events_cards, event_stories, card_episodes) = tokio::try_join!(
client.get(raw_url("eventCards.json")).send().await?.text(),
client
.get(raw_url("eventStories.json"))
.send()
.await?
.text(),
client
.get(raw_url("cardEpisodes.json"))
.send()
.await?
.text()
)?;
ending_point!(ok);
let event_stories = &client
.get(raw_url("eventStories.json"))
.send()
.await?
.text()
.await?;
ending_point!(ok);
let card_episodes = &client
.get(raw_url("cardEpisodes.json"))
.send()
.await?
.text()
.await?;
let mut store = STORE.write().map_err(|e| anyhow!("{}", e))?;
store.set_event_cards(serde_json::from_str(events_cards)?);
store.set_event_stories(serde_json::from_str(event_stories)?);
store.set_card_episodes(serde_json::from_str(card_episodes)?);
store.set_event_cards(serde_json::from_str(&events_cards)?);
store.set_event_stories(serde_json::from_str(&event_stories)?);
store.set_card_episodes(serde_json::from_str(&card_episodes)?);
Ok(())
}

View File

@ -1,3 +1,8 @@
use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
mod data;
mod download;
mod github;
@ -7,14 +12,15 @@ mod store;
use crate::data::{Config, FetchResult, OptConfig, State};
use crate::main_loop::main;
use crate::store::Store;
use log::{LevelFilter, debug, error};
use simplelog::{ColorChoice, TermLogger, TerminalMode};
use chrono::Local;
use fern::colors::{Color, ColoredLevelConfig};
use log::{LevelFilter, debug};
use std::ffi::{CStr, CString, c_char, c_int};
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{OnceLock, RwLock};
use std::thread;
use std::time::Duration;
use tokio::runtime::Runtime;
use tokio::sync::Notify;
pub static CONFIG: OnceLock<Config> = OnceLock::new();
@ -27,23 +33,23 @@ pub static KILLING: AtomicBool = AtomicBool::new(false);
pub static NOTIFY: Notify = Notify::const_new();
#[macro_export]
macro_rules! ending_point {
(ok)=>{
(ok) => {
ending_point!(Err(anyhow!("Killed")))
};
(ok_with $b:block)=>{
if $crate::KILLING.load(::std::sync::atomic::Ordering::SeqCst){
(ok_with $b:block) => {
if $crate::KILLING.load(::std::sync::atomic::Ordering::SeqCst) {
$b;
return Err(anyhow!("Killed"));
}
};
($ret: expr) => {
if $crate::KILLING.load(::std::sync::atomic::Ordering::SeqCst){
if $crate::KILLING.load(::std::sync::atomic::Ordering::SeqCst) {
return $ret;
}
};
() => {
ending_point!(())
}
};
}
pub const UA: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36";
@ -73,6 +79,7 @@ macro_rules! def_opt_str_setter {
def_opt_str_setter!(proxy);
def_opt_str_setter!(ghp);
def_opt_str_setter!(log_level);
/// # Safety
#[unsafe(no_mangle)]
@ -96,17 +103,53 @@ pub unsafe extern "C" fn boot(
.init()
.expect("Failed to initialize state");
*STORE.write().unwrap() = Store::load();
// TermLogger::init(
// LevelFilter::Debug,
// simplelog::Config::default(),
// TerminalMode::Mixed,
// ColorChoice::Auto,
// )
// .unwrap();
if let Some(log_level) = &OPT_CONFIG.read().unwrap().log_level {
match LevelFilter::from_str(log_level) {
Ok(level) => {
let name = CONFIG.get().unwrap().name.clone();
let colors = ColoredLevelConfig::new()
.info(Color::BrightWhite)
.debug(Color::BrightBlue)
.warn(Color::Yellow)
.error(Color::Red);
const GREEN: &str = "\x1b[32m"; // 绿色
const CYAN_UNDERLINE: &str = "\x1b[36;4m"; // 青色 + 下划线
const RESET: &str = "\x1b[0m"; // 重置颜色
let _ = fern::Dispatch::new()
.format(move |out, message, record| {
out.finish(format_args!(
"{green}{time}{reset} [{level}] {cyan_ul}{name}{reset} | {message}",
green = GREEN,
time = Local::now().format("%m-%d %H:%M:%S"),
reset = RESET,
level = colors.color(record.level()), // fern 处理等级颜色
cyan_ul = CYAN_UNDERLINE,
name = name,
message = message
))
})
.level(level)
.chain(std::io::stdout())
.apply()
.is_err_and(|e| {
eprintln!("Failed to initialize logger: {}", e);
true
});
}
Err(err) => {
eprintln!("Invalid log level: '{}': {}", log_level, err);
}
}
}
let handle = thread::Builder::new()
.name("sekai-sync-lib".to_string())
.spawn(|| {
let rt = Runtime::new().expect("Failed to create Tokio runtime");
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Failed to create Tokio runtime");
rt.block_on(keep_loop());
});
@ -117,12 +160,13 @@ pub unsafe extern "C" fn boot(
0
}
Err(error) => {
error!("{}", error);
eprintln!("{}", error);
-1
}
}
}
/// # Safety
#[unsafe(no_mangle)]
pub unsafe extern "C" fn stop() -> c_int {
if let Some(thread) = THREAD.write().unwrap().take() {

View File

@ -4,7 +4,9 @@ use crate::github::{fetch_last_hash, sync_data};
use crate::{CONFIG, PROVIDE, STATE, STORE, ending_point};
use anyhow::anyhow;
use assets_updater::core::export_pipeline::find_files;
use log::debug;
use futures::StreamExt;
use futures::stream::FuturesUnordered;
use log::{debug, info};
use std::fs;
pub async fn main() -> anyhow::Result<()> {
@ -55,45 +57,61 @@ pub async fn main() -> anyhow::Result<()> {
let event = event.unwrap();
// event updated
info!("Event updated to {}", event.event_id);
let cards = store.find_cards_by_event(event.event_id);
drop(store);
ending_point!(ok);
let downloader = Downloader::new().await?;
ending_point!(ok);
let mut images = Vec::new();
let cache_dir = CONFIG.get().unwrap().cache_dir();
for card in cards {
ending_point!(ok);
debug!(
"Pulling card {} from bundle {}",
card.card_id, card.assetbundle_name
);
let dir = downloader
.pull(&format!("character/member/{}", card.assetbundle_name))
.await?;
ending_point!(ok_with {
fs::remove_dir_all(dir.0)?;
});
let files = find_files(&dir.0)?;
for file in files {
let mut futures = FuturesUnordered::new();
for card in &cards {
futures.push(async {
ending_point!(ok);
debug!(
"Pulling card {} from bundle {}",
card.card_id, card.assetbundle_name
);
let dir = downloader
.pull(&format!("character/member/{}", card.assetbundle_name))
.await?;
ending_point!(ok_with {
fs::remove_dir_all(dir.0)?;
});
if file.is_file() && file.extension().map(|ext| ext == "png").unwrap_or(false) {
let new_filename = format!(
"{}_{}",
card.assetbundle_name,
file.file_name().unwrap().to_str().unwrap()
);
let new_file = cache_dir.join(new_filename);
fs::copy(&file, &new_file)?;
fs::remove_file(&file)?;
images.push(new_file);
let files = find_files(&dir.0)?;
let mut local_images = Vec::new();
for file in files {
ending_point!(ok_with {
fs::remove_dir_all(dir.0)?;
});
if file.is_file() && file.extension().map(|ext| ext == "png").unwrap_or(false) {
let new_filename = format!(
"{}_{}",
card.assetbundle_name,
file.file_name().unwrap().to_str().unwrap()
);
let new_file = cache_dir.join(new_filename);
fs::copy(&file, &new_file)?;
fs::remove_file(&file)?;
local_images.push(new_file);
}
}
}
fs::remove_dir_all(dir.0)?;
fs::remove_dir_all(dir.0)?;
Ok::<_, anyhow::Error>((card.card_id, local_images))
});
}
let mut images = Vec::new();
while let Some(res) = futures.next().await {
images.push(res?);
}
images.sort_by(|a, b| b.0.cmp(&a.0)); // 大在前
let images: Vec<_> = images.into_iter().flat_map(|(_, sub)| sub).collect();
info!(
"In event {} collected {} images",
event.event_id,
images.len()
);
*PROVIDE.write().map_err(|e| anyhow!("{}", e))? = Some(FetchResult {
event_id: event.event_id,
card_paths: images,

View File

@ -5,5 +5,3 @@ edition = "2024"
[dependencies]
entry = { path = "../entry" }
simplelog = "0.12.2"

View File

@ -1,19 +1,13 @@
use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
use sekai_sync_lib::{boot, ghp, proxy};
use sekai_sync_lib::{boot, ghp, log_level, proxy};
fn main() {
let _proxy = "http://127.0.0.1:7890";
let _cwd = "D:\\Workspace\\sekai-sync-lib\\target";
let _name = "sekai-sync-lib";
let _as_path = "D:\\Workspace\\AssetStudio\\AssetStudioCLI\\bin\\Release\\net9.0\\AssetStudioModCLI.exe";
let _as_path =
"D:\\Workspace\\AssetStudio\\AssetStudioCLI\\bin\\Release\\net9.0\\AssetStudioModCLI.exe";
let _ghp = "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";
//
// TermLogger::init(
// LevelFilter::Debug,
// Config::default(),
// TerminalMode::Mixed,
// ColorChoice::Auto,
// ).unwrap();
let _log_level = "debug";
macro_rules! cstr {
($v:expr) => {
@ -21,6 +15,7 @@ fn main() {
};
}
unsafe {
log_level(cstr!(_log_level));
proxy(cstr!(_proxy));
ghp(cstr!(_ghp));
boot(cstr!(_cwd), cstr!(_name), cstr!(_as_path));