From: Guus Sliepen Date: Sun, 2 May 2021 12:23:41 +0000 (+0200) Subject: Add __FILE__ and __LINE__ information to log messages. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=325d837df2ec867c668b6a911b97e719f196ba0e;p=meshlink Add __FILE__ and __LINE__ information to log messages. --- diff --git a/src/logger.c b/src/logger.c index 78d629ee..59d027ef 100644 --- a/src/logger.c +++ b/src/logger.c @@ -23,8 +23,8 @@ #include "meshlink_internal.h" #include "sptps.h" -// TODO: refactor logging code to use a meshlink_handle_t *. -void logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *format, ...) { +#ifndef MESHLINK_NO_LOG +void logger2(const char *file, int line, meshlink_handle_t *mesh, meshlink_log_level_t level, const char *format, ...) { assert(format); if(mesh) { @@ -40,12 +40,18 @@ void logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *for va_list ap; char message[1024] = ""; + int hlen = snprintf(message, sizeof message, "%s:%d ", file, line); + va_start(ap, format); - int len = vsnprintf(message, sizeof(message), format, ap); + int len = vsnprintf(message + hlen, sizeof(message) - hlen, format, ap); va_end(ap); - if(len > 0 && (size_t)len < sizeof(message) && message[len - 1] == '\n') { - message[len - 1] = 0; + if(len > 0) { + len += hlen; + + if((size_t)len < sizeof(message) && message[len - 1] == '\n') { + message[len - 1] = 0; + } } if(mesh) { @@ -54,3 +60,4 @@ void logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *for global_log_cb(NULL, level, message); } } +#endif diff --git a/src/logger.h b/src/logger.h index b9269f04..787a6ec8 100644 --- a/src/logger.h +++ b/src/logger.h @@ -22,6 +22,11 @@ #include "meshlink_internal.h" -void logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *format, ...) __attribute__((__format__(printf, 3, 4))); +#ifdef MESHLINK_NO_LOG +#define logger(mesh, level, ...) do {(void)(mesh);} while(0) +#else +#define logger(mesh, level, ...) logger2(__FILE__, __LINE__, (mesh), (level), __VA_ARGS__) +void logger2(const char *file, int line, meshlink_handle_t *mesh, meshlink_log_level_t level, const char *format, ...) __attribute__((__format__(printf, 5, 6))); +#endif #endif