Usability: Prefix logging to stdout with ISO 8601 formatted date and time in UTC

This commit is contained in:
practicalswift 2018-02-02 10:35:41 +10:30 committed by Rusty Russell
parent d1ab2e05f4
commit 3696e1b607

View File

@ -53,10 +53,17 @@ static void log_default_print(const char *prefix,
bool continued,
const char *str, void *arg)
{
struct timeval tv;
gettimeofday(&tv, NULL);
char iso8601_msec_fmt[sizeof("YYYY-mm-ddTHH:MM:SS.%03dZ")];
strftime(iso8601_msec_fmt, sizeof(iso8601_msec_fmt), "%FT%T.%%03dZ", gmtime(&tv.tv_sec));
char iso8601_s[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ")];
snprintf(iso8601_s, sizeof(iso8601_s), iso8601_msec_fmt, (int) tv.tv_usec / 1000);
if (!continued) {
printf("%s %s\n", prefix, str);
printf("%s %s %s\n", iso8601_s, prefix, str);
} else {
printf("%s \t%s\n", prefix, str);
printf("%s %s \t%s\n", iso8601_s, prefix, str);
}
fflush(stdout);
}