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
//! Helper functions for writing to stdout/stderr
//!
//! Some can format, some cannot
#[cfg(any(feature = "macros", not(feature = "no_logger")))]
use std::fmt::Display;

#[cfg(feature = "macros")]
use crate::formatter;

/// Gets the current timestamp or empty string
/// based on whether timestamps feature is enabled
#[cfg(any(feature = "macros", not(feature = "no_logger")))]
fn current_time() -> String {
    #[cfg(feature = "timestamps")]
    {
        crate::timestamp::now()
    }

    #[cfg(not(feature = "timestamps"))]
    {
        String::new()
    }
}

/// Writes to stdout without replacing keys
#[cfg(not(feature = "no_logger"))]
pub fn stdout<T>(message: T, line_ending: &str, with_carriage: bool)
where
    T: Display,
{
    let mut carriage = "";

    if with_carriage {
        carriage = "\r";
    }

    let timestamp = current_time();
    let message = format!("{}{}{}{}", carriage, timestamp, message, line_ending);
    print!("{}", message);
}

/// Writes to stderr without replacing keys
#[cfg(not(feature = "no_logger"))]
pub fn stderr<T>(message: T, line_ending: &str, with_carriage: bool)
where
    T: Display,
{
    let mut carriage = "";

    if with_carriage {
        carriage = "\r";
    }

    let timestamp = current_time();
    let message = format!("{}{}{}{}", carriage, timestamp, message, line_ending);
    eprint!("{}", message);
}

/// Writes to stdout and replaces keys inside the given string
#[cfg(feature = "macros")]
pub fn format_stdout<T>(message: T, line_ending: &str)
where
    T: Display,
{
    let timestamp = current_time();
    let message = format!("{}{}{}", timestamp, message, line_ending);
    print!("{}", formatter::colorize_string(message));
}

/// Writes to stderr and replaces keys inside the given string
#[cfg(feature = "macros")]
pub fn format_stderr<T>(message: T, line_ending: &str)
where
    T: Display,
{
    let timestamp = current_time();
    let message = format!("{}{}{}", timestamp, message, line_ending);
    eprint!("{}", formatter::colorize_string(message));
}