logid_core/logging/
event_entry.rs

1use evident::event::finalized::FinalizedEvent;
2
3use crate::evident::event::origin::Origin;
4use crate::log_id::{LogId, LogLevel};
5
6use super::msg::LogMsg;
7
8#[cfg(feature = "fmt")]
9use super::msg::FmtMsg;
10
11#[derive(Default, Debug, Clone)]
12pub struct LogEventEntry {
13    /// The event id of this entry
14    pub(crate) event_id: LogId,
15    /// The unique id of this entry
16    pub(crate) entry_id: crate::evident::uuid::Uuid,
17    /// The main message set when creating the log-id entry
18    pub(crate) msg: Option<LogMsg>,
19    /// List of additional informations for this log-id entry
20    pub(crate) infos: Vec<String>,
21    /// List of additional formatted information for this log-id entry
22    #[cfg(feature = "fmt")]
23    pub(crate) fmt_infos: Vec<FmtMsg>,
24    /// List of additional debug informations for this log-id entry
25    pub(crate) debugs: Vec<String>,
26    /// List of additional formatted debug information for this log-id entry
27    #[cfg(feature = "fmt")]
28    pub(crate) fmt_debugs: Vec<FmtMsg>,
29    /// List of additional trace information for this log-id entry
30    pub(crate) traces: Vec<String>,
31    /// List of additional formatted trace information for this log-id entry
32    #[cfg(feature = "fmt")]
33    pub(crate) fmt_traces: Vec<FmtMsg>,
34    /// List of related log-id event entries
35    pub(crate) related: Vec<FinalizedEvent<LogId>>,
36    /// Code position where the log-id entry was created
37    pub(crate) origin: Origin,
38
39    /// List of hints for this log-id entry
40    #[cfg(feature = "hint_note")]
41    pub(crate) hints: Vec<String>,
42    /// List of formatted hints for this log-id entry
43    #[cfg(all(feature = "hint_note", feature = "fmt"))]
44    pub(crate) fmt_hints: Vec<FmtMsg>,
45    /// List of notes for this log-id entry
46    #[cfg(feature = "hint_note")]
47    pub(crate) notes: Vec<String>,
48    /// List of formatted notes for this log-id entry
49    #[cfg(all(feature = "hint_note", feature = "fmt"))]
50    pub(crate) fmt_notes: Vec<FmtMsg>,
51
52    /// List of diagnostics for this log-id entry
53    #[cfg(feature = "diagnostics")]
54    pub(crate) diagnostics: Vec<crate::lsp_types::Diagnostic>,
55    /// List of formatted diagnostics for this log-id entry
56    #[cfg(all(feature = "diagnostics", feature = "fmt"))]
57    pub(crate) fmt_diagnostics: Vec<FmtDiagnostics>,
58
59    /// List of payloads for this log-id entry
60    #[cfg(feature = "payloads")]
61    pub(crate) payloads: Vec<crate::serde_json::value::Value>,
62    /// List of formatted payloads for this log-id entry
63    #[cfg(all(feature = "payloads", feature = "fmt"))]
64    pub(crate) fmt_payloads: Vec<FmtMsg>,
65}
66
67impl crate::evident::event::entry::EventEntry<LogId, LogMsg> for LogEventEntry {
68    fn new(event_id: LogId, msg: Option<impl Into<LogMsg>>, origin: Origin) -> Self {
69        LogEventEntry {
70            event_id,
71            entry_id: crate::evident::uuid::Uuid::new_v4(),
72            msg: msg.map(|m| m.into()),
73            infos: Vec::new(),
74            debugs: Vec::new(),
75            traces: Vec::new(),
76            related: Vec::new(),
77            origin,
78
79            #[cfg(feature = "fmt")]
80            fmt_infos: Vec::new(),
81            #[cfg(feature = "fmt")]
82            fmt_debugs: Vec::new(),
83            #[cfg(feature = "fmt")]
84            fmt_traces: Vec::new(),
85
86            #[cfg(feature = "hint_note")]
87            hints: Vec::new(),
88            #[cfg(all(feature = "hint_note", feature = "fmt"))]
89            fmt_hints: Vec::new(),
90            #[cfg(feature = "hint_note")]
91            notes: Vec::new(),
92            #[cfg(all(feature = "hint_note", feature = "fmt"))]
93            fmt_notes: Vec::new(),
94
95            #[cfg(feature = "diagnostics")]
96            diagnostics: Vec::new(),
97            #[cfg(all(feature = "diagnostics", feature = "fmt"))]
98            fmt_diagnostics: Vec::new(),
99
100            #[cfg(feature = "payloads")]
101            payloads: Vec::new(),
102            #[cfg(all(feature = "payloads", feature = "fmt"))]
103            fmt_payloads: Vec::new(),
104        }
105    }
106
107    fn get_event_id(&self) -> &LogId {
108        &self.event_id
109    }
110
111    fn into_event_id(self) -> LogId {
112        self.event_id
113    }
114
115    fn get_entry_id(&self) -> crate::evident::uuid::Uuid {
116        self.entry_id
117    }
118
119    fn get_msg(&self) -> Option<&LogMsg> {
120        self.msg.as_ref()
121    }
122
123    fn get_origin(&self) -> &crate::evident::event::origin::Origin {
124        &self.origin
125    }
126}
127
128impl LogEventEntry {
129    /// Get the level of the log-id of this entry
130    pub fn get_level(&self) -> LogLevel {
131        self.event_id.log_level
132    }
133
134    /// Get the code position where the log-id entry was created
135    pub fn get_origin(&self) -> &Origin {
136        &self.origin
137    }
138
139    /// Get the list of additional informations for this log-id entry
140    pub fn get_infos(&self) -> &Vec<String> {
141        &self.infos
142    }
143    /// Get the list of additional formatted informations for this log-id entry
144    #[cfg(feature = "fmt")]
145    pub fn get_fmt_infos(&self) -> &Vec<FmtMsg> {
146        &self.fmt_infos
147    }
148    /// Get the list of additional debug informations for this log-id entry
149    pub fn get_debugs(&self) -> &Vec<String> {
150        &self.debugs
151    }
152    /// Get the list of additional formatted debug informations for this log-id entry
153    #[cfg(feature = "fmt")]
154    pub fn get_fmt_debugs(&self) -> &Vec<FmtMsg> {
155        &self.fmt_debugs
156    }
157    /// Get the list of additional trace information for this log-id entry
158    pub fn get_traces(&self) -> &Vec<String> {
159        &self.traces
160    }
161    /// Get the list of additional formatted trace informations for this log-id entry
162    #[cfg(feature = "fmt")]
163    pub fn get_fmt_traces(&self) -> &Vec<FmtMsg> {
164        &self.fmt_traces
165    }
166    /// Get the list of related log-id event entries
167    pub fn get_related(&self) -> &Vec<FinalizedEvent<LogId>> {
168        &self.related
169    }
170
171    #[cfg(feature = "hint_note")]
172    pub fn get_hints(&self) -> &Vec<String> {
173        &self.hints
174    }
175    #[cfg(all(feature = "hint_note", feature = "fmt"))]
176    pub fn get_fmt_hints(&self) -> &Vec<FmtMsg> {
177        &self.fmt_hints
178    }
179
180    #[cfg(feature = "hint_note")]
181    pub fn get_notes(&self) -> &Vec<String> {
182        &self.notes
183    }
184    #[cfg(all(feature = "hint_note", feature = "fmt"))]
185    pub fn get_fmt_notes(&self) -> &Vec<FmtMsg> {
186        &self.fmt_notes
187    }
188
189    #[cfg(feature = "diagnostics")]
190    pub fn get_diagnostics(&self) -> &Vec<crate::lsp_types::Diagnostic> {
191        &self.diagnostics
192    }
193    #[cfg(all(feature = "diagnostics", feature = "fmt"))]
194    pub fn get_fmt_diagnostics(&self) -> &Vec<FmtDiagnostics> {
195        &self.fmt_diagnostics
196    }
197
198    #[cfg(feature = "payloads")]
199    pub fn get_payloads(&self) -> &Vec<crate::serde_json::value::Value> {
200        &self.payloads
201    }
202    #[cfg(all(feature = "payloads", feature = "fmt"))]
203    pub fn get_fmt_payloads(&self) -> &Vec<FmtMsg> {
204        &self.fmt_payloads
205    }
206}
207
208/// [`AddonKind`] defines the information kind to be added to an [`EventEntry`].
209#[derive(Debug, Clone, PartialEq, Eq)]
210pub enum AddonKind {
211    Info(String),
212    Debug(String),
213    Trace(String),
214    Related(FinalizedEvent<LogId>),
215
216    #[cfg(feature = "fmt")]
217    FmtInfo(FmtMsg),
218    #[cfg(feature = "fmt")]
219    FmtDebug(FmtMsg),
220    #[cfg(feature = "fmt")]
221    FmtTrace(FmtMsg),
222
223    #[cfg(feature = "hint_note")]
224    Hint(String),
225    #[cfg(all(feature = "hint_note", feature = "fmt"))]
226    FmtHint(FmtMsg),
227    #[cfg(feature = "hint_note")]
228    Note(String),
229    #[cfg(all(feature = "hint_note", feature = "fmt"))]
230    FmtNote(FmtMsg),
231
232    #[cfg(feature = "diagnostics")]
233    Diagnostic(crate::lsp_types::Diagnostic),
234    #[cfg(all(feature = "diagnostics", feature = "fmt"))]
235    FmtDiagnostic(FmtDiagnostics),
236
237    #[cfg(feature = "payloads")]
238    Payload(crate::serde_json::value::Value),
239    #[cfg(all(feature = "payloads", feature = "fmt"))]
240    FmtPayload(FmtMsg),
241}
242
243#[cfg(all(feature = "diagnostics", feature = "fmt"))]
244#[derive(Clone)]
245pub struct FmtDiagnostics {
246    func: for<'a> fn(&'a crate::lsp_types::Diagnostic) -> String,
247    data: crate::lsp_types::Diagnostic,
248}
249
250#[cfg(all(feature = "diagnostics", feature = "fmt"))]
251impl FmtDiagnostics {
252    pub fn new(
253        func: for<'a> fn(&'a crate::lsp_types::Diagnostic) -> String,
254        data: crate::lsp_types::Diagnostic,
255    ) -> Self {
256        FmtDiagnostics { func, data }
257    }
258
259    pub fn get_data(&self) -> &crate::lsp_types::Diagnostic {
260        &self.data
261    }
262}
263
264#[cfg(all(feature = "diagnostics", feature = "fmt"))]
265impl std::fmt::Debug for FmtDiagnostics {
266    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
267        write!(f, "{}", (self.func)(&self.data))
268    }
269}
270
271#[cfg(all(feature = "diagnostics", feature = "fmt"))]
272impl std::fmt::Display for FmtDiagnostics {
273    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
274        write!(f, "{}", (self.func)(&self.data))
275    }
276}
277
278#[cfg(all(feature = "diagnostics", feature = "fmt"))]
279impl PartialEq<FmtDiagnostics> for FmtDiagnostics {
280    fn eq(&self, other: &FmtDiagnostics) -> bool {
281        (self.func)(&self.data) == (other.func)(&other.data)
282    }
283}
284
285#[cfg(all(feature = "diagnostics", feature = "fmt"))]
286impl Eq for FmtDiagnostics {}
287
288#[macro_export]
289macro_rules! info_addon {
290    ($msg:expr) => {
291        $crate::logging::event_entry::AddonKind::Info($msg)
292    };
293    ($fmt_fn:expr, $fmt_data:expr) => {
294        $crate::logging::event_entry::AddonKind::FmtInfo($crate::logging::msg::FmtMsg::new(
295            $fmt_fn, $fmt_data,
296        ))
297    };
298}
299
300#[macro_export]
301macro_rules! debug_addon {
302    ($msg:expr) => {
303        $crate::logging::event_entry::AddonKind::Debug($msg)
304    };
305    ($fmt_fn:expr, $fmt_data:expr) => {
306        $crate::logging::event_entry::AddonKind::FmtDebug($crate::logging::msg::FmtMsg::new(
307            $fmt_fn, $fmt_data,
308        ))
309    };
310}
311
312#[macro_export]
313macro_rules! trace_addon {
314    ($msg:expr) => {
315        $crate::logging::event_entry::AddonKind::Trace($msg)
316    };
317    ($fmt_fn:expr, $fmt_data:expr) => {
318        $crate::logging::event_entry::AddonKind::FmtTrace($crate::logging::msg::FmtMsg::new(
319            $fmt_fn, $fmt_data,
320        ))
321    };
322}
323
324#[cfg(feature = "hint_note")]
325#[macro_export]
326macro_rules! hint_addon {
327    ($msg:expr) => {
328        $crate::logging::event_entry::AddonKind::Hint($msg)
329    };
330    ($fmt_fn:expr, $fmt_data:expr) => {
331        $crate::logging::event_entry::AddonKind::FmtHint($crate::logging::msg::FmtMsg::new(
332            $fmt_fn, $fmt_data,
333        ))
334    };
335}
336
337#[cfg(feature = "hint_note")]
338#[macro_export]
339macro_rules! note_addon {
340    ($msg:expr) => {
341        $crate::logging::event_entry::AddonKind::Note($msg)
342    };
343    ($fmt_fn:expr, $fmt_data:expr) => {
344        $crate::logging::event_entry::AddonKind::FmtNote($crate::logging::msg::FmtMsg::new(
345            $fmt_fn, $fmt_data,
346        ))
347    };
348}
349
350#[cfg(feature = "diagnostics")]
351#[macro_export]
352macro_rules! diagnostic_addon {
353    ($msg:expr) => {
354        $crate::logging::event_entry::AddonKind::Diagnostic($msg)
355    };
356    ($fmt_fn:expr, $fmt_data:expr) => {
357        $crate::logging::event_entry::AddonKind::FmtDiagnostic(
358            $crate::logging::event_entry::FmtDiagnostics::new($fmt_fn, $fmt_data),
359        )
360    };
361}
362
363#[cfg(feature = "payloads")]
364#[macro_export]
365macro_rules! payload_addon {
366    ($msg:expr) => {
367        $crate::logging::event_entry::AddonKind::Payload($msg)
368    };
369    ($fmt_fn:expr, $fmt_data:expr) => {
370        $crate::logging::event_entry::AddonKind::FmtPayload($crate::logging::msg::FmtMsg::new(
371            $fmt_fn, $fmt_data,
372        ))
373    };
374}