Application State Management
Relevant source files
The following files were used as context for generating this wiki page:
Application State Management in LogUtil is a centralized system responsible for coordinating the lifecycle of the TUI (Terminal User Interface), managing parsed log data through a high-performance in-memory database, and handling user configurations. The system is built on a modular architecture that separates data storage, UI rendering logic, and file-based settings.
The state management lifecycle begins with the FileSettings module, which captures initial user preferences and file selections. Once the analysis starts, the App struct becomes the primary orchestrator, maintaining references to the active UI tabs and the global database state.
Core Components
The application state is distributed across several key structures that interact to provide real-time updates and interactive navigation.
The App Orchestrator
The App struct is the central hub for the application's runtime state. it manages the navigation between different functional tabs and holds global UI states like progress bars and modal notifications.
| Component | Description | Source |
|---|---|---|
tab_manager | Tracks the active tab and provides navigation methods. | src/app.rs:38 |
tabs | A collection of Box<dyn Tab> objects representing functional modules. | src/app.rs:39 |
modal_state | Manages temporary notification messages (e.g., "Copied to clipboard"). | src/app.rs:41 |
cached_summary | Caches high-level statistics (Total Requests, Unique IPs) for the header UI. | src/app.rs:43 |
Sources: src/app.rs:36-44
Global Memory Database (MemoryDB)
LogUtil uses a high-performance, in-memory database called MemoryDB to store and index log records. It utilizes DashMap for concurrent access and implements a global singleton instance accessed via GLOBAL_DB.
The diagram shows the relationship between data insertion, the eviction policy for memory management, and how UI components query the global state.
Sources: src/memory_db.rs:31-50, src/memory_db.rs:555
State Lifecycle and Data Flow
The application moves through distinct states from initialization to real-time monitoring.
1. Configuration State (FileSettings)
Before the main application loop starts, the system enters an interactive configuration mode. The state is managed by FileSettings, which tracks directory navigation and parameter toggles (e.g., enabling Security or Performance tabs).
- Active Panel: Tracks whether the user is interacting with the file browser or the settings table.
- CliArgs: A structured data object that captures all settings to transition into the Analysis state.
Sources: src/file_settings.rs:41-70, src/main.rs:149-160
2. Analysis and Monitoring State
Once analysis begins, the state becomes dynamic. A background thread monitors the log file for new lines, while the main thread renders the TUI.
The sequence diagram illustrates the decoupled nature of data ingestion and UI state updates.
Sources: src/main.rs:327-347, src/app.rs:189-215
Memory Management State
To prevent unbounded memory growth, MemoryDB maintains an internal state regarding its capacity.
- Eviction Policy: When records exceed
max_records(default 10,000,000), a FIFO (First-In-First-Out) eviction is triggered. - Index Consistency: The
remove_from_indexesfunction ensures that when a record is evicted to save memory, its entries in the IP, URL, and Status Code indexes are also purged.
Sources: src/memory_db.rs:88-110, src/memory_db.rs:136-150
Tab-Specific State Management
Each tab maintains its own local state for UI interactions (scrolling, selection, filtering) that is independent of the global database.
Security Tab State
The SecurityTab manages state related to threat detection and log investigation.
- active_panel: Toggles between the IP list and the detailed log view.
- input: Stores current search/filter strings for IP filtering.
- show_log_detail: A boolean flag that shifts the entire tab view into a "drill-down" mode for a specific IP.
Sources: src/tabs/security.rs:13-21
Detailed Tab State
The DetailedTab handles synchronized selection state between two panels.
- ip_table_state: Manages the selection in the "Top IPs" list.
- request_list_state: Manages selection in the "Requests for IP" list.
- The system uses
on_left()andon_right()methods to transition focus between these two local states.
Sources: src/tabs/detailed.rs:32-36, src/tabs/detailed.rs:207-224
Summary of Configuration State
The initial application state is defined by CliArgs, which are either passed via command line or configured through the FileSettings UI.
| Parameter | Type | Default | Description |
|---|---|---|---|
count | isize | 0 | Lines to read (0=tail, -1=all). |
regex | String | Nginx Default | Pattern used to parse log lines into state. |
enable_security | bool | false | Toggles the Security state module. |
enable_performance | bool | false | Toggles the Performance state module. |
top | usize | 10 | Limits the number of entries in the Overview state. |
Sources: src/main.rs:51-105, src/file_settings.rs:90-160
Conclusion
Application State Management in LogUtil is characterized by a strong separation between the Global Data State (managed by MemoryDB) and the Local UI State (managed by App and individual Tab implementations). This architecture allows for efficient real-time updates while maintaining a responsive and interactive user interface.