Skip to content

Commit 177ba9f

Browse files
committed
Redirecting console logs to logcat
1 parent 44e1807 commit 177ba9f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src-tauri/capabilities/main.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"barcode-scanner:default",
2424
"os:allow-platform",
2525
"dialog:allow-save",
26-
"fs:write-all"
26+
"fs:write-all",
27+
"log:default"
2728
]
2829
}

ui/src/hooks.client.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
import type { HandleClientError } from "@sveltejs/kit";
2+
import { info, warn, error as logError, debug, trace } from "@tauri-apps/plugin-log";
3+
4+
// Forward JS console logs to the Tauri native log plugin.
5+
// On Android this routes to logcat. Requires "log:default" in capabilities/main.json.
6+
function patchConsoleForLogcat() {
7+
const _log = console.log.bind(console);
8+
const _warn = console.warn.bind(console);
9+
const _error = console.error.bind(console);
10+
const _debug = console.debug.bind(console);
11+
12+
const stringify = (...args: unknown[]) =>
13+
args.map((a) => (typeof a === "string" ? a : JSON.stringify(a))).join(" ");
14+
15+
console.log = (...args: unknown[]) => {
16+
_log(...args);
17+
info(stringify(...args)).catch(() => {});
18+
};
19+
console.warn = (...args: unknown[]) => {
20+
_warn(...args);
21+
warn(stringify(...args)).catch(() => {});
22+
};
23+
console.error = (...args: unknown[]) => {
24+
_error(...args);
25+
logError(stringify(...args)).catch(() => {});
26+
};
27+
console.debug = (...args: unknown[]) => {
28+
_debug(...args);
29+
debug(stringify(...args)).catch(() => {});
30+
};
31+
32+
// Emit a test log immediately so we can confirm the bridge works
33+
info("[VollaMessages:Boot] JS→logcat bridge active").catch(() => {});
34+
}
35+
36+
try {
37+
patchConsoleForLogcat();
38+
} catch {
39+
// Not in a Tauri context — ignore silently.
40+
}
241

342
export const handleError: HandleClientError = async ({ error, event, status, message }) => {
443
const errorId = crypto.randomUUID();

0 commit comments

Comments
 (0)