1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! Types related to the `debug` host function

use holochain_serialized_bytes::prelude::*;

/// Maps directly to the tracing Levels but here to define the interface.
/// See https://docs.rs/tracing-core/0.1.17/tracing_core/struct.Level.html
#[derive(PartialEq, serde::Serialize, serde::Deserialize, Debug, Clone)]
pub enum Level {
    /// Error.
    ERROR,
    /// Warning.
    WARN,
    /// Info.
    INFO,
    /// Debug.
    DEBUG,
    /// Trace.
    TRACE,
}

impl From<&tracing::Level> for Level {
    fn from(level: &tracing::Level) -> Self {
        match *level {
            tracing::Level::ERROR => Self::ERROR,
            tracing::Level::WARN => Self::WARN,
            tracing::Level::INFO => Self::INFO,
            tracing::Level::DEBUG => Self::DEBUG,
            tracing::Level::TRACE => Self::TRACE,
        }
    }
}

/// Representation of message to be logged via the `debug` host function
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct TraceMsg {
    /// A formatted string to be forwarded to `tracing` on the host side.
    ///
    /// The host will provide:
    /// - Timestamps
    /// - ANSI coloured levels
    ///
    /// The guest should provide:
    /// - Useful message
    /// - Line numbers etc.
    pub msg: String,
    /// Severity level for the message.
    pub level: Level,
}