kovi_plugin_live_agent/
log.rs

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
//! Log methods default to [indoc] format.
//!
//! # Examples  
//! ```
//! std_error!(
//!     "
//!     Write bot log to database failed: {e}
//!     Log: {content}
//!     "
//! );
//! ```
//!
//! which is equivalent to  
//! ```
//! kovi::log::error!("Write bot log to database failed: {e}\nLog: {content}")
//! ```
//!
//! Depending on demand, a log can be volatile or non-volatile.  
//! 1. kovi: use std_debug, std_info, std_warn, std_error  
//! 2. database: use db_debug, db_info, db_warn, db_error  
//! 3. both: use std_db_debug, std_db_info, std_db_warn, std_db_error  
//!
//! Pitfalls  
//! 1. db_* and std_db_* must be in async context  
//! 2. [indoc] does not trim trailing spaces

/// Append debug log entry to stdout
#[macro_export]
macro_rules! std_debug {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        kovi::log::debug!("{}", content);
    }};
}

/// Append info log entry to stdout
#[macro_export]
macro_rules! std_info {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        kovi::log::info!("{}", content);
    }};
}

/// Append warn log entry to stdout
#[macro_export]
macro_rules! std_warn {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        kovi::log::warn!("{}", content);
    }};
}

/// Append error log entry to stdout
#[macro_export]
macro_rules! std_error {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        kovi::log::error!("{}", content);
    }};
}

/// Append debug log entry to database.
#[macro_export]
macro_rules! db_debug {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        $crate::store::db_write_bot_log(time, "DEBUG".to_string(), content).await;
    }};
}

/// Append info log entry to database.
#[macro_export]
macro_rules! db_info {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        $crate::store::db_write_bot_log(time, "INFO".to_string(), content).await;
    }};
}

/// Append warn log entry to database.
#[macro_export]
macro_rules! db_warn {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        $crate::store::db_write_bot_log(time, "WARN".to_string(), content).await;
    }};
}

/// Append error log entry to database.
#[macro_export]
macro_rules! db_error {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        $crate::store::db_write_bot_log(time, "ERROR".to_string(), content).await;
    }};
}

/// Append debug log entry to stdout and database.
#[macro_export]
macro_rules! std_db_debug {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        kovi::log::debug!("{}", content);
        $crate::store::db_write_bot_log(time, "DEBUG".to_string(), content).await;
    }};
}

/// Append info log entry to stdout and database.
#[macro_export]
macro_rules! std_db_info {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        kovi::log::info!("{}", content);
        $crate::store::db_write_bot_log(time, "INFO".to_string(), content).await;
    }};
}

/// Append warn log entry to stdout and database.
#[macro_export]
macro_rules! std_db_warn {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        kovi::log::warn!("{}", content);
        $crate::store::db_write_bot_log(time, "WARN".to_string(), content).await;
    }};
}

/// Append error log entry to stdout and database.
#[macro_export]
macro_rules! std_db_error {
    ($($t:tt)*) => {{
        let content = indoc::formatdoc!($($t)*);
        let time = $crate::util::cur_time_iso8601();
        kovi::log::error!("{}", content);
        $crate::store::db_write_bot_log(time, "ERROR".to_string(), content).await;
    }};
}