[vlc-devel] [PATCH] Merge PrintMsg and PrintColorMsg to avoid duplicated code

Ludovic SEGARRA segarral at free.fr
Tue Sep 6 19:11:35 CEST 2011


The functions PrintMsg and PrintColorMsg were almost the
same function except some more color codes.
---
 src/misc/messages.c |   66 +++++++++++++++++++-------------------------------
 1 files changed, 25 insertions(+), 41 deletions(-)

diff --git a/src/misc/messages.c b/src/misc/messages.c
index 8bc8b16..24d1418 100644
--- a/src/misc/messages.c
+++ b/src/misc/messages.c
@@ -129,9 +129,8 @@ void vlc_Log (vlc_object_t *obj, int type, const char *module,
     va_end (args);
 }
 
-static void PrintColorMsg (void *, int, const msg_item_t *,
+static void PrintMsg (const int, bool, int, const msg_item_t *,
                            const char *, va_list);
-static void PrintMsg (void *, int, const msg_item_t *, const char *, va_list);
 
 /**
  * Emit a log message. This function is the variable argument list equivalent
@@ -224,10 +223,7 @@ void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
     va_list ap;
 
     va_copy (ap, args);
-    if (priv->b_color)
-        PrintColorMsg (&priv->i_verbose, type, &msg, format, ap);
-    else
-        PrintMsg (&priv->i_verbose, type, &msg, format, ap);
+    PrintMsg (priv->i_verbose, priv->b_color, type, &msg, format, ap);
     va_end (ap);
 
     vlc_rwlock_rdlock (&msg_lock);
@@ -243,59 +239,46 @@ void vlc_vaLog (vlc_object_t *obj, int type, const char *module,
     freelocale (c);
 }
 
-static const char msg_type[4][9] = { "", " error", " warning", " debug" };
+/* Colors sorted by increasing importance. Green is for convenience. */
+enum {
+    white  = 0,         /* Equal to normal message */
+    red,                /* Equal to type "error"   */
+    yellow,             /* Equal to type "Warning" */
+    gray,               /* Equal to type "Debug"   */
+    green
+};
 #define COL(x,y)  "\033[" #x ";" #y "m"
+#define WHITE   COL(0,1)
 #define RED     COL(31,1)
-#define GREEN   COL(32,1)
 #define YELLOW  COL(0,33)
-#define WHITE   COL(0,1)
 #define GRAY    "\033[0m"
-static const char msg_color[4][8] = { WHITE, RED, YELLOW, GRAY };
-
-static void PrintColorMsg (void *d, int type, const msg_item_t *p_item,
-                           const char *format, va_list ap)
-{
-    const int *pverbose = d;
-    FILE *stream = stderr;
-
-    if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
-        return;
-
-    int canc = vlc_savecancel ();
+#define GREEN   COL(32,1)
+static const char msg_color[5][8] = { WHITE, RED, YELLOW, GRAY, GREEN };
+static const char nocolor[1] = "";
 
-    flockfile (stream);
-    fprintf (stream, "["GREEN"%p"GRAY"] ", (void *)p_item->i_object_id);
-    if (p_item->psz_header != NULL)
-        utf8_fprintf (stream, "[%s] ", p_item->psz_header);
-    utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
-                  p_item->psz_object_type, msg_type[type], msg_color[type]);
-    utf8_vfprintf (stream, format, ap);
-    fputs (GRAY"\n", stream);
-#if defined (WIN32) || defined (__OS2__)
-    fflush (stream);
-#endif
-    funlockfile (stream);
-    vlc_restorecancel (canc);
-}
+static const char msg_type[4][9] = { "", " error", " warning", " debug" };
 
-static void PrintMsg (void *d, int type, const msg_item_t *p_item,
+static void PrintMsg (const int verbose, bool b_color, int type,
+                      const msg_item_t *p_item,
                       const char *format, va_list ap)
 {
-    const int *pverbose = d;
     FILE *stream = stderr;
 
-    if (*pverbose < 0 || *pverbose < (type - VLC_MSG_ERR))
+    if (verbose < 0 || verbose < (type - VLC_MSG_ERR))
         return;
 
     int canc = vlc_savecancel ();
+# define COLOR(color) ( (b_color == true ) ? msg_color[color] : nocolor )
 
     flockfile (stream);
-    fprintf (stream, "[%p] ", (void *)p_item->i_object_id);
+    fprintf (stream, "[%s%p%s] ", COLOR (green),
+             (void *)p_item->i_object_id, COLOR (gray) );
     if (p_item->psz_header != NULL)
         utf8_fprintf (stream, "[%s] ", p_item->psz_header);
-    utf8_fprintf (stream, "%s %s%s: ", p_item->psz_module,
-                  p_item->psz_object_type, msg_type[type]);
+    utf8_fprintf (stream, "%s %s%s: %s", p_item->psz_module,
+                  p_item->psz_object_type, msg_type[type], COLOR (type));
     utf8_vfprintf (stream, format, ap);
+    fputs ( COLOR (gray), stream);
     putc_unlocked ('\n', stream);
 #if defined (WIN32) || defined (__OS2__)
     fflush (stream);
@@ -303,3 +286,4 @@ static void PrintMsg (void *d, int type, const msg_item_t *p_item,
     funlockfile (stream);
     vlc_restorecancel (canc);
 }
+
-- 
1.7.4.1



More information about the vlc-devel mailing list