Skip to content

Commit 1c4cfd9

Browse files
feat: more information to logging
also fix game library to don't list non-game data better game-library error catching
1 parent 39db60b commit 1c4cfd9

File tree

10 files changed

+178
-88
lines changed

10 files changed

+178
-88
lines changed

bun.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"clsx": "^2.1.1",
3232
"date-fns": "^4.1.0",
3333
"dequal": "^2.0.3",
34+
"error-stack-parser": "^2.1.4",
3435
"jotai": "^2.11.1",
3536
"jotai-optics": "^0.4.0",
3637
"jotai-tanstack-query": "^0.9.0",
@@ -557,6 +558,8 @@
557558

558559
"enhanced-resolve": ["[email protected]", "", { "dependencies": { "graceful-fs": "^4.2.4", "tapable": "^2.2.0" } }, "sha512-ZSW3ma5GkcQBIpwZTSRAI8N71Uuwgs93IezB7mf7R60tC8ZbJideoDNKjHn2O9KIlx6rkGTTEk1xUCK2E1Y2Yg=="],
559560

561+
"error-stack-parser": ["[email protected]", "", { "dependencies": { "stackframe": "^1.3.4" } }, "sha512-Sk5V6wVazPhq5MhpO+AUxJn5x7XSXGl1R93Vn7i+zS15KDVxQijejNCrz8340/2bgLBjR9GtEG8ZVKONDjcqGQ=="],
562+
560563
"esbuild": ["[email protected]", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.24.2", "@esbuild/android-arm": "0.24.2", "@esbuild/android-arm64": "0.24.2", "@esbuild/android-x64": "0.24.2", "@esbuild/darwin-arm64": "0.24.2", "@esbuild/darwin-x64": "0.24.2", "@esbuild/freebsd-arm64": "0.24.2", "@esbuild/freebsd-x64": "0.24.2", "@esbuild/linux-arm": "0.24.2", "@esbuild/linux-arm64": "0.24.2", "@esbuild/linux-ia32": "0.24.2", "@esbuild/linux-loong64": "0.24.2", "@esbuild/linux-mips64el": "0.24.2", "@esbuild/linux-ppc64": "0.24.2", "@esbuild/linux-riscv64": "0.24.2", "@esbuild/linux-s390x": "0.24.2", "@esbuild/linux-x64": "0.24.2", "@esbuild/netbsd-arm64": "0.24.2", "@esbuild/netbsd-x64": "0.24.2", "@esbuild/openbsd-arm64": "0.24.2", "@esbuild/openbsd-x64": "0.24.2", "@esbuild/sunos-x64": "0.24.2", "@esbuild/win32-arm64": "0.24.2", "@esbuild/win32-ia32": "0.24.2", "@esbuild/win32-x64": "0.24.2" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-+9egpBW8I3CD5XPe0n6BfT5fxLzxrlDzqydF3aviG+9ni1lDC/OvMHcxqEFV0+LANZG5R1bFMWfUrjVsdwxJvA=="],
561564

562565
"escalade": ["[email protected]", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="],
@@ -767,6 +770,8 @@
767770

768771
"spawn-sync": ["[email protected]", "", { "dependencies": { "concat-stream": "^1.4.7", "os-shim": "^0.1.2" } }, "sha512-9DWBgrgYZzNghseho0JOuh+5fg9u6QWhAWa51QC7+U5rCheZ/j1DrEZnyE0RBBRqZ9uEXGPgSSM0nky6burpVw=="],
769772

773+
"stackframe": ["[email protected]", "", {}, "sha512-oeVtt7eWQS+Na6F//S4kJ2K2VbRlS9D43mAlMyVpVWovy9o+jfgH8O9agzANzaiLjclA0oYzUXEM4PurhSUChw=="],
774+
770775
"string_decoder": ["[email protected]", "", { "dependencies": { "safe-buffer": "~5.1.0" } }, "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg=="],
771776

772777
"strip-json-comments": ["[email protected]", "", {}, "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig=="],

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"clsx": "^2.1.1",
4141
"date-fns": "^4.1.0",
4242
"dequal": "^2.0.3",
43+
"error-stack-parser": "^2.1.4",
4344
"jotai": "^2.11.1",
4445
"jotai-optics": "^0.4.0",
4546
"jotai-tanstack-query": "^0.9.0",

src-tauri/Cargo.lock

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

src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ tauri-plugin-store = "2"
3838
tauri-plugin-updater = "2"
3939
tauri-plugin-upload = { version = "2", features = ["rustls-tls"] }
4040
thiserror = "2.0.11"
41+
time = "0.3"
4142
tokio = { version = "1.44.1", features = ["full"] }
4243
zerocopy = { version = "0.8.17", features = ["std"] }
4344
zerocopy-derive = "0.8.17"

src-tauri/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::game_process::state::GameBridge;
22
use log::info;
33
use tauri::Manager;
4+
use crate::logger::build_log_plugin;
45

56
mod file_format;
67
mod game_process;
@@ -9,9 +10,10 @@ mod logger;
910
mod utility_commands;
1011

1112
pub fn run() {
13+
let logger_plugin = build_log_plugin();
1214
tauri::Builder::default()
1315
.invoke_handler(handlers::all_handlers())
14-
.plugin(logger::build_log_plugin())
16+
.plugin(logger_plugin)
1517
.plugin(tauri_plugin_dialog::init())
1618
.plugin(tauri_plugin_fs::init())
1719
.plugin(tauri_plugin_opener::init())

src-tauri/src/logger.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use log::kv::{Key, ToKey};
12
use log::LevelFilter;
23
use std::env;
34
use tauri::plugin::TauriPlugin;
4-
use tauri::Runtime;
5+
use tauri::Wry;
56
use tauri_plugin_log::fern::colors::{Color, ColoredLevelConfig};
67
use tauri_plugin_log::{RotationStrategy, Target, TargetKind};
78

@@ -61,9 +62,14 @@ fn parse_spec(spec: &str) -> ParseResult {
6162
result
6263
}
6364

64-
pub fn build_log_plugin<R: Runtime>() -> TauriPlugin<R> {
65+
pub fn build_log_plugin() -> TauriPlugin<Wry> {
66+
let tz = tauri_plugin_log::TimezoneStrategy::UseLocal;
67+
let format =
68+
time::format_description::parse("[[[year]-[month]-[day]][[[hour]:[minute]:[second]]")
69+
.unwrap();
70+
let column_key = Key::from_str("column");
71+
6572
let mut log_plugin = tauri_plugin_log::Builder::new()
66-
.timezone_strategy(tauri_plugin_log::TimezoneStrategy::UseLocal)
6773
.rotation_strategy(RotationStrategy::KeepAll)
6874
.max_file_size(1024 * 1024 * 32) // 32MB
6975
.targets([
@@ -103,5 +109,37 @@ pub fn build_log_plugin<R: Runtime>() -> TauriPlugin<R> {
103109
}
104110
}
105111

112+
let log_plugin = log_plugin.format(move |out, message, record| {
113+
let target = record.target();
114+
if target.starts_with("webview") {
115+
match (
116+
record.file(),
117+
record.line(),
118+
record.key_values().get(column_key.to_key()),
119+
) {
120+
(Some(file), Some(line), Some(column)) => {
121+
out.finish(format_args!(
122+
"{}[{}][webview {}:{}:{}] {}",
123+
tz.get_now().format(&format).unwrap(),
124+
record.level(),
125+
file,
126+
line,
127+
column,
128+
message
129+
));
130+
return;
131+
}
132+
_ => {}
133+
}
134+
}
135+
out.finish(format_args!(
136+
"{}[{}][{}] {}",
137+
tz.get_now().format(&format).unwrap(),
138+
record.level(),
139+
record.target(),
140+
message
141+
))
142+
});
143+
106144
log_plugin.build()
107145
}

src/handlers/auto-update.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function installUpdate() {
7676
console.info("Update Installed. Restarting app");
7777
await relaunch();
7878
} catch (e: unknown) {
79-
console.error(e);
79+
console.error("download update error", e);
8080
toast.error("Error downloading update: " + stringifyError(e));
8181
}
8282
}

src/lib/native/forward-log.ts

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,65 @@ import {
33
debug,
44
error,
55
info,
6+
type LogOptions,
67
trace,
78
warn,
89
} from "@tauri-apps/plugin-log";
10+
import { format } from "date-fns";
11+
import ErrorStackParser from "error-stack-parser";
912

1013
function forwardConsole(
1114
fnName: "log" | "trace" | "debug" | "info" | "warn" | "error",
12-
logger: (message: string) => Promise<void>,
15+
logger: (message: string, options?: LogOptions) => Promise<void>,
16+
level: string,
1317
) {
1418
const original = console[fnName];
1519
// biome-ignore lint/suspicious/noExplicitAny: This is the original signature
16-
console[fnName] = (...data: any[]) => {
17-
original(...data);
20+
console[fnName] = function (...data: any[]) {
21+
let callFrame: ErrorStackParser.StackFrame | undefined = undefined;
22+
try {
23+
callFrame = ErrorStackParser.parse(new Error())?.[1];
24+
} catch (_e) {
25+
void 0;
26+
}
27+
const now = new Date();
28+
let prefix = "";
29+
prefix += `${format(now, "[y-MM-dd][HH:mm:ss]")}`;
30+
prefix += `[${level}]`;
31+
prefix += callFrame?.fileName
32+
? `[webview ${callFrame.fileName}:${callFrame.lineNumber}:${callFrame.columnNumber}]`
33+
: "[webview]";
34+
prefix += "\n";
35+
36+
if (typeof data?.[0] === "string") {
37+
original(prefix + data.shift(), ...data);
38+
} else {
39+
original(prefix, ...data);
40+
}
41+
42+
let opts: LogOptions | undefined;
43+
if (callFrame) {
44+
opts = {
45+
file: callFrame.fileName?.replace(
46+
/^http:\/\/(localhost|127\.0\.0\.1)(:\d{1,5})?/,
47+
"",
48+
),
49+
line: callFrame.lineNumber,
50+
keyValues: {
51+
column: callFrame.columnNumber?.toString(),
52+
},
53+
};
54+
}
55+
1856
logger(
1957
data
20-
.map((e) => {
58+
.map((e, i) => {
59+
if (e instanceof Error) {
60+
return (
61+
(i > 0 ? "\n" : "") +
62+
(e.stack || `${e.name}: ${e.message}`)
63+
);
64+
}
2165
switch (typeof e) {
2266
case "string":
2367
case "number":
@@ -33,6 +77,7 @@ function forwardConsole(
3377
}
3478
})
3579
.join(" "),
80+
opts,
3681
);
3782
};
3883

@@ -41,12 +86,12 @@ function forwardConsole(
4186

4287
export async function setupForwardingConsole() {
4388
const c = {
44-
log: forwardConsole("log", info),
45-
trace: forwardConsole("trace", trace),
46-
debug: forwardConsole("debug", debug),
47-
info: forwardConsole("info", info),
48-
warn: forwardConsole("warn", warn),
49-
error: forwardConsole("error", error),
89+
log: forwardConsole("log", info, "INFO"),
90+
trace: forwardConsole("trace", trace, "TRACE"),
91+
debug: forwardConsole("debug", debug, "DEBUG"),
92+
info: forwardConsole("info", info, "INFO"),
93+
warn: forwardConsole("warn", warn, "WARN"),
94+
error: forwardConsole("error", error, "ERROR"),
5095
} as const;
5196

5297
window.addEventListener("error", (event) => {
@@ -68,7 +113,7 @@ export async function setupForwardingConsole() {
68113
});
69114

70115
await attachLogger(({ level, message }) => {
71-
if (message.includes("[webview:console.<computed>")) {
116+
if (message.includes("][webview")) {
72117
return;
73118
}
74119
switch (level) {

0 commit comments

Comments
 (0)