gmessages: Add timestamp to g_log_writer_format_fields()

Since journald adds a timestamp, it would be useful to add one to the
stdout/stderr output too — we do not want it to miss out on the
timestamping fun.

Make it blue, because we can.

https://bugzilla.gnome.org/show_bug.cgi?id=769846
This commit is contained in:
Philip Withnall 2016-08-13 17:34:58 +02:00 committed by Philip Withnall
parent df957fa81a
commit a6fc4daeb9

View File

@ -189,6 +189,7 @@
#include "gcharset.h"
#include "gconvert.h"
#include "genviron.h"
#include "gmain.h"
#include "gmem.h"
#include "gprintfint.h"
#include "gtestutils.h"
@ -2205,6 +2206,10 @@ g_log_writer_format_fields (GLogLevelFlags log_level,
const gchar *log_domain = NULL;
gchar level_prefix[STRING_BUFFER_SIZE];
GString *gstring;
gint64 now;
time_t now_secs;
struct tm *now_tm;
gchar time_buf[128];
/* Extract some common fields. */
for (i = 0; (message == NULL || log_domain == NULL) && i < n_fields; i++)
@ -2246,6 +2251,18 @@ g_log_writer_format_fields (GLogLevelFlags log_level,
g_string_append (gstring, level_prefix);
g_string_append (gstring, ": ");
/* Timestamp */
now = g_get_real_time ();
now_secs = (time_t) now / 1000000;
now_tm = localtime (&now_secs);
strftime (time_buf, sizeof (time_buf), "%H:%M:%S", now_tm);
g_string_append_printf (gstring, "%s%s.%03d%s: ",
use_color ? "\033[34m" : "",
time_buf, (gint) ((now / 1000) % 1000),
color_reset (use_color));
if (message == NULL)
{
g_string_append (gstring, "(NULL) message");