From f2a4f0412bc1f7b9069ecbcce8f5599f46f757e0 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Thu, 24 Sep 2009 00:56:56 +0200 Subject: Improved file logging, providing source file and line number info Also changed malloc_nullsafe() and free_nullsafe() to report directly which file:line which called the malloc/free function. --- common/eurephia_log.c | 12 ++++++++---- common/eurephia_log.h | 6 ++++-- common/eurephia_nullsafe.c | 16 ++++++++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) (limited to 'common') diff --git a/common/eurephia_log.c b/common/eurephia_log.c index baf63e7..defe781 100644 --- a/common/eurephia_log.c +++ b/common/eurephia_log.c @@ -164,7 +164,9 @@ pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; * @param fmt stdarg, format string * @param ap stdarg va_list, prepared by va_start() */ -static void file_log(FILE *log, int logdst, int loglvl, const char *fmt, va_list ap) { +static void file_log(FILE *log, int logdst, int loglvl, const char *file, const int line, + const char *fmt, va_list ap) +{ char tstmp_str[200]; time_t tstmp; struct tm *loctstmp; @@ -187,7 +189,7 @@ static void file_log(FILE *log, int logdst, int loglvl, const char *fmt, va_list // Do the logging pthread_mutex_lock(&log_mutex); // Block other threads from writing when we write - fprintf(log, "[%s] %s [%i] ", tstmp_str, logprio_str(logdst), loglvl); + fprintf(log, "[%s] %s [%i] {%s:%i} ", tstmp_str, logprio_str(logdst), loglvl, file, line); vfprintf(log, fmt, ap); fprintf(log, "\n"); fflush(log); @@ -204,7 +206,9 @@ static void file_log(FILE *log, int logdst, int loglvl, const char *fmt, va_list * than what this parameter is set to, the message will not be logged. * @param fmt Contents of the log message (stdarg) */ -void eurephia_log(eurephiaCTX *ctx, int logdst, int loglvl, const char *fmt, ... ) { +void _eurephia_log_func(eurephiaCTX *ctx, int logdst, int loglvl, const char *file, const int line, + const char *fmt, ... ) +{ // Only log if we have an open log file and which has high enough log level if( (ctx != NULL) && (ctx->log != NULL) && (ctx->log->opened == 1) @@ -214,7 +218,7 @@ void eurephia_log(eurephiaCTX *ctx, int logdst, int loglvl, const char *fmt, ... va_start(ap, fmt); switch( ctx->log->logtype ) { case logFILE: - file_log(ctx->log->logfile, logdst, loglvl, fmt, ap); + file_log(ctx->log->logfile, logdst, loglvl, file, line, fmt, ap); break; case logSYSLOG: vsyslog(syslog_priority[logdst], fmt, ap); diff --git a/common/eurephia_log.h b/common/eurephia_log.h index cb83f61..07aaea3 100644 --- a/common/eurephia_log.h +++ b/common/eurephia_log.h @@ -41,7 +41,7 @@ * binary if debug logging is not enabled at compile time. This will always use the LOG_DEBUG target * when calling eurephia_log(). */ -#define DEBUG(ctx, log_level, log_string...) eurephia_log(ctx, LOG_DEBUG, log_level, ## log_string); +#define DEBUG(ctx, log_level, log_string...) _eurephia_log_func(ctx, LOG_DEBUG, log_level, __FILE__, __LINE__, ## log_string); #else #define DEBUG(ctx, lvl, rest...) {}; #endif @@ -56,6 +56,8 @@ int eurephia_log_init(eurephiaCTX *ctx, const char *dest, int loglvl); void eurephia_log_close(eurephiaCTX *ctx); -void eurephia_log(eurephiaCTX *ctx, int logdst, int loglvl, const char *fmt, ... ); +#define eurephia_log(ctx, dst, lvl, log_string...) _eurephia_log_func(ctx, dst, lvl, __FILE__, __LINE__, ## log_string) +void _eurephia_log_func(eurephiaCTX *ctx, int logdst, int loglvl, const char *file, int line, + const char *fmt, ... ); #endif /* !EUREPHIA_LOG_H_ */ diff --git a/common/eurephia_nullsafe.c b/common/eurephia_nullsafe.c index bd78d6b..f70b463 100644 --- a/common/eurephia_nullsafe.c +++ b/common/eurephia_nullsafe.c @@ -65,10 +65,15 @@ void *__malloc_nullsafe(eurephiaCTX *ctx, size_t sz, const char *file, int line) "Could not allocate memory region for %ld bytes (File %s, line %i)", sz, file, line); } - } else { - DEBUG(ctx, 40, "Allocated %ld bytes of memory on address %p (File %s, line %i)", - sz, buf, file, line); } +#ifdef DEBUG + else { + // Don't use DEBUG macro, to catch the right file and line number for the log + _eurephia_log_func(ctx, LOG_DEBUG, 40, file, line, + "Allocated %ld bytes of memory on address %p", + sz, buf); + } +#endif return buf; } @@ -87,6 +92,9 @@ void inline __free_nullsafe(eurephiaCTX *ctx, void *ptr, const char *file, int l if( ptr == NULL ) { return; } - DEBUG(ctx, 40, "Freeing memory on address %p (File %s, line %i)", ptr, file, line); +#ifdef DEBUG + // Don't use DEBUG macro, to catch the right file and line number for the log + _eurephia_log_func(ctx, LOG_DEBUG, 40, file, line, "Freeing memory on address %p", ptr); +#endif free(ptr); } -- cgit