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
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

/// Logs a critical level message using the slog global logger.
#[macro_export]
macro_rules! crit( ($($args:tt)+) => {
    ::slog_global::crit!($($args)+)
};);

/// Logs a error level message using the slog global logger. /// Use '?' to output error in debug format or '%' to ouput error in display format.
/// As the third and forth rules shown, the last log field should follow a ',' to seperate the 'err' field. eg. `error!(?e, "msg"; "foo" => foo,);`
/// If you don't want to output error code, just use the common form like other macros.
/// Require `slog_global` dependency and `#![feature(min_speacilization)]` in all crates.
#[macro_export]
macro_rules! error {
    (?$e:expr; $l:literal) => {
        ::slog_global::error!($l; "err" => ?$e,"err_code" => %error_code::ErrorCodeExt::error_code(&$e))
    };

    (%$e:expr; $l:literal) => {
        ::slog_global::error!($l; "err" => ?$e,"err_code" => %error_code::ErrorCodeExt::error_code(&$e))
    };

    (?$e:expr; $($args:tt)+) => {
        ::slog_global::error!($($args)+ "err" => ?$e,"err_code" => %error_code::ErrorCodeExt::error_code(&$e))
    };

    (%$e:expr; $($args:tt)+) => {
        ::slog_global::error!($($args)+ "err" => %$e,"err_code" => %error_code::ErrorCodeExt::error_code(&$e))
    };

    ($($args:tt)+) => {
        ::slog_global::error!($($args)+)
    };
}

// error_unknown is used the same as the above error macro
// However, it will always use an error code of UNKNOWN
// This is for errors that do not implement ErrorCodeExt
// It is recommended to implement ErrorCodeExt instead of using this macro
#[macro_export]
macro_rules! error_unknown {
    (?$e:expr; $l:literal) => {
        ::slog_global::error!($l; "err" => ?$e,"err_code" => %error_code::UNKNOWN)
    };

    (%$e:expr; $l:literal) => {
        ::slog_global::error!($l; "err" => ?$e,"err_code" => %error_code::UNKNOWN)
    };

    (?$e:expr; $($args:tt)+) => {
        ::slog_global::error!($($args)+ "err" => ?$e,"err_code" => %error_code::UNKNOWN)
    };

    (%$e:expr; $($args:tt)+) => {
        ::slog_global::error!($($args)+ "err" => %$e,"err_code" => %error_code::UNKNOWN)
    };
}

/// Logs a warning level message using the slog global logger.
#[macro_export]
macro_rules! warn(($($args:tt)+) => {
    ::slog_global::warn!($($args)+)
};);

/// Logs a info level message using the slog global logger.
#[macro_export]
macro_rules! info(($($args:tt)+) => {
    ::slog_global::info!($($args)+)
};);

/// Logs a debug level message using the slog global logger.
#[macro_export]
macro_rules! debug(($($args:tt)+) => {
    ::slog_global::debug!($($args)+)
};);

/// Logs a trace level message using the slog global logger.
#[macro_export]
macro_rules! trace(($($args:tt)+) => {
    ::slog_global::trace!($($args)+)
};);