aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-09-08 08:46:12 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-09-08 08:46:12 +0200
commite0ea43bf4e54b4cf993f7638a80b752fa3391c64 (patch)
tree7c3f942e56df858614863e4458902331f0013b57
parentsanitize #include-s (diff)
downloadcimple-e0ea43bf4e54b4cf993f7638a80b752fa3391c64.tar.gz
cimple-e0ea43bf4e54b4cf993f7638a80b752fa3391c64.zip
log: prepend timestamps
Diffstat (limited to '')
-rw-r--r--src/log.h41
1 files changed, 38 insertions, 3 deletions
diff --git a/src/log.h b/src/log.h
index 72a63c4..867919a 100644
--- a/src/log.h
+++ b/src/log.h
@@ -5,13 +5,44 @@
#include <libgen.h>
#include <netdb.h>
#include <stdio.h>
+#include <sys/time.h>
+#include <time.h>
+
+static inline void log_print_timestamp(FILE *dest)
+{
+ struct timeval tv;
+ struct tm tm;
+ char buf[64];
+ size_t used = 0;
+
+ if (gettimeofday(&tv, NULL) < 0)
+ return;
+ if (!gmtime_r(&tv.tv_sec, &tm))
+ return;
+
+ buf[0] = '\0';
+ used += strftime(buf + used, sizeof(buf) - used, "[%F %T", &tm);
+ used += snprintf(buf + used, sizeof(buf) - used, ".%03ld] ", tv.tv_usec / 1000);
+ fprintf(dest, "%s", buf);
+}
#define CONCAT_INNER(a, b) a##b
#define CONCAT(a, b) CONCAT_INNER(a, b)
-#define print_errno(s) \
+static inline void log_prefix(FILE *dest)
+{
+ log_print_timestamp(dest);
+}
+
+#define log_error_prefix() \
do { \
+ log_prefix(stderr); \
fprintf(stderr, "%s(%d): ", basename(__FILE__), __LINE__); \
+ } while (0)
+
+#define print_errno(s) \
+ do { \
+ log_error_prefix(); \
perror(s); \
} while (0)
@@ -38,10 +69,14 @@
#define print_error(...) \
do { \
- fprintf(stderr, "%s(%d): ", basename(__FILE__), __LINE__); \
+ log_error_prefix(); \
fprintf(stderr, __VA_ARGS__); \
} while (0)
-#define print_log(...) printf(__VA_ARGS__)
+#define print_log(...) \
+ do { \
+ log_prefix(stdout); \
+ printf(__VA_ARGS__); \
+ } while (0)
#endif