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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
#[macro_export]
/// Print the file and line number of the location this macro is invoked
///
/// Note: temporarily prints only a null string to reduce verbosity of logging
macro_rules! line_info {
() => {
format!("")
};
}
pub use line_info;
#[macro_export]
/// Create an error at the trace level.
///
/// The argument can be either:
/// - an expression implementing `Display`
/// - a string literal
/// - a format string, similar to the `format!()` macro
macro_rules! trace {
($error:expr) => {
Error {
level: Level::Trace,
message: format!("{}: {}", line_info!(), $error)
}
};
($message:literal) => {
Error {
level: Level::Trace,
message: format!("{}: {}", line_info!(), $message)
}
};
($fmt:expr, $($arg:tt)*) => {
Error {
level: Level::Trace,
message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
}
};
}
pub use trace;
#[macro_export]
/// Create an error at the debug level.
///
/// The argument can be either:
/// - an expression implementing `Display`
/// - a string literal
/// - a format string, similar to the `format!()` macro
macro_rules! debug {
($error:expr) => {
Error {
level: Level::Debug,
message: format!("{}: {}", line_info!(), $error)
}
};
($message:literal) => {
Error {
level: Level::Debug,
message: format!("{}: {}", line_info!(), $message)
}
};
($fmt:expr, $($arg:tt)*) => {
Error {
level: Level::Debug,
message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
}
};
}
pub use debug;
#[macro_export]
/// Create an error at the info level.
///
/// The argument can be either:
/// - an expression implementing `Display`
/// - a string literal
/// - a format string, similar to the `format!()` macro
macro_rules! info {
($error:expr) => {
Error {
level: Level::Info,
message: format!("{}: {}", line_info!(), $error)
}
};
($message:literal) => {
Error {
level: Level::Info,
message: format!("{}: {}", line_info!(), $message)
}
};
($fmt:expr, $($arg:tt)*) => {
Error {
level: Level::Info,
message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
}
};
}
pub use info;
#[macro_export]
/// Create an error at the warn level.
///
/// The argument can be either:
/// - an expression implementing `Display`
/// - a string literal
/// - a format string, similar to the `format!()` macro
macro_rules! warn {
($error:expr) => {
Error {
level: Level::Warn,
message: format!("{}: {}", line_info!(), $error)
}
};
($message:literal) => {
Error {
level: Level::Warn,
message: format!("{}: {}", line_info!(), $message)
}
};
($fmt:expr, $($arg:tt)*) => {
Error {
level: Level::Warn,
message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
}
};
}
pub use crate::warn;
#[macro_export]
/// Create an error at the error level.
///
/// The argument can be either:
/// - an expression implementing `Display`
/// - a string literal
/// - a format string, similar to the `format!()` macro
macro_rules! error {
($error:expr) => {
Error {
level: Level::Error,
message: format!("{}: {}", line_info!(), $error)
}
};
($message:literal) => {
Error {
level: Level::Error,
message: format!("{}: {}", line_info!(), $message)
}
};
($fmt:expr, $($arg:tt)*) => {
Error {
level: Level::Error,
message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
}
};
}
pub use error;
#[macro_export]
/// Log a `anytrace::Error` at the corresponding level.
macro_rules! log {
($result:expr) => {
if let Err(ref error) = $result {
let mut error_level = error.level;
if error_level == Level::Unspecified {
error_level = DEFAULT_LOG_LEVEL;
}
match error_level {
Level::Trace => {
tracing::trace!("{}", error.message);
}
Level::Debug => {
tracing::debug!("{}", error.message);
}
Level::Info => {
tracing::info!("{}", error.message);
}
Level::Warn => {
tracing::warn!("{}", error.message);
}
Level::Error => {
tracing::error!("{}", error.message);
}
// impossible
Level::Unspecified => {}
}
}
};
}
pub use log;
#[macro_export]
/// Check that the given condition holds, otherwise return an error.
///
/// The argument can be either:
/// - a condition, in which case a generic error is logged at the `Unspecified` level.
/// - a condition and a string literal, in which case the provided literal is logged at the `Unspecified` level.
/// - a condition and a format expression, in which case the message is formatted and logged at the `Unspecified` level.
/// - a condition and an `Error`, in which case the given error is logged unchanged.
macro_rules! ensure {
($condition:expr) => {
if !$condition {
let result = Err(Error {
level: Level::Unspecified,
message: format!("{}: condition '{}' failed.", line_info!(), stringify!($condition))
});
log!(result);
return result;
}
};
($condition:expr, $message:literal) => {
if !$condition {
let result = Err(Error {
level: Level::Unspecified,
message: format!("{}: {}", line_info!(), $message)
});
log!(result);
return result;
}
};
($condition:expr, $fmt:expr, $($arg:tt)*) => {
if !$condition {
let result = Err(Error {
level: Level::Unspecified,
message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
});
log!(result);
return result;
}
};
($condition:expr, $error:expr) => {
if !$condition {
let result = Err($error);
log!(result);
return result;
}
};
}
pub use ensure;
#[macro_export]
/// Return an error.
///
/// The argument can be either:
/// - nothing, in which case a generic message is logged at the `Unspecified` level.
/// - a string literal, in which case the provided literal is logged at the `Unspecified` level.
/// - a format expression, in which case the message is formatted and logged at the `Unspecified` level.
/// - an `Error`, in which case the given error is logged unchanged.
macro_rules! bail {
() => {
let result = Err(Error {
level: Level::Unspecified,
message: format!("{}: bailed.", line_info!()),
});
log!(result);
return result;
};
($message:literal) => {
let result = Err(Error {
level: Level::Unspecified,
message: format!("{}: {}", line_info!(), $message)
});
log!(result);
return result;
};
($fmt:expr, $($arg:tt)*) => {
let result = Err(Error {
level: Level::Unspecified,
message: format!("{}: {}", line_info!(), format!($fmt, $($arg)*))
});
log!(result);
return result;
};
($error:expr) => {
let result = Err($error);
log!(result);
return result;
};
}
pub use bail;