Centralized Logging
Relevant source files
The following files were used as context for generating this wiki page:
- src/logger.rs
- guest-js/logger.ts
- src/commands.rs
- src/lib.rs
- debug.md
- android/src/main/kotlin/app/tauri/serialplugin/SerialPlugin.kt (referenced via debug logs)
Centralized Logging in the tauri-plugin-serialplugin provides a unified interface for diagnostic reporting across the Rust backend, the JavaScript guest API, and the Android mobile implementation. The system ensures that logging behavior remains consistent across environments by utilizing a global log level state that filters messages based on severity.
This architecture avoids circular dependencies and code duplication by providing specific macros in Rust and exported functions in TypeScript to wrap standard output streams (e.g., println!, console.log).
Sources: src/logger.rs:1-5, guest-js/logger.ts:1-2
System Architecture
The logging system is distributed across three layers: the Rust Core, the Guest JavaScript API, and the Mobile (Android) implementation. Each layer implements a severity-based filtering mechanism.
Log Levels
Both Rust and JavaScript implementations share a consistent hierarchy of log levels. The levels are ordered by verbosity, and setting a specific level enables all messages at that level and higher severity.
| Level | Severity | Description |
|---|---|---|
None | 0 | All logging is disabled. |
Error | 1 | Critical failures and error messages. |
Warn | 2 | Warnings that do not stop execution but require attention. |
Info | 3 | High-level operational messages (Default). |
Debug | 4 | Detailed diagnostic information for development. |
Sources: guest-js/logger.ts:5-10, src/logger.rs:45-70
Logic Flow Diagram
The following diagram illustrates how a log request is processed through the level filter before being committed to the output stream.
The system checks the global state to determine if the requested severity meets the minimum threshold defined by the application.
Sources: guest-js/logger.ts:21-27, src/logger.rs:8-40
Rust Implementation
The Rust backend utilizes declarative macros to provide a familiar syntax while enforcing global log level checks. These macros wrap println! and eprintln! calls.
Core Macros
The plugin provides four primary macros for backend logging:
log_error!: Outputs toeprintln!if the level isErroror higher.log_warn!: Outputs toprintln!if the level isWarnor higher.log_info!: Outputs toprintln!if the level isInfoor higher.log_debug!: Outputs toprintln!if the level isDebugor higher.
Sources: src/logger.rs:8-40
Log Level State Management
The global log level is managed via the state module and can be modified at runtime through Tauri commands.
Sources: src/commands.rs:1015-1030, src/lib.rs:150-180
Guest JavaScript API
The JavaScript implementation mirrors the Rust logic, providing a centralized point for frontend logs related to serial operations. It maintains its own internal state for the currentLogLevel.
API Functions
The guest-js/logger.ts file exports functions that wrap standard console methods:
logError(...args): Callsconsole.errorif level permitted.logWarn(...args): Callsconsole.warnif level permitted.logInfo(...args): Callsconsole.logif level permitted.logDebug(...args): Callsconsole.logif level permitted.
Sources: guest-js/logger.ts:30-65
Configuration
Developers can set the frontend log level independently using the setLogLevel function.
Sources: guest-js/logger.ts:12-15
Android Mobile Logging
On Android, logging is integrated into the Kotlin plugin implementation. It utilizes the standard android.util.Log class but is specifically formatted to assist in USB and SerialPort debugging via Logcat.
Common Log Tags
Diagnostic messages on Android use specific tags to allow for precise filtering:
SerialPlugin: Logs related to the Tauri-to-Kotlin IPC bridge.SerialPortManager: Logs related to USB device discovery and lifecycle.
Sources: debug.md:65-75, debug.md:130-140
Logcat Filtering
For effective debugging, the following filter patterns are used:
adb logcat -v time | grep -E "SerialPlugin|SerialPortManager"
Sources: debug.md:125-135
API Integration
Logging levels can be controlled programmatically from the frontend using the SerialPort class or direct command invocation.
Commands Table
| Command | Parameter | Description |
|---|---|---|
set_log_level | level: LogLevel | Updates the global Rust backend log level. |
get_log_level | None | Retrieves the current Rust backend log level. |
Sources: src/commands.rs:1015-1055, build.rs:45-46
Implementation Snippet (Rust Command)
#[tauri::command]
pub fn set_log_level<R: Runtime>(
_app: AppHandle<R>,
_serial: State<'_, SerialPort<R>>,
level: crate::state::LogLevel,
) -> Result<(), Error> {
crate::state::set_log_level(level);
Ok(())
}
Sources: src/commands.rs:1031-1039
Conclusion
The Centralized Logging system provides the necessary infrastructure for cross-platform debugging. By synchronizing log levels between the Rust core and the JavaScript guest, the plugin allows developers to tune the verbosity of the entire serial communication pipeline, which is critical when debugging timing-sensitive hardware interactions.
Sources: src/logger.rs:1-5, debug.md:15-25