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
/**
 * Copyright (c) 2019, Jesús Rubio <jesusprubio@gmail.com>
 *                     Hikaru Terazono <3c1u@vulpesgames.tokyo>
 *
 * This source code is licensed under the MIT License found in
 * the LICENSE.txt file in the root directory of this source tree.
 */

use colored::*;

mod printer;
pub use printer::*;

#[cfg(feature = "log_crate")]
mod log_support;
#[cfg(feature = "log_crate")]
pub use log_support::*;

/// Logger.
pub struct Logger;

impl Logger {
    /// Create a custom logger.
    ///
    /// * `writer` - A writer to output a log.
    /// * `tag` - String to use as prefix (after scope).
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - Use `eprintln` instead `eprint` (default: true).
    pub fn custom<W: std::io::Write>(
        writer: W,
        tag: &ColoredString,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        let pref = match scope {
            None => "".to_string(),
            Some(p) => format!("[{}]", p),
        };

        let mut writer = writer;

        write!(writer, "{} {} {}", pref.dimmed(), tag, message)?;

        if ln.unwrap_or(true) {
            writeln!(writer)?;
        }

        Ok(())
    }

    /// Simple header/title for CLIs.
    ///
    /// * `writer` - A writer to output a log.
    /// * `name` - Name of the project.
    /// * `version` - Include also the version.
    pub fn head<W: std::io::Write>(
        writer: W,
        name: &str,
        icon: Option<&str>,
        version: Option<&str>,
    ) -> std::io::Result<()> {
        let ver = match version {
            None => "".to_string(),
            Some(v) => format!("\n\t(v{})\n", v),
        };

        let ico = match icon {
            None => "",
            Some(i) => i,
        };

        let mut writer = writer;

        write!(
            writer,
            "\n\t{} {}{}",
            ico,
            name.bold().underline(),
            ver.dimmed()
        )
    }

    /// Informational message.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn info<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"ℹ".blue().bold(), message, scope, ln)
    }

    /// Succesfull operation.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn success<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"✔".green().bold(), message, scope, ln)
    }

    /// Warn message.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn warn<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"⚠".yellow().bold(), message, scope, ln)
    }

    /// Error message.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn error<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"✖".red().bold(), message, scope, ln)
    }

    /// Waiting for something.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn wait<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"…".magenta().bold(), message, scope, ln)
    }

    /// Something finished.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn done<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"☒".cyan().bold(), message, scope, ln)
    }

    /// Trace log.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn trace<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"🔍".blue().bold(), message, scope, ln)
    }

    /// Debug log.
    ///
    /// * `writer` - A writer to output a log.
    /// * `message` - String to print.
    /// * `scope` - Preffix to append.
    /// * `ln` - To print in the same line instead  (default: false).
    pub fn debug<W: std::io::Write>(
        writer: W,
        message: &str,
        scope: Option<&str>,
        ln: Option<bool>,
    ) -> std::io::Result<()> {
        Self::custom(writer, &"🐛".green().bold(), message, scope, ln)
    }

    /// Put the cursor at the init of the actual line.
    ///
    /// * `writer` - A writer to output a log.
    pub fn remove<W: std::io::Write>(writer: W) -> std::io::Result<()> {
        let mut writer = writer;

        write!(writer, "\r")
    }
}