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
//! # Core Log functionality
//!
//! Log provides an easy way to log a message to the console with a simple, readable output format.
//!

// Copyright © 2022-2023 Mini Functions. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::fmt; // Import the `fmt` module from the standard library.

/// Implements [`Log`] to log a message to the console with a simple, readable output format.
///
/// # Arguments
///
/// * `session_id` - A string slice that holds a session ID. The session ID is a unique identifier for the current session. A random GUID (Globally Unique Identifier) is generated by default.
/// * `time` - A string slice that holds the timestamp in ISO 8601 format.
/// * `level` - A string slice that holds the level (INFO, WARN, ERROR, etc.).
/// * `component` - A string slice that holds the component name.
/// * `description` - A string slice that holds the description of the log message.
///
/// # Examples
/// ```
/// use mini_functions::log::Log;
///
/// let log = Log::new(
/// "12345678-1234-1234-1234-1234567890ab",
/// "2023-12-23T23:23:23.222222+00:00",
/// "INFO",
/// "SystemTrayEvent",
/// "Showing main window",
/// );
/// ```
///

#[non_exhaustive]
#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct Log {
    session_id: String,
    time: String,
    level: String,
    component: String,
    description: String,
}

impl Log {
    /// Create a new `Log` instance.
    /// # Arguments
    /// * `session_id` - A string slice that holds a session ID. The session ID is a unique identifier for the current session. A random GUID (Globally Unique Identifier) is generated by default.
    /// * `time` - A string slice that holds the timestamp in ISO 8601 format.
    /// * `level` - A string slice that holds the level (INFO, WARN, ERROR, etc.).
    /// * `component` - A string slice that holds the component name.
    /// * `description` - A string slice that holds the description of the log message.
    ///
    /// # Returns
    /// A new `Log` instance.
    ///
    /// # Examples
    ///
    /// ```
    /// use mini_functions::log::Log;
    ///
    /// let log = Log::new(
    /// "12345678-1234-1234-1234-1234567890ab",
    /// "2023-12-23T23:23:23.222222+00:00",
    /// "INFO",
    /// "SystemTrayEvent",
    /// "Showing main window",
    /// );
    /// ```
    #[must_use]
    pub fn new(
        session_id: &str,
        time: &str,
        level: &str,
        component: &str,
        description: &str,
    ) -> Self {
        Self {
            session_id: session_id.to_string(),
            time: time.to_string(),
            level: level.to_string(),
            component: component.to_string(),
            description: description.to_string(),
        }
    }
    /// Log a message to the console with a simple, readable output format.
    pub fn log(&self) {
        println!(
            "SessionID={} Timestamp={} Level={} Component={} Description=\"{}\"",
            self.session_id, self.time, self.level, self.component, self.description
        );
    }
}
impl fmt::Display for Log {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "SessionID={} Timestamp={} Level={} Component={} Description=\"{}\"",
            self.session_id, self.time, self.level, self.component, self.description
        )
    }
}