|
| 1 | +use self::sealed::{LogKVs, LogLevel, LogTarget}; |
| 2 | +use crate::{Level, LevelFilter, Metadata, Record}; |
| 3 | +use std::cmp::Ordering; |
| 4 | +pub use std::convert::identity; |
| 5 | +use std::fmt::Arguments; |
| 6 | +use std::option::Option; |
| 7 | +pub use std::primitive::str; |
| 8 | +pub use std::{file, format_args, line, module_path, stringify}; |
| 9 | + |
| 10 | +#[cfg(feature = "kv_unstable")] |
| 11 | +pub type LogKvValue<'a> = dyn crate::kv::value::ToValue + 'a; |
| 12 | + |
| 13 | +#[cfg(not(feature = "kv_unstable"))] |
| 14 | +pub type LogKvValue<'a> = str; |
| 15 | + |
| 16 | +mod sealed { |
| 17 | + use crate::Level; |
| 18 | + |
| 19 | + pub trait LogLevel { |
| 20 | + fn into_log_level(self) -> Level; |
| 21 | + } |
| 22 | + |
| 23 | + pub trait LogTarget { |
| 24 | + fn with<R>(self, module_path: &'static str, f: impl FnOnce(&str) -> R) -> R; |
| 25 | + } |
| 26 | + |
| 27 | + pub trait LogKVs { |
| 28 | + fn with<R>(self, f: impl FnOnce(&[(&str, &super::LogKvValue)]) -> R) -> R; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// `LogLevel`. |
| 33 | + |
| 34 | +impl LogLevel for Level { |
| 35 | + #[inline] |
| 36 | + fn into_log_level(self) -> Level { |
| 37 | + self |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +macro_rules! define_static_levels { |
| 42 | + ($($ty:ident => $lvl:ident,)*) => { |
| 43 | + $( |
| 44 | + #[derive(Debug)] |
| 45 | + pub struct $ty; |
| 46 | + |
| 47 | + impl LogLevel for $ty { |
| 48 | + #[inline] |
| 49 | + fn into_log_level(self) -> Level { |
| 50 | + Level::$lvl |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + impl PartialEq<LevelFilter> for $ty { |
| 55 | + #[inline] |
| 56 | + fn eq(&self, other: &LevelFilter) -> bool { |
| 57 | + Level::$lvl.eq(other) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + impl PartialOrd<LevelFilter> for $ty { |
| 62 | + #[inline] |
| 63 | + fn partial_cmp(&self, other: &LevelFilter) -> Option<Ordering> { |
| 64 | + Level::$lvl.partial_cmp(other) |
| 65 | + } |
| 66 | + |
| 67 | + #[inline] |
| 68 | + fn lt(&self, other: &LevelFilter) -> bool { |
| 69 | + Level::$lvl.lt(other) |
| 70 | + } |
| 71 | + |
| 72 | + #[inline] |
| 73 | + fn le(&self, other: &LevelFilter) -> bool { |
| 74 | + Level::$lvl.le(other) |
| 75 | + } |
| 76 | + |
| 77 | + #[inline] |
| 78 | + fn gt(&self, other: &LevelFilter) -> bool { |
| 79 | + Level::$lvl.gt(other) |
| 80 | + } |
| 81 | + |
| 82 | + #[inline] |
| 83 | + fn ge(&self, other: &LevelFilter) -> bool { |
| 84 | + Level::$lvl.ge(other) |
| 85 | + } |
| 86 | + } |
| 87 | + )* |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +define_static_levels![ |
| 92 | + StaticLevelError => Error, |
| 93 | + StaticLevelWarn => Warn, |
| 94 | + StaticLevelInfo => Info, |
| 95 | + StaticLevelDebug => Debug, |
| 96 | + StaticLevelTrace => Trace, |
| 97 | +]; |
| 98 | + |
| 99 | +// `LogTarget`. |
| 100 | + |
| 101 | +impl LogTarget for &str { |
| 102 | + #[inline] |
| 103 | + fn with<R>(self, _module_path: &'static str, f: impl FnOnce(&str) -> R) -> R { |
| 104 | + f(self) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Debug)] |
| 109 | +pub struct TargetIsModulePath; |
| 110 | + |
| 111 | +impl LogTarget for TargetIsModulePath { |
| 112 | + #[inline] |
| 113 | + fn with<R>(self, module_path: &'static str, f: impl FnOnce(&str) -> R) -> R { |
| 114 | + f(module_path) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// `LogKVs`. |
| 119 | + |
| 120 | +impl LogKVs for &[(&str, &LogKvValue<'_>)] { |
| 121 | + #[inline] |
| 122 | + fn with<R>(self, f: impl FnOnce(&[(&str, &LogKvValue)]) -> R) -> R { |
| 123 | + f(self) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#[derive(Debug)] |
| 128 | +pub struct EmptyKVs; |
| 129 | + |
| 130 | +impl LogKVs for EmptyKVs { |
| 131 | + #[inline] |
| 132 | + fn with<R>(self, f: impl FnOnce(&[(&str, &LogKvValue)]) -> R) -> R { |
| 133 | + f(&[]) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Log functions. |
| 138 | + |
| 139 | +fn log_0( |
| 140 | + &(module_path, file): &'static (&'static str, &'static str), |
| 141 | + line: u32, |
| 142 | + args: Arguments, |
| 143 | + level: Level, |
| 144 | + target: &str, |
| 145 | + kvs: &[(&str, &LogKvValue)], |
| 146 | +) { |
| 147 | + #[cfg(not(feature = "kv_unstable"))] |
| 148 | + if !kvs.is_empty() { |
| 149 | + panic!( |
| 150 | + "key-value support is experimental and must be enabled using the `kv_unstable` feature" |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + let mut builder = Record::builder(); |
| 155 | + |
| 156 | + builder |
| 157 | + .args(args) |
| 158 | + .level(level) |
| 159 | + .target(target) |
| 160 | + .module_path_static(Some(module_path)) |
| 161 | + .file_static(Some(file)) |
| 162 | + .line(Some(line)); |
| 163 | + |
| 164 | + #[cfg(feature = "kv_unstable")] |
| 165 | + builder.key_values(&kvs); |
| 166 | + |
| 167 | + crate::logger().log(&builder.build()); |
| 168 | +} |
| 169 | + |
| 170 | +fn log_1<K>( |
| 171 | + module_path_and_file: &'static (&'static str, &'static str), |
| 172 | + line: u32, |
| 173 | + args: Arguments, |
| 174 | + level: Level, |
| 175 | + target: &str, |
| 176 | + kvs: K, |
| 177 | +) where |
| 178 | + K: LogKVs, |
| 179 | +{ |
| 180 | + kvs.with(|kvs| log_0(module_path_and_file, line, args, level, target, kvs)); |
| 181 | +} |
| 182 | + |
| 183 | +fn log_2<T, K>( |
| 184 | + module_path_and_file: &'static (&'static str, &'static str), |
| 185 | + line: u32, |
| 186 | + args: Arguments, |
| 187 | + level: Level, |
| 188 | + target: T, |
| 189 | + kvs: K, |
| 190 | +) where |
| 191 | + T: LogTarget, |
| 192 | + K: LogKVs, |
| 193 | +{ |
| 194 | + target.with(module_path_and_file.0, |target| { |
| 195 | + log_1( |
| 196 | + module_path_and_file, |
| 197 | + line, |
| 198 | + args, |
| 199 | + level.into_log_level(), |
| 200 | + target, |
| 201 | + kvs, |
| 202 | + ) |
| 203 | + }); |
| 204 | +} |
| 205 | + |
| 206 | +pub fn log<L, T, K>( |
| 207 | + module_path_and_file: &'static (&'static str, &'static str), |
| 208 | + line: u32, |
| 209 | + args: Arguments, |
| 210 | + level: L, |
| 211 | + target: T, |
| 212 | + kvs: K, |
| 213 | +) where |
| 214 | + L: LogLevel, |
| 215 | + T: LogTarget, |
| 216 | + K: LogKVs, |
| 217 | +{ |
| 218 | + log_2( |
| 219 | + module_path_and_file, |
| 220 | + line, |
| 221 | + args, |
| 222 | + level.into_log_level(), |
| 223 | + target, |
| 224 | + kvs, |
| 225 | + ) |
| 226 | +} |
| 227 | + |
| 228 | +pub fn enabled(level: Level, target: &str) -> bool { |
| 229 | + crate::logger().enabled(&Metadata::builder().level(level).target(target).build()) |
| 230 | +} |
0 commit comments