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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
pub use backtrace; #[cfg(feature = "impl")] pub use chrono; #[cfg(feature = "impl")] pub use fern; #[cfg(feature = "impl")] pub use libc; #[cfg_attr(feature = "cargo-clippy", allow(useless_attribute))] #[allow(unused_imports)] pub use log; pub use log::*; pub use anyhow::*; pub use anyhow; #[derive(Debug)] pub struct Position { pub file: &'static str, pub line: u32, pub column: u32, } impl std::fmt::Display for Position { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { write!(f, "{}:{}:{}", self.file, self.line, self.column) } } #[macro_export] macro_rules! diag_position { () => {{ $crate::Position { file: file!(), line: line!(), column: column!(), } }}; } #[macro_export] macro_rules! diag { ($($arg:tt)+) => {{ error!(target: "diagnostics", $($arg)*); }}; } #[macro_export] macro_rules! diag_backtrace { () => {{ $crate::error!(target: "diagnostics", "{:?}", $crate::backtrace::Backtrace::new()); }} } #[macro_export] macro_rules! diag_err { () => {{ $crate::diag!("internal error at {}", $crate::diag_position!()); $crate::diag_backtrace!(); $crate::anyhow::anyhow!("internal error") }}; ($($arg:tt)+) => {{ $crate::diag!("internal error at {}", $crate::diag_position!()); $crate::diag!($($arg)*); $crate::diag_backtrace!(); $crate::anyhow::anyhow!($($arg)*) }} } #[macro_export] macro_rules! bail_diag { () => {{ $crate::diag!("internal error at {}", $crate::diag_position!()); $crate::diag_backtrace!(); $crate::anyhow::bail!("internal error"); }}; ($($arg:tt)+) => {{ $crate::diag!("internal error at {}", $crate::diag_position!()); $crate::diag!($($arg)*); $crate::diag_backtrace!(); $crate::anyhow::bail!($($arg)*); }} } #[macro_export] macro_rules! diag_unreachable { () => {{ debug_assert!(false, "unreachable code reached"); $crate::diag!("unreachable code reached at {}", $crate::diag_position!()); $crate::diag_backtrace!(); }}; ($($arg:tt)+) => {{ debug_assert!(false, $($arg)*); $crate::diag!($($arg)*); $crate::diag_backtrace!(); }} } #[macro_export] macro_rules! diag_unreachable_err { () => {{ $crate::diag_unreachable!(); $crate::anyhow::anyhow!("unreachable code reached at {}", $crate::diag_position!()) }}; ($($arg:tt)+) => {{ $crate::diag_unreachable!($($arg)*); $crate::anyhow::anyhow!($($arg)*) }} } #[macro_export] macro_rules! diag_unimplemented { () => {{ debug_assert!(false, "unimplemented code reached"); $crate::diag!("unimplemented code reached at {}", $crate::diag_position!()); $crate::diag_backtrace!(); }}; ($($arg:tt)+) => {{ debug_assert!(false, $($arg)*); $crate::diag!($($arg)*); $crate::diag_backtrace!(); }} } #[macro_export] macro_rules! diag_unimplemented_err { () => {{ $crate::diag_unreachable!(); $crate::anyhow::anyhow!("unimplemented code reached at {}", $crate::diag_position!()) }}; ($($arg:tt)+) => {{ $crate::diag_unreachable!($($arg)*); $crate::anyhow::anyhow!($($arg)*) }} } #[macro_export] macro_rules! throw { () => {{ return None; }}; ($($arg:tt)+) => {{ return Err($($arg)*); }} } #[cfg(feature = "impl")] pub fn stdout_dispatch() -> fern::Dispatch { use fern::colors::Color; let colors = fern::colors::ColoredLevelConfig::new() .trace(Color::White) .debug(Color::Blue) .info(Color::Green) .warn(Color::Yellow) .error(Color::Red); fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( "{} {}{}: {}", chrono::Local::now().format("[%Y-%m-%d %H:%M:%S]"), if atty::is(atty::Stream::Stdout) { format!("{}", colors.color(record.level())) } else { format!("{}", record.level()) }, if record.level() == log::Level::Info || record.level() == log::Level::Warn { " " } else { "" }, message, )) }) .level(log::LevelFilter::Info) .level_for("diagnostics", log::LevelFilter::Off) } #[cfg(feature = "impl")] pub fn stdout_dispatch_with_target() -> fern::Dispatch { use fern::colors::Color; let colors = fern::colors::ColoredLevelConfig::new() .trace(Color::White) .debug(Color::Blue) .info(Color::Green) .warn(Color::Yellow) .error(Color::Red); fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( "{} {}{} {}: {}", chrono::Local::now().format("[%Y-%m-%d %H:%M:%S]"), colors.color(record.level()), if record.level() == log::Level::Info || record.level() == log::Level::Warn { " " } else { "" }, record.target(), message, )) }) .level(log::LevelFilter::Info) .level_for("diagnostics", log::LevelFilter::Off) } #[cfg(feature = "impl")] pub fn diag_dispatch() -> fern::Dispatch { fern::Dispatch::new() .format(move |out, message, record| { out.finish(format_args!( "{} {}{} {}: {}", chrono::Local::now().format("[%Y-%m-%d %H:%M:%S%.9f]"), record.level(), if record.level() == log::Level::Info { " " } else { "" }, record.target(), message, )) }) .level(log::LevelFilter::Info) .level_for("diagnostics", log::LevelFilter::Trace) } #[cfg(feature = "impl")] pub fn init_logger( log_file: Option<impl AsRef<std::path::Path>>, ) -> std::io::Result<fern::Dispatch> { let mut dispatch = fern::Dispatch::new().chain(stdout_dispatch().chain(std::io::stdout())); let log_file = if let Some(log_file) = log_file { Some(log_file.as_ref().to_owned()) } else if let Ok(exe_path) = std::env::current_exe() { exe_path.parent().map(|exe_dir| exe_dir.join(".diag.log")) } else { None }; if let Some(log_file) = log_file { let log_file = std::fs::OpenOptions::new() .write(true) .create(true) .truncate(true) .open(log_file)?; dispatch = dispatch.chain(diag_dispatch().chain(log_file)) } Ok(dispatch) } #[cfg(test)] mod tests;