From 428d6aeedd037dbab86b7cfd3010e10fead2f4d4 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 19 Apr 2020 16:41:54 -0500 Subject: [PATCH] log_error! macro to just log an error but with file and line --- src/game/state/login.rs | 5 +++-- src/macros.rs | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/game/state/login.rs b/src/game/state/login.rs index 08dd302..7d54fb2 100644 --- a/src/game/state/login.rs +++ b/src/game/state/login.rs @@ -7,6 +7,7 @@ use crate::password::Password; use crate::player::Player; use crate::queue::SendQueue; use crate::state::*; +use crate::log_error; impl Game { pub fn login(&mut self, token: Token, message: String, state: &Login) -> SendQueue { @@ -194,7 +195,7 @@ impl Game { } } Ok(None) => { - log::error!("Player has no password: {}", player.id); + log_error!("Player has no password: {}", player.id); send_queue.push(token, "Error\n", false, None); send_queue.push( token, @@ -204,7 +205,7 @@ impl Game { ); } Err(e) => { - log::error!("Database error: {}", e); + log_error!("Database error :: {}", e); send_queue.push(token, "Error\n", false, None); send_queue.push( token, diff --git a/src/macros.rs b/src/macros.rs index 6f3af33..1aca1f1 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,3 +1,12 @@ +/// Log a message to the error log and add file and line. +#[macro_export] +macro_rules! log_error { + ($($arg:tt)*) => { + let s = std::fmt::format(format_args!($($arg)*)); + log::error!("{}({}) :: {}", file!(), line!(), s); + } +} + /// Unwrap a Result. If it's Err then log the error and provided format string /// and return. ///