Skip to content

Commit a0f5755

Browse files
committed
🦄 refactor: 优化日志输出
1 parent 82ecd7f commit a0f5755

4 files changed

Lines changed: 52 additions & 30 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/external-media-integration/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ tokio = { version = "1", features = ["full"] }
1717
# Log
1818
tracing = "0.1"
1919
tracing-appender = "0.2"
20-
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
20+
tracing-subscriber = { version = "0.3", features = [
21+
"env-filter",
22+
"time",
23+
"local-time",
24+
] }
25+
time = { version = "0.3", features = ["macros", "formatting", "local-offset"] }
2126

2227
# Error & Serialize
2328
anyhow = "1.0"

native/external-media-integration/index.d.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ export declare function disableSystemMedia(): void
1414

1515
/** Discord 配置参数 */
1616
export interface DiscordConfigPayload {
17-
/** 暂停时是否显示 */
17+
/**
18+
* 暂停时是否显示
19+
*
20+
* 注意暂停时进度会固定为 0
21+
*/
1822
showWhenPaused: boolean
19-
/** 显示模式 */
23+
/** 显示模式,参考 [`DiscordDisplayMode`] */
2024
displayMode?: DiscordDisplayMode
2125
}
2226

2327
/**
2428
* Discord 显示模式枚举
2529
*
26-
* 控制 Discord 左下角 "正在听 XXX" 的显示内容
30+
* 不打开详细信息面板时,在用户名下方显示的小字
2731
*/
2832
export type DiscordDisplayMode = /** Listening to SPlayer */
2933
'Name'|
@@ -76,19 +80,23 @@ export interface MetadataParam {
7680
/**
7781
* 封面的 HTTP URL,更新 Discord RPC 时必传,其他平台可不传
7882
*
79-
* Linux 平台会优先使用 URL 来更新封面
83+
* Linux 平台在没有提供 `cover_data` 时会使用它
8084
*/
8185
originalCoverUrl?: string
8286
/**
83-
* 网易云音乐 ID
87+
* 网易云音乐中对应的曲目 ID
8488
*
85-
* 会以 "NCM-{ID}" 的格式上传到 SMTC 的 “流派” 字段,以及用来生成 Discord RPC 的按钮链接
89+
* ### 用途
90+
* - 以 "NCM-{ID}" 的格式上传到 SMTC 的 “流派” 字段
91+
* - 生成 Discord RPC 的按钮链接
92+
* - MacOS 和 Linux 会使用此值来填充唯一的曲目 ID
8693
*/
8794
ncmId?: number
8895
/**
8996
* 当前歌曲时长,单位是毫秒
9097
*
91-
* 用于 Linux、MacOS、Discord RPC 的元数据更新。Windows 使用 [`TimelinePayload`] 的 `total_time` 字段。
98+
* 用于 Linux、MacOS、Discord RPC 的元数据更新。Windows 使用 [`TimelinePayload`] 的
99+
* `total_time` 字段。
92100
*/
93101
duration?: number
94102
}
@@ -152,7 +160,8 @@ export interface TimelinePayload {
152160
*
153161
* ### 参数
154162
*
155-
* * `payload` - 配置信息,可以配置是否在暂停后也显示 Discord Activity 和 状态显示风格。详情请查看 [`DiscordConfigPayload`]
163+
* * `payload` - 配置信息,可以配置是否在暂停后也显示 Discord Activity 和 状态显示风格。详情请查看
164+
* [`DiscordConfigPayload`]
156165
*/
157166
export declare function updateDiscordConfig(payload: DiscordConfigPayload): void
158167

native/external-media-integration/src/logger.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use anyhow::{
88
Context,
99
Result,
1010
};
11+
use time::macros::format_description;
1112
use tracing::{
1213
error,
1314
trace,
@@ -17,8 +18,12 @@ use tracing_appender::{
1718
rolling::RollingFileAppender,
1819
};
1920
use tracing_subscriber::{
21+
Layer,
2022
filter::LevelFilter,
21-
fmt,
23+
fmt::{
24+
self,
25+
time::LocalTime,
26+
},
2227
layer::SubscriberExt,
2328
util::SubscriberInitExt,
2429
};
@@ -36,7 +41,7 @@ pub fn init(log_dir_str: String) -> Result<()> {
3641
.rotation(tracing_appender::rolling::Rotation::DAILY)
3742
.filename_prefix("external-media-integration")
3843
.filename_suffix("log")
39-
.max_log_files(3)
44+
.max_log_files(5)
4045
.build(&log_path)
4146
.context("无法创建日志文件 Appender")?;
4247

@@ -47,21 +52,24 @@ pub fn init(log_dir_str: String) -> Result<()> {
4752
return Ok(());
4853
}
4954

55+
let time_format = format_description!("[hour]:[minute]:[second]");
56+
let local_timer = LocalTime::new(time_format);
57+
5058
let file_layer = fmt::layer()
5159
.with_writer(non_blocking)
5260
.with_ansi(false)
53-
.with_file(true)
54-
.with_line_number(true)
55-
.with_thread_ids(true)
56-
.with_target(true);
61+
.with_target(true)
62+
.with_timer(local_timer.clone())
63+
.with_filter(LevelFilter::TRACE);
5764

5865
let stdout_layer = fmt::layer()
5966
.with_writer(std::io::stdout)
6067
.with_ansi(true)
61-
.pretty();
68+
.pretty()
69+
.with_timer(local_timer)
70+
.with_filter(LevelFilter::WARN);
6271

6372
tracing_subscriber::registry()
64-
.with(LevelFilter::INFO)
6573
.with(file_layer)
6674
.with(stdout_layer)
6775
.try_init()

0 commit comments

Comments
 (0)