From patchwork Mon Oct 31 21:18:12 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: 4880 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 68DF123E06 for ; Mon, 31 Oct 2011 21:18: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 3E29AA18341 for ; Mon, 31 Oct 2011 21:18:19 +0000 (UTC) Received: by faan26 with SMTP id n26so9103635faa.11 for ; Mon, 31 Oct 2011 14:18:19 -0700 (PDT) Received: by 10.223.5.82 with SMTP id 18mr13660742fau.27.1320095899011; Mon, 31 Oct 2011 14:18:19 -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.1.71 with SMTP id 7cs47419lak; Mon, 31 Oct 2011 14:18:17 -0700 (PDT) Received: by 10.227.198.85 with SMTP id en21mr19993594wbb.11.1320095893965; Mon, 31 Oct 2011 14:18:13 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id fn7si13854653wbb.28.2011.10.31.14.18.13 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 31 Oct 2011 14:18:13 -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 1RKzFF-0004bD-3I for ; Mon, 31 Oct 2011 21:18:13 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id F3E7DE04E8 for ; Mon, 31 Oct 2011 21:18:12 +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: <20111031211812.1044.74249.launchpad@ackee.canonical.com> Date: Mon, 31 Oct 2011 21:18:12 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="14197"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: 7111bcfdfd6f0355462d5842e3771f6a80696ef4 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: 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 === 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 #include +#include +#include #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; }