Skip to main content

Module log_viewer

Module log_viewer 

Source
Available on crate feature log-viewer only.
Expand description

A live, searchable log: entries with a level, an optional timestamp and target, and a message, streamed in from threads or tasks.

State keeps the newest entries up to a limit, dropping the oldest, so memory stays bounded however long the log runs. It filters by level, searches messages with every match marked, follows the newest line, pauses and resumes the stream, and selects lines to copy. Only the lines in view are built, as in a virtual list.

subscription creates a bounded channel and emits Event::Ready with its Sender once. Producers send Entry values through clones of the sender, and entries arrive in batches as Event::Received.

Copying returns Output::Copy with the text, for the app to write to the clipboard, and the search shortcut returns Output::FocusSearch, for the app to answer with focus_search. Lines can carry a context menu, such as the one from menu(), through LogViewer::context_menu.

use iced::{Element, Subscription, Task};
use iced_cube::data::log_viewer::{self, Event, Level, Output, State, entry, log_viewer};

#[derive(Debug, Clone)]
enum Message {
    Logs(Event),
}

struct App {
    logs: State,
    sender: Option<log_viewer::Sender>,
}

impl App {
    fn update(&mut self, message: Message) -> Task<Message> {
        let Message::Logs(event) = message;
        match self.logs.update(event) {
            Some(Output::Ready(mut sender)) => {
                let _ = sender.try_send(entry(Level::Info, "Listening on :8080"));
                self.sender = Some(sender);
                Task::none()
            }
            Some(Output::Copy(text)) => iced::clipboard::write(text),
            Some(Output::FocusSearch) => log_viewer::focus_search(&self.logs),
            None => Task::none(),
        }
    }

    fn view(&self) -> Element<'_, Message> {
        log_viewer(&self.logs).height(400).on_event(Message::Logs).into()
    }

    fn subscription(&self) -> Subscription<Message> {
        log_viewer::subscription().map(Message::Logs)
    }
}

Structs§

Entry
One log line.
LineStyle
The colours of a line.
LogViewer
A log viewer builder. Convert it into an Element to render.
State
The kept entries, the filters, the search, the selection and whether the log follows its newest line.

Enums§

Action
What a log keyboard shortcut does.
Event
Everything that changes a log.
Level
How severe an entry is.
MenuItem
An entry of the context menu from menu.
Output
What the app may need to act on after an Event.

Constants§

BATCH_SIZE
The most entries delivered in one Event::Received.
BREAKPOINT
Below this width the toolbar stacks its parts.
CHANNEL_CAPACITY
How many entries a producer can send before it has to wait.
DEFAULT_LIMIT
The most entries kept unless the state is given another limit.
ROW_HEIGHT
Height of a line that does not wrap.
SEARCH_ID
The default widget id of the search field.
STACKED_ROW_HEIGHT
Height of a line that does not wrap below the BREAKPOINT, where the message sits under the timestamp, level and target.

Functions§

default_keymap
The default log shortcuts:
entry
Creates an entry. Add a timestamp and a target with the builder methods.
focus_search
Focuses the search field of state’s log.
level_colour
The colour of a level’s tag.
line_style
Resolves a line’s colours. Lines take the list rows’ colours, with the accent’s text on a selected line, and an error line is tinted when nothing else colours it. The current match is marked more strongly than the others.
log_viewer
Renders state: a toolbar with the search, level filters and actions, above the lines. Without on_event everything renders disabled.
match_label
The text beside the search field: “3 of 12” for the current match of all matches, “12 found” before one is current, “None” when nothing matches, and empty without a search.
menu
A context menu to pass to LogViewer::context_menu: copy the line or only its message, or show only lines of its level. Turn the chosen entry into an event with MenuItem::event.
stream
The stream behind subscription, for driving it without a runtime.
subscription
Owns the channel entries arrive on. Emits Event::Ready first, then batches of up to BATCH_SIZE entries.

Type Aliases§

LineId
Names a line for as long as it is kept: its position counted from the first entry the log ever received.
Sender
The sending half of the channel entries arrive on. Clone it for each producer.
Status
How a line is shown.