From patchwork Tue Nov 1 13:58:15 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: alexandros.frantzis@linaro.org X-Patchwork-Id: 4886 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 46EBA23E06 for ; Tue, 1 Nov 2011 13:58:19 +0000 (UTC) Received: from mail-fx0-f52.google.com (mail-fx0-f52.google.com [209.85.161.52]) by fiordland.canonical.com (Postfix) with ESMTP id 2F680A18442 for ; Tue, 1 Nov 2011 13:58:19 +0000 (UTC) Received: by faan26 with SMTP id n26so9987164faa.11 for ; Tue, 01 Nov 2011 06:58:19 -0700 (PDT) Received: by 10.223.16.82 with SMTP id n18mr971463faa.2.1320155898635; Tue, 01 Nov 2011 06:58:18 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.152.14.103 with SMTP id o7cs10115lac; Tue, 1 Nov 2011 06:58:18 -0700 (PDT) Received: by 10.14.2.132 with SMTP id 4mr1720656eef.213.1320155896222; Tue, 01 Nov 2011 06:58:16 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id v44si15927955wec.96.2011.11.01.06.58.15 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 01 Nov 2011 06:58:16 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1RLEr1-0003lb-MK for ; Tue, 01 Nov 2011 13:58:15 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id 96D44E03B3 for ; Tue, 1 Nov 2011 13:58:15 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: glmark2 X-Launchpad-Branch: ~glmark2-dev/glmark2/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 160 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~glmark2-dev/glmark2/trunk] Rev 160: Log: Add colored prefixes to log messages. Message-Id: <20111101135815.31626.16557.launchpad@ackee.canonical.com> Date: Tue, 01 Nov 2011 13:58:15 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="14214"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: a84038c5c417fc6134b1e3ca44ada1c8a6816fa3 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 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 === 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 #include +#include +#include #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; }