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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
use evident::event::finalized::FinalizedEvent;

use crate::evident::event::origin::Origin;
use crate::log_id::{LogId, LogLevel};

use super::msg::LogMsg;

#[cfg(feature = "fmt")]
use super::msg::FmtMsg;

#[derive(Default, Debug, Clone)]
pub struct LogEventEntry {
    /// The event id of this entry
    pub(crate) event_id: LogId,
    /// The unique id of this entry
    pub(crate) entry_id: crate::evident::uuid::Uuid,
    /// The main message set when creating the log-id entry
    pub(crate) msg: Option<LogMsg>,
    /// List of additional informations for this log-id entry
    pub(crate) infos: Vec<String>,
    /// List of additional formatted information for this log-id entry
    #[cfg(feature = "fmt")]
    pub(crate) fmt_infos: Vec<FmtMsg>,
    /// List of additional debug informations for this log-id entry
    pub(crate) debugs: Vec<String>,
    /// List of additional formatted debug information for this log-id entry
    #[cfg(feature = "fmt")]
    pub(crate) fmt_debugs: Vec<FmtMsg>,
    /// List of additional trace information for this log-id entry
    pub(crate) traces: Vec<String>,
    /// List of additional formatted trace information for this log-id entry
    #[cfg(feature = "fmt")]
    pub(crate) fmt_traces: Vec<FmtMsg>,
    /// List of related log-id event entries
    pub(crate) related: Vec<FinalizedEvent<LogId>>,
    /// Code position where the log-id entry was created
    pub(crate) origin: Origin,

    /// List of hints for this log-id entry
    #[cfg(feature = "hint_note")]
    pub(crate) hints: Vec<String>,
    /// List of formatted hints for this log-id entry
    #[cfg(all(feature = "hint_note", feature = "fmt"))]
    pub(crate) fmt_hints: Vec<FmtMsg>,
    /// List of notes for this log-id entry
    #[cfg(feature = "hint_note")]
    pub(crate) notes: Vec<String>,
    /// List of formatted notes for this log-id entry
    #[cfg(all(feature = "hint_note", feature = "fmt"))]
    pub(crate) fmt_notes: Vec<FmtMsg>,

    /// List of diagnostics for this log-id entry
    #[cfg(feature = "diagnostics")]
    pub(crate) diagnostics: Vec<crate::lsp_types::Diagnostic>,
    /// List of formatted diagnostics for this log-id entry
    #[cfg(all(feature = "diagnostics", feature = "fmt"))]
    pub(crate) fmt_diagnostics: Vec<FmtDiagnostics>,

    /// List of payloads for this log-id entry
    #[cfg(feature = "payloads")]
    pub(crate) payloads: Vec<crate::serde_json::value::Value>,
    /// List of formatted payloads for this log-id entry
    #[cfg(all(feature = "payloads", feature = "fmt"))]
    pub(crate) fmt_payloads: Vec<FmtMsg>,
}

impl crate::evident::event::entry::EventEntry<LogId, LogMsg> for LogEventEntry {
    fn new(event_id: LogId, msg: Option<impl Into<LogMsg>>, origin: Origin) -> Self {
        LogEventEntry {
            event_id,
            entry_id: crate::evident::uuid::Uuid::new_v4(),
            msg: msg.map(|m| m.into()),
            infos: Vec::new(),
            debugs: Vec::new(),
            traces: Vec::new(),
            related: Vec::new(),
            origin,

            #[cfg(feature = "fmt")]
            fmt_infos: Vec::new(),
            #[cfg(feature = "fmt")]
            fmt_debugs: Vec::new(),
            #[cfg(feature = "fmt")]
            fmt_traces: Vec::new(),

            #[cfg(feature = "hint_note")]
            hints: Vec::new(),
            #[cfg(all(feature = "hint_note", feature = "fmt"))]
            fmt_hints: Vec::new(),
            #[cfg(feature = "hint_note")]
            notes: Vec::new(),
            #[cfg(all(feature = "hint_note", feature = "fmt"))]
            fmt_notes: Vec::new(),

            #[cfg(feature = "diagnostics")]
            diagnostics: Vec::new(),
            #[cfg(all(feature = "diagnostics", feature = "fmt"))]
            fmt_diagnostics: Vec::new(),

            #[cfg(feature = "payloads")]
            payloads: Vec::new(),
            #[cfg(all(feature = "payloads", feature = "fmt"))]
            fmt_payloads: Vec::new(),
        }
    }

    fn get_event_id(&self) -> &LogId {
        &self.event_id
    }

    fn into_event_id(self) -> LogId {
        self.event_id
    }

    fn get_entry_id(&self) -> crate::evident::uuid::Uuid {
        self.entry_id
    }

    fn get_msg(&self) -> Option<&LogMsg> {
        self.msg.as_ref()
    }

    fn get_origin(&self) -> &crate::evident::event::origin::Origin {
        &self.origin
    }
}

impl LogEventEntry {
    /// Get the level of the log-id of this entry
    pub fn get_level(&self) -> LogLevel {
        self.event_id.log_level
    }

    /// Get the code position where the log-id entry was created
    pub fn get_origin(&self) -> &Origin {
        &self.origin
    }

    /// Get the list of additional informations for this log-id entry
    pub fn get_infos(&self) -> &Vec<String> {
        &self.infos
    }
    /// Get the list of additional formatted informations for this log-id entry
    #[cfg(feature = "fmt")]
    pub fn get_fmt_infos(&self) -> &Vec<FmtMsg> {
        &self.fmt_infos
    }
    /// Get the list of additional debug informations for this log-id entry
    pub fn get_debugs(&self) -> &Vec<String> {
        &self.debugs
    }
    /// Get the list of additional formatted debug informations for this log-id entry
    #[cfg(feature = "fmt")]
    pub fn get_fmt_debugs(&self) -> &Vec<FmtMsg> {
        &self.fmt_debugs
    }
    /// Get the list of additional trace information for this log-id entry
    pub fn get_traces(&self) -> &Vec<String> {
        &self.traces
    }
    /// Get the list of additional formatted trace informations for this log-id entry
    #[cfg(feature = "fmt")]
    pub fn get_fmt_traces(&self) -> &Vec<FmtMsg> {
        &self.fmt_traces
    }
    /// Get the list of related log-id event entries
    pub fn get_related(&self) -> &Vec<FinalizedEvent<LogId>> {
        &self.related
    }

    #[cfg(feature = "hint_note")]
    pub fn get_hints(&self) -> &Vec<String> {
        &self.hints
    }
    #[cfg(all(feature = "hint_note", feature = "fmt"))]
    pub fn get_fmt_hints(&self) -> &Vec<FmtMsg> {
        &self.fmt_hints
    }

    #[cfg(feature = "hint_note")]
    pub fn get_notes(&self) -> &Vec<String> {
        &self.notes
    }
    #[cfg(all(feature = "hint_note", feature = "fmt"))]
    pub fn get_fmt_notes(&self) -> &Vec<FmtMsg> {
        &self.fmt_notes
    }

    #[cfg(feature = "diagnostics")]
    pub fn get_diagnostics(&self) -> &Vec<crate::lsp_types::Diagnostic> {
        &self.diagnostics
    }
    #[cfg(all(feature = "diagnostics", feature = "fmt"))]
    pub fn get_fmt_diagnostics(&self) -> &Vec<FmtDiagnostics> {
        &self.fmt_diagnostics
    }

    #[cfg(feature = "payloads")]
    pub fn get_payloads(&self) -> &Vec<crate::serde_json::value::Value> {
        &self.payloads
    }
    #[cfg(all(feature = "payloads", feature = "fmt"))]
    pub fn get_fmt_payloads(&self) -> &Vec<FmtMsg> {
        &self.fmt_payloads
    }
}

/// [`AddonKind`] defines the information kind to be added to an [`EventEntry`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AddonKind {
    Info(String),
    Debug(String),
    Trace(String),
    Related(FinalizedEvent<LogId>),

    #[cfg(feature = "fmt")]
    FmtInfo(FmtMsg),
    #[cfg(feature = "fmt")]
    FmtDebug(FmtMsg),
    #[cfg(feature = "fmt")]
    FmtTrace(FmtMsg),

    #[cfg(feature = "hint_note")]
    Hint(String),
    #[cfg(all(feature = "hint_note", feature = "fmt"))]
    FmtHint(FmtMsg),
    #[cfg(feature = "hint_note")]
    Note(String),
    #[cfg(all(feature = "hint_note", feature = "fmt"))]
    FmtNote(FmtMsg),

    #[cfg(feature = "diagnostics")]
    Diagnostic(crate::lsp_types::Diagnostic),
    #[cfg(all(feature = "diagnostics", feature = "fmt"))]
    FmtDiagnostic(FmtDiagnostics),

    #[cfg(feature = "payloads")]
    Payload(crate::serde_json::value::Value),
    #[cfg(all(feature = "payloads", feature = "fmt"))]
    FmtPayload(FmtMsg),
}

#[cfg(all(feature = "diagnostics", feature = "fmt"))]
#[derive(Clone)]
pub struct FmtDiagnostics {
    func: for<'a> fn(&'a crate::lsp_types::Diagnostic) -> String,
    data: crate::lsp_types::Diagnostic,
}

#[cfg(all(feature = "diagnostics", feature = "fmt"))]
impl FmtDiagnostics {
    pub fn new(
        func: for<'a> fn(&'a crate::lsp_types::Diagnostic) -> String,
        data: crate::lsp_types::Diagnostic,
    ) -> Self {
        FmtDiagnostics { func, data }
    }

    pub fn get_data(&self) -> &crate::lsp_types::Diagnostic {
        &self.data
    }
}

#[cfg(all(feature = "diagnostics", feature = "fmt"))]
impl std::fmt::Debug for FmtDiagnostics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", (self.func)(&self.data))
    }
}

#[cfg(all(feature = "diagnostics", feature = "fmt"))]
impl std::fmt::Display for FmtDiagnostics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", (self.func)(&self.data))
    }
}

#[cfg(all(feature = "diagnostics", feature = "fmt"))]
impl PartialEq<FmtDiagnostics> for FmtDiagnostics {
    fn eq(&self, other: &FmtDiagnostics) -> bool {
        (self.func)(&self.data) == (other.func)(&other.data)
    }
}

#[cfg(all(feature = "diagnostics", feature = "fmt"))]
impl Eq for FmtDiagnostics {}

#[macro_export]
macro_rules! info_addon {
    ($msg:expr) => {
        $crate::logging::event_entry::AddonKind::Info($msg)
    };
    ($fmt_fn:expr, $fmt_data:expr) => {
        $crate::logging::event_entry::AddonKind::FmtInfo($crate::logging::msg::FmtMsg::new(
            $fmt_fn, $fmt_data,
        ))
    };
}

#[macro_export]
macro_rules! debug_addon {
    ($msg:expr) => {
        $crate::logging::event_entry::AddonKind::Debug($msg)
    };
    ($fmt_fn:expr, $fmt_data:expr) => {
        $crate::logging::event_entry::AddonKind::FmtDebug($crate::logging::msg::FmtMsg::new(
            $fmt_fn, $fmt_data,
        ))
    };
}

#[macro_export]
macro_rules! trace_addon {
    ($msg:expr) => {
        $crate::logging::event_entry::AddonKind::Trace($msg)
    };
    ($fmt_fn:expr, $fmt_data:expr) => {
        $crate::logging::event_entry::AddonKind::FmtTrace($crate::logging::msg::FmtMsg::new(
            $fmt_fn, $fmt_data,
        ))
    };
}

#[cfg(feature = "hint_note")]
#[macro_export]
macro_rules! hint_addon {
    ($msg:expr) => {
        $crate::logging::event_entry::AddonKind::Hint($msg)
    };
    ($fmt_fn:expr, $fmt_data:expr) => {
        $crate::logging::event_entry::AddonKind::FmtHint($crate::logging::msg::FmtMsg::new(
            $fmt_fn, $fmt_data,
        ))
    };
}

#[cfg(feature = "hint_note")]
#[macro_export]
macro_rules! note_addon {
    ($msg:expr) => {
        $crate::logging::event_entry::AddonKind::Note($msg)
    };
    ($fmt_fn:expr, $fmt_data:expr) => {
        $crate::logging::event_entry::AddonKind::FmtNote($crate::logging::msg::FmtMsg::new(
            $fmt_fn, $fmt_data,
        ))
    };
}

#[cfg(feature = "diagnostics")]
#[macro_export]
macro_rules! diagnostic_addon {
    ($msg:expr) => {
        $crate::logging::event_entry::AddonKind::Diagnostic($msg)
    };
    ($fmt_fn:expr, $fmt_data:expr) => {
        $crate::logging::event_entry::AddonKind::FmtDiagnostic(
            $crate::logging::event_entry::FmtDiagnostics::new($fmt_fn, $fmt_data),
        )
    };
}

#[cfg(feature = "payloads")]
#[macro_export]
macro_rules! payload_addon {
    ($msg:expr) => {
        $crate::logging::event_entry::AddonKind::Payload($msg)
    };
    ($fmt_fn:expr, $fmt_data:expr) => {
        $crate::logging::event_entry::AddonKind::FmtPayload($crate::logging::msg::FmtMsg::new(
            $fmt_fn, $fmt_data,
        ))
    };
}