Skip to main content

flm_core/progress/
log.rs

1//! The log sink: lines of text for a person about what a build does.
2
3use super::RowId;
4
5/// The level of a log line.
6///
7/// The levels are ordered from the most to the least severe: `Error <
8/// Warn < Info`, so that a sink that shows the lines up to a chosen level
9/// tests `level <= chosen`.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize)]
11#[derive(schemars::JsonSchema)]
12#[serde(rename_all = "lowercase")]
13pub enum LogLevel {
14    /// Something went wrong in the work, such as a program that could not be
15    /// started.
16    Error,
17    /// Something a person should know, such as a fetch that was retried.
18    Warn,
19    /// What the work is doing, for a person who asked for detail.
20    Info,
21}
22
23/// One log line, as a [`LogSink`] receives it.
24///
25/// The record borrows its texts from the writer: building one allocates
26/// nothing.
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28pub struct LogRecord<'a> {
29    /// The level of the line.
30    pub level: LogLevel,
31    /// A short name of who writes the line. For a line written through a
32    /// [`Scope`](super::Scope), the name of the kind of its row.
33    pub source: &'a str,
34    /// The text of the line.
35    pub text: &'a str,
36    /// For a line written through a scope, the id of its row.
37    pub row: Option<RowId>,
38    /// For a line written through a scope, the label of its row, if the row
39    /// has one.
40    pub label: Option<&'a str>,
41}
42
43impl<'a> LogRecord<'a> {
44    /// A record of the line `text` at `level`, written by `source`, outside
45    /// any row.
46    pub fn new(level: LogLevel, source: &'a str, text: &'a str) -> LogRecord<'a> {
47        LogRecord { level, source, text, row: None, label: None }
48    }
49}
50
51/// Where the log lines of a build, or of any other work, go.
52///
53/// A host gives a sink when it starts a [`Sheet`](super::Sheet), and the work
54/// writes to it through [`Scope::log`](super::Scope::log). The trait stands on
55/// its own: a host may give the same sink to code that runs outside any
56/// build.
57///
58/// A log line is text for a person about what the work does; a problem of the
59/// document is a diagnostic, never a log line.
60///
61/// The sink is called on the thread that writes the line, possibly from
62/// several threads at once, and holds that work up until it returns.
63pub trait LogSink: Send + Sync {
64    /// Whether the sink wants the lines of `level`. A writer calls it before
65    /// building the text of a line, to skip building a text nobody wants. A
66    /// sink may still receive a record of a level it does not want, and then
67    /// ignores it.
68    fn enabled(&self, level: LogLevel) -> bool;
69
70    /// Receives one line.
71    fn log(&self, record: &LogRecord<'_>);
72}