diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 160: Log: Add colored prefixes to log messages.

Message ID 20111031211812.1044.74249.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

alexandros.frantzis@linaro.org Oct. 31, 2011, 9:18 p.m. UTC
Merge authors:
  Alexandros Frantzis (afrantzis)
Related merge proposals:
  https://code.launchpad.net/~linaro-graphics-wg/glmark2/log-color-prefix/+merge/80678
  proposed by: Alexandros Frantzis (afrantzis)
  review: Approve - Jesse Barker (jesse-barker)
------------------------------------------------------------
revno: 160 [merge]
committer: Alexandros Frantzis <alexandros.frantzis@linaro.org>
branch nick: trunk
timestamp: Mon 2011-10-31 23:15:21 +0200
message:
  Log: Add colored prefixes to log messages.
  
  Colors are only used when printing to a terminal. Informational
  messages are prefixed only when showing debug messages.
modified:
  src/canvas-x11-egl.cpp
  src/canvas-x11-glx.cpp
  src/log.cpp
  src/main.cpp


--
lp:glmark2
https://code.launchpad.net/~glmark2-dev/glmark2/trunk

You are subscribed to branch lp:glmark2.
To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'src/canvas-x11-egl.cpp'
--- src/canvas-x11-egl.cpp	2011-10-10 11:00:36 +0000
+++ src/canvas-x11-egl.cpp	2011-10-28 14:25:15 +0000
@@ -44,7 +44,7 @@ 
     if (!eglGetConfigAttrib(egl_display_, egl_config_,
                             EGL_NATIVE_VISUAL_ID, &vid))
     {
-        Log::error("Error: eglGetConfigAttrib() failed with error: %d\n",
+        Log::error("eglGetConfigAttrib() failed with error: %d\n",
                    eglGetError());
         return 0;
     }
@@ -54,7 +54,7 @@ 
     vis_info = XGetVisualInfo(xdpy_, VisualIDMask, &vis_tmpl,
                              &num_visuals);
     if (!vis_info) {
-        Log::error("Error: couldn't get X visual\n");
+        Log::error("couldn't get X visual\n");
         return 0;
     }
 
@@ -73,12 +73,12 @@ 
 
     egl_display_ = eglGetDisplay((EGLNativeDisplayType) xdpy_);
     if (!egl_display_) {
-        Log::error("Error: eglGetDisplay() failed with error: %d\n",
+        Log::error("eglGetDisplay() failed with error: %d\n",
                    eglGetError());
         return false;
     }
     if (!eglInitialize(egl_display_, NULL, NULL)) {
-        Log::error("Error: eglInitialize() failed with error: %d\n",
+        Log::error("eglInitialize() failed with error: %d\n",
                    eglGetError());
         return false;
         egl_display_ = 0;
@@ -115,7 +115,7 @@ 
     if (!eglChooseConfig(egl_display_, attribs, &egl_config_,
                          1, &num_configs))
     {
-        Log::error("Error: eglChooseConfig() failed with error: %d\n",
+        Log::error("eglChooseConfig() failed with error: %d\n",
                      eglGetError());
         return false;
     }
@@ -123,7 +123,7 @@ 
     if (!eglGetConfigAttrib(egl_display_, egl_config_,
                             EGL_NATIVE_VISUAL_ID, &vid))
     {
-        Log::error("Error: eglGetConfigAttrib() failed with error: %d\n",
+        Log::error("eglGetConfigAttrib() failed with error: %d\n",
                    eglGetError());
         return false;
     }
@@ -177,7 +177,7 @@ 
     egl_context_ = eglCreateContext(egl_display_, egl_config_,
                                     EGL_NO_CONTEXT, ctx_attribs);
     if (!egl_context_) {
-        Log::error("Error: eglCreateContext() failed with error: %d\n",
+        Log::error("eglCreateContext() failed with error: %d\n",
                      eglGetError());
         return false;
     }
@@ -186,7 +186,7 @@ 
                                           (EGLNativeWindowType) xwin_,
                                           NULL);
     if (!egl_surface_) {
-        Log::error("Error: eglCreateWindowSurface failed with error: %d\n",
+        Log::error("eglCreateWindowSurface failed with error: %d\n",
                      eglGetError());
         return false;
     }
@@ -231,7 +231,7 @@ 
         return true;
 
     if (!eglMakeCurrent(egl_display_, egl_surface_, egl_surface_, egl_context_)) {
-        Log::error("Error: eglMakeCurrent failed with error %d\n", eglGetError());
+        Log::error("eglMakeCurrent failed with error %d\n", eglGetError());
         return false;
     }
 

=== modified file 'src/canvas-x11-glx.cpp'
--- src/canvas-x11-glx.cpp	2011-10-10 11:00:36 +0000
+++ src/canvas-x11-glx.cpp	2011-10-28 14:25:15 +0000
@@ -55,7 +55,7 @@ 
     init_extensions();
 
     if (!glXMakeCurrent(xdpy_, xwin_, glx_context_)) {
-        Log::error("Error: glXMakeCurrent failed\n");
+        Log::error("glXMakeCurrent failed\n");
         return false;
     }
 
@@ -151,7 +151,7 @@ 
     GLXFBConfig *fbc = glXChooseFBConfig(xdpy_, DefaultScreen(xdpy_),
                                          attribs, &num_configs);
     if (!fbc) {
-        Log::error("Error: glXChooseFBConfig() failed\n");
+        Log::error("glXChooseFBConfig() failed\n");
         return false;
     }
 
@@ -206,7 +206,7 @@ 
     glx_context_ = glXCreateNewContext(xdpy_, glx_fbconfig_, GLX_RGBA_TYPE,
                                        0, True);
     if (!glx_context_) {
-        Log::error("Error: glXCreateNewContext failed\n");
+        Log::error("glXCreateNewContext failed\n");
         return false;
     }
 

=== modified file 'src/log.cpp'
--- src/log.cpp	2011-08-10 17:57:38 +0000
+++ src/log.cpp	2011-10-28 14:24:19 +0000
@@ -23,6 +23,8 @@ 
 
 #include <cstdio>
 #include <cstdarg>
+#include <string>
+#include <sstream>
 
 #include "options.h"
 #include "log.h"
@@ -32,12 +34,59 @@ 
 #endif
 
 #ifndef ANDROID
+
+static const char *terminal_color_normal("\033[0m");
+static const char *terminal_color_red("\033[1;31m");
+static const char *terminal_color_cyan("\033[36m");
+static const char *terminal_color_yellow("\033[33m");
+
+static void
+print_prefixed_message(FILE *stream, const char *color, const char *prefix,
+                       const char *fmt, va_list ap)
+{
+    va_list aq;
+
+    /* Estimate message size */
+    va_copy(aq, ap);
+    int msg_size = vsnprintf(NULL, 0, fmt, aq);
+    va_end(aq);
+
+    /* Create the buffer to hold the message */
+    char *buf = new char[msg_size + 1];
+
+    /* Store the message in the buffer */
+    va_copy(aq, ap);
+    vsnprintf(buf, msg_size + 1, fmt, aq);
+    va_end(aq);
+
+    /*
+     * Print the message lines prefixed with the supplied prefix.
+     * If the target stream is a terminal make the prefix colored.
+     */
+    bool use_color = isatty(fileno(stream));
+    const char *start_color(use_color ? color : "");
+    const char *end_color(use_color && *color ? terminal_color_normal : "");
+
+    std::string line;
+    std::stringstream ss(buf);
+
+    while(std::getline(ss, line)) {
+        fprintf(stream, "%s%s%s: %s\n", start_color, prefix, end_color,
+                line.c_str());
+    }
+
+    delete[] buf;
+}
+
 void
 Log::info(const char *fmt, ...)
 {
     va_list ap;
     va_start(ap, fmt);
-    vfprintf(stdout, fmt, ap);
+    if (Options::show_debug)
+        print_prefixed_message(stdout, terminal_color_cyan, "Info", fmt, ap);
+    else
+        vfprintf(stdout, fmt, ap);
     va_end(ap);
 }
 
@@ -48,7 +97,7 @@ 
         return;
     va_list ap;
     va_start(ap, fmt);
-    vfprintf(stdout, fmt, ap);
+    print_prefixed_message(stdout, terminal_color_yellow, "Debug", fmt, ap);
     va_end(ap);
 }
 
@@ -57,7 +106,7 @@ 
 {
     va_list ap;
     va_start(ap, fmt);
-    vfprintf(stderr, fmt, ap);
+    print_prefixed_message(stderr, terminal_color_red, "Error", fmt, ap);
     va_end(ap);
 }
 

=== modified file 'src/main.cpp'
--- src/main.cpp	2011-10-13 15:58:38 +0000
+++ src/main.cpp	2011-10-28 14:25:15 +0000
@@ -287,7 +287,7 @@ 
     }
 
     if (!canvas.init()) {
-        Log::error("Error: %s: Could not initialize canvas\n", __FUNCTION__);
+        Log::error("%s: Could not initialize canvas\n", __FUNCTION__);
         return 1;
     }