aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-12-15 00:08:54 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-12-15 00:08:54 +0100
commit83c95dc26d82d5761b6702fd64b71dde1c41f5a0 (patch)
tree2a98af5c548385aa8a6e1d893f1aa300960d139c
parentSimplify logging tags (diff)
downloadwireguard-apple-83c95dc26d82d5761b6702fd64b71dde1c41f5a0.tar.xz
wireguard-apple-83c95dc26d82d5761b6702fd64b71dde1c41f5a0.zip
Prettier log time format
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--WireGuard/Shared/Logging/ringlogger.c28
1 files changed, 19 insertions, 9 deletions
diff --git a/WireGuard/Shared/Logging/ringlogger.c b/WireGuard/Shared/Logging/ringlogger.c
index a30b3818..74bafa66 100644
--- a/WireGuard/Shared/Logging/ringlogger.c
+++ b/WireGuard/Shared/Logging/ringlogger.c
@@ -5,11 +5,12 @@
#include <string.h>
#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
-#include <stdint.h>
-#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
@@ -62,8 +63,8 @@ static bool first_before_second(const struct log_line *line1, const struct log_l
int write_logs_to_file(const char *file_name, const struct log *log1, const char *tag1, const struct log *log2, const char *tag2)
{
uint32_t i1, i2, len1 = log1->header.len, len2 = log2->header.len;
- char buf[MAX_LOG_LINE_LENGTH];
FILE *file;
+ int ret;
if (len1 > MAX_LINES)
len1 = MAX_LINES;
@@ -75,6 +76,8 @@ int write_logs_to_file(const char *file_name, const struct log *log1, const char
return -errno;
for (i1 = 0, i2 = 0;;) {
+ struct tm tm;
+ char buf[MAX_LOG_LINE_LENGTH];
const struct log_line *line1 = &log1->lines[(log1->header.first + i1) % MAX_LINES];
const struct log_line *line2 = &log2->lines[(log2->header.first + i2) % MAX_LINES];
const struct log_line *line;
@@ -91,16 +94,23 @@ int write_logs_to_file(const char *file_name, const struct log *log1, const char
} else {
break;
}
+
memcpy(buf, line->line, MAX_LOG_LINE_LENGTH);
buf[MAX_LOG_LINE_LENGTH - 1] = '\0';
- if (fprintf(file, "%lu.%06d: [%s] %s\n", line->tv.tv_sec, line->tv.tv_usec, tag, buf) < 0) {
- int ret = -errno;
- fclose(file);
- return ret;
- }
+ if (!localtime_r(&line->tv.tv_sec, &tm))
+ goto err;
+ if (fprintf(file, "%04d-%02d-%02d %02d:%02d:%02d.%06d: [%s] %s\n",
+ tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
+ tm.tm_hour, tm.tm_min, tm.tm_sec, line->tv.tv_usec,
+ tag, buf) < 0)
+ goto err;
}
+ errno = 0;
+
+err:
+ ret = -errno;
fclose(file);
- return 0;
+ return ret;
}
struct log *open_log(const char *file_name)