diff mbox

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

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

Commit Message

alexandros.frantzis@linaro.org Nov. 1, 2011, 1:58 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: Tue 2011-11-01 15:56:35 +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/log.h
  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-11-01 13:46:05 +0000
@@ -23,6 +23,8 @@ 
 
 #include <cstdio>
 #include <cstdarg>
+#include <string>
+#include <sstream>
 
 #include "options.h"
 #include "log.h"
@@ -32,12 +34,73 @@ 
 #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)) {
+        /* 
+         * If this line is a continuation of a previous log message
+         * just print the line plainly.
+         */
+        if (line[0] == LOG_CONTINUE[0]) {
+            fprintf(stream, "%s", line.c_str() + 1);
+        }
+        else {
+            /* Normal line, emit the prefix. */
+            fprintf(stream, "%s%s%s: %s", start_color, prefix, end_color,
+                    line.c_str());
+        }
+
+        /* Only emit a newline if the original message has it. */
+        if (!(ss.rdstate() & std::stringstream::eofbit))
+            fputs("\n", stream);
+    }
+
+    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 +111,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 +120,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/log.h'
--- src/log.h	2011-06-15 11:33:20 +0000
+++ src/log.h	2011-11-01 13:46:05 +0000
@@ -24,6 +24,13 @@ 
 #ifndef LOG_H_
 #define LOG_H_
 
+/** 
+ * A prefix that informs the logging infrastructure that the log
+ * message is a continuation of a previous log message to be put
+ * on the same line.
+ */
+#define LOG_CONTINUE "\x10"
+
 class Log
 {
 public:

=== modified file 'src/main.cpp'
--- src/main.cpp	2011-10-13 15:58:38 +0000
+++ src/main.cpp	2011-11-01 13:46:05 +0000
@@ -198,7 +198,7 @@ 
                 canvas.update();
             }
 
-            Log::info(" FPS: %u\n", scene.average_fps());
+            Log::info(LOG_CONTINUE" FPS: %u\n", scene.average_fps());
             score += scene.average_fps();
             benchmarks_run++;
         }
@@ -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;
     }