From 2db1606a3646a081991b8af8026c385ccaff8660 Mon Sep 17 00:00:00 2001 From: djcb Date: Mon, 17 Sep 2012 13:17:49 +0300 Subject: [PATCH] * lib: refactor mu-log a bit --- lib/mu-log.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/mu-log.c b/lib/mu-log.c index 62de2b6e..b9d4117c 100644 --- a/lib/mu-log.c +++ b/lib/mu-log.c @@ -245,19 +245,16 @@ pfx (GLogLevelFlags level) do{if (MU_LOG->_color_stderr) fputs ((C),stderr);} while (0) + static void -log_write (const char* domain, GLogLevelFlags level, - const gchar *msg) +log_write_fd (GLogLevelFlags level, const gchar *msg) { time_t now; ssize_t len; - const char *mu; - /* log lines will be truncated at 768-1 chars */ + /* truncate at 768-1 chars */ char buf [768], timebuf [22]; - g_return_if_fail (MU_LOG); - /* get the time/date string */ now = time(NULL); strftime (timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", @@ -267,9 +264,17 @@ log_write (const char* domain, GLogLevelFlags level, len = snprintf (buf, sizeof(buf), "%s [%s] %s\n", timebuf, pfx(level), msg); + if (write (MU_LOG->_fd, buf, (size_t)len) < 0) fprintf (stderr, "%s: failed to write to log: %s\n", __FUNCTION__, strerror(errno)); +} + + +static void +log_write_stdout_stderr (GLogLevelFlags level, const gchar *msg) +{ + const char *mu; mu = MU_LOG->_opts & MU_LOG_OPTIONS_NEWLINE ? "\nmu: " : "mu: "; @@ -294,3 +299,13 @@ log_write (const char* domain, GLogLevelFlags level, color_stderr_maybe (MU_COLOR_DEFAULT); } } + + +static void +log_write (const char* domain, GLogLevelFlags level, const gchar *msg) +{ + g_return_if_fail (MU_LOG); + + log_write_fd (level, msg); + log_write_stdout_stderr (level, msg); +}