[PATCH 2/5] log: Refactor log level text output

Torsten Hilbrich torsten.hilbrich at secunet.com
Wed Jun 21 08:31:39 UTC 2017


Adding a helper function to turn the log level into the text. Also
add an internal flag to disable this output. This flag will become
useful when adding systemd journal support.
---
 src/mm-log.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/mm-log.c b/src/mm-log.c
index 001f95d..7b4fa7e 100644
--- a/src/mm-log.c
+++ b/src/mm-log.c
@@ -48,6 +48,7 @@ static guint32 log_level = MM_LOG_LEVEL_INFO | MM_LOG_LEVEL_WARN | MM_LOG_LEVEL_
 static GTimeVal rel_start = { 0, 0 };
 static int logfd = -1;
 static gboolean func_loc = FALSE;
+static gboolean append_log_level_text = TRUE;
 
 typedef struct {
     guint32 num;
@@ -82,6 +83,23 @@ mm_to_syslog_priority (MMLogLevel level)
     return 0;
 }
 
+static const char *
+log_level_description (MMLogLevel level)
+{
+    switch (level) {
+    case MM_LOG_LEVEL_DEBUG:
+        return "<debug>";
+    case MM_LOG_LEVEL_WARN:
+        return "<warn> ";
+    case MM_LOG_LEVEL_INFO:
+        return "<info> ";
+    case MM_LOG_LEVEL_ERR:
+        return "<error>";
+    }
+    g_assert_not_reached();
+    return NULL;
+}
+
 void
 _mm_log (const char *loc,
          const char *func,
@@ -102,20 +120,8 @@ _mm_log (const char *loc,
     } else
         g_string_truncate (msgbuf, 0);
 
-    switch (level) {
-    case MM_LOG_LEVEL_DEBUG:
-        g_string_append (msgbuf, "<debug> ");
-        break;
-    case MM_LOG_LEVEL_INFO:
-        g_string_append (msgbuf, "<info>  ");
-        break;
-    case MM_LOG_LEVEL_WARN:
-        g_string_append (msgbuf, "<warn>  ");
-        break;
-    case MM_LOG_LEVEL_ERR:
-        g_string_append (msgbuf, "<error> ");
-        break;
-    }
+    if (append_log_level_text)
+        g_string_append_printf (msgbuf, "%s ", log_level_description (level));
 
     if (ts_flags == TS_FLAG_WALL) {
         g_get_current_time (&tv);
-- 
2.7.4



More information about the ModemManager-devel mailing list