From 874aad0e7bd44cf6d5611b8195464d4cd2800324 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Sat, 21 Sep 2013 17:59:30 +0800 Subject: [PATCH] Add filename and line number to Log/Fatal functions Discovered the __FILE__ and __LINE__ preprocessor defines which seem useful. --- server/log.c | 16 ++++++++++------ server/log.h | 8 ++++---- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/server/log.c b/server/log.c index d41021f..c23d158 100644 --- a/server/log.c +++ b/server/log.c @@ -22,10 +22,12 @@ static const char * unspecified_funct = "???"; If level is higher (less urgent) than the program's verbosity (see options.h) no message will be printed * @param funct - String indicating the function name from which this function was called. If this is NULL, Log will show the unspecified_funct string instead + * @param file - Source file containing the function + * @param line - Line in the source file at which Log is called * @param fmt - A format string * @param ... - Arguments to be printed according to the format string */ -void LogEx(int level, const char * funct, ...) +void LogEx(int level, const char * funct, const char * file, int line, ...) { //Todo: consider setlogmask(3) to filter messages const char *fmt; @@ -36,7 +38,7 @@ void LogEx(int level, const char * funct, ...) if (level > g_options.verbosity) return; - va_start(va, funct); + va_start(va, line); fmt = va_arg(va, const char*); if (fmt == NULL) // sanity check @@ -74,22 +76,24 @@ void LogEx(int level, const char * funct, ...) break; } - syslog(level, "%s: %s - %s", severity, funct, buffer); + syslog(level, "%s: %s (%s:%d) - %s", severity, funct, file, line, buffer); } /** * Handle a Fatal error in the program by printing a message and exiting the program * CALLING THIS FUNCTION WILL CAUSE THE PROGAM TO EXIT * @param funct - Name of the calling function + * @param file - Name of the source file containing the calling function + * @param line - Line in the source file at which Fatal is called * @param fmt - A format string * @param ... - Arguments to be printed according to the format string */ -void FatalEx(const char * funct, ...) +void FatalEx(const char * funct, const char * file, int line, ...) { const char *fmt; char buffer[BUFSIZ]; va_list va; - va_start(va, funct); + va_start(va, line); fmt = va_arg(va, const char*); if (fmt == NULL) @@ -106,7 +110,7 @@ void FatalEx(const char * funct, ...) if (funct == NULL) funct = unspecified_funct; - syslog(LOG_CRIT, "FATAL: %s - %s", funct, buffer); + syslog(LOG_CRIT, "FATAL: %s (%s:%d) - %s", funct, file, line, buffer); exit(EXIT_FAILURE); } diff --git a/server/log.h b/server/log.h index eec5e53..fd98190 100644 --- a/server/log.h +++ b/server/log.h @@ -7,14 +7,14 @@ #define _LOG_H //To get around a 'pedantic' C99 rule that you must have at least 1 variadic arg, combine fmt into that. -#define Log(level, ...) LogEx(level, __func__, __VA_ARGS__) -#define Fatal(...) FatalEx(__func__, __VA_ARGS__) +#define Log(level, ...) LogEx(level, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define Fatal(...) FatalEx(__func__, __FILE__, __LINE__, __VA_ARGS__) // An enum to make the severity of log messages human readable in code enum {LOGERR=0, LOGWARN=1, LOGNOTE=2, LOGINFO=3,LOGDEBUG=4}; -extern void LogEx(int level, const char * funct, ...); // General function for printing log messages to stderr -extern void FatalEx(const char * funct, ...); // Function that deals with a fatal error (prints a message, then exits the program). +extern void LogEx(int level, const char * funct, const char * file, int line, ...); // General function for printing log messages to stderr +extern void FatalEx(const char * funct, const char * file, int line, ...); // Function that deals with a fatal error (prints a message, then exits the program). #endif //_LOG_H -- 2.20.1