#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) {
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) {
global_log_cb(NULL, level, message);
}
}
+#endif
#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