Merge lp:~agateau/unity-2d/debug-tools into lp:unity-2d/3.0

Proposed by Aurélien Gâteau
Status: Merged
Approved by: Florian Boucault
Approved revision: 431
Merged at revision: 446
Proposed branch: lp:~agateau/unity-2d/debug-tools
Merge into: lp:unity-2d/3.0
Diff against target: 376 lines (+186/-34)
10 files modified
config.h.in (+0/-20)
launcher/app/launcher.cpp (+2/-2)
libunity-2d-private/src/CMakeLists.txt (+6/-0)
libunity-2d-private/src/debug_p.h (+3/-6)
libunity-2d-private/src/unity2ddebug.cpp (+127/-0)
libunity-2d-private/src/unity2ddebug.h (+39/-0)
panel/app/main.cpp (+2/-2)
places/app/places.cpp (+2/-2)
spread/app/CMakeLists.txt (+2/-0)
spread/app/spread.cpp (+3/-2)
To merge this branch: bzr merge lp:~agateau/unity-2d/debug-tools
Reviewer Review Type Date Requested Status
Florian Boucault (community) Approve
Review via email: mp+52430@code.launchpad.net

Commit message

[plumbing] Unify message handling between Qt, GTK and part of the code from debug_p.h

Makes it possible to get timestamps as well as colored output. Timestamps will make it easier to investigate bugs like 703542.

Description of the change

Unify message handling between Qt, GTK and part of the code from debug_p.h

Makes it possible to get timestamps as well as colored output. Timestamps will make it easier to investigate bugs like 703542.

To post a comment you must log in.
lp:~agateau/unity-2d/debug-tools updated
431. By Aurélien Gâteau

Use fputs to avoid flushing stderr before the end of line

flushing before EOL can lead to mixed log lines

Revision history for this message
Florian Boucault (fboucault) wrote :

Good job!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.h.in'
2--- config.h.in 2011-01-27 23:00:24 +0000
3+++ config.h.in 2011-03-08 10:13:05 +0000
4@@ -3,8 +3,6 @@
5 #define UNITY_DIR "${UNITY_DIR}"
6
7 #include <QCoreApplication>
8-#include <cstdio>
9-#include <cstdlib>
10
11 inline bool isRunningInstalled() {
12 static bool cachedResult = QCoreApplication::applicationDirPath() == INSTALL_PREFIX "/bin";
13@@ -26,21 +24,3 @@
14 return QString(QCoreApplication::applicationDirPath()+"/../");
15 }
16 }
17-
18-inline void globalMessageHandler(QtMsgType type, const char *message)
19-{
20- switch (type) {
21- case QtDebugMsg:
22- fprintf(stderr, "%s: [DEBUG] %s\n", qPrintable(QCoreApplication::applicationName()), message);
23- break;
24- case QtWarningMsg:
25- fprintf(stderr, "%s: [WARNING] %s\n", qPrintable(QCoreApplication::applicationName()), message);
26- break;
27- case QtCriticalMsg:
28- fprintf(stderr, "%s: [CRITICAL] %s\n", qPrintable(QCoreApplication::applicationName()), message);
29- break;
30- case QtFatalMsg:
31- fprintf(stderr, "%s: [FATAL] %s\n", qPrintable(QCoreApplication::applicationName()), message);
32- abort();
33- }
34-}
35
36=== modified file 'launcher/app/launcher.cpp'
37--- launcher/app/launcher.cpp 2011-02-24 01:13:42 +0000
38+++ launcher/app/launcher.cpp 2011-03-08 10:13:05 +0000
39@@ -41,6 +41,7 @@
40 #include "launcherview.h"
41 #include "launchercontrol.h"
42 #include "hidemodecontroller.h"
43+#include "unity2ddebug.h"
44 #include "unity2dpanel.h"
45 #include "gesturehandler.h"
46
47@@ -50,8 +51,7 @@
48 (gtk_icon_theme_get_default) and requires a call to gtk_init */
49 gtk_init(&argc, &argv);
50
51- QApplication::setApplicationName("Unity 2D Launcher");
52- qInstallMsgHandler(globalMessageHandler);
53+ Unity2dDebug::installHandlers();
54
55 /* Forcing graphics system to 'raster' instead of the default 'native'
56 which on X11 is 'XRender'.
57
58=== modified file 'libunity-2d-private/src/CMakeLists.txt'
59--- libunity-2d-private/src/CMakeLists.txt 2011-02-24 01:13:42 +0000
60+++ libunity-2d-private/src/CMakeLists.txt 2011-03-08 10:13:05 +0000
61@@ -1,3 +1,6 @@
62+# Dependencies
63+pkg_check_modules(GLIB REQUIRED glib-2.0)
64+
65 # Sources
66 set(libunity-2d-private_SRCS
67 gettexttranslator.cpp
68@@ -7,6 +10,7 @@
69 hotkey.cpp
70 mousearea.cpp
71 unity2dapplication.cpp
72+ unity2ddebug.cpp
73 unity2dpanel.cpp
74 mimedata.cpp
75 dragdropevent.cpp
76@@ -19,6 +23,7 @@
77 include_directories(
78 ${CMAKE_CURRENT_BINARY_DIR}
79 ${CMAKE_CURRENT_SOURCE_DIR}
80+ ${GLIB_INCLUDE_DIRS}
81 )
82
83 add_library(unity-2d-private SHARED ${libunity-2d-private_SRCS})
84@@ -32,6 +37,7 @@
85 ${QT_QTCORE_LIBRARIES}
86 ${QT_QTDBUS_LIBRARIES}
87 ${X11_LIBRARIES}
88+ ${GLIB_LDFLAGS}
89 )
90
91 # Install
92
93=== modified file 'libunity-2d-private/src/debug_p.h'
94--- libunity-2d-private/src/debug_p.h 2011-02-10 00:48:58 +0000
95+++ libunity-2d-private/src/debug_p.h 2011-03-08 10:13:05 +0000
96@@ -19,14 +19,11 @@
97
98 #include <QDebug>
99
100-#define _UQ_BLUE "\033[34m"
101-#define _UQ_RED "\033[31m"
102-#define _UQ_RESET "\033[0m"
103-#define _UQ_TRACE(level, color) (level().nospace() << color << __PRETTY_FUNCTION__ << _UQ_RESET ":").space()
104+#define _UQ_TRACE(level) (level().nospace() << __PRETTY_FUNCTION__ << ":").space()
105
106 // Simple macros to get KDebug like support
107-#define UQ_DEBUG _UQ_TRACE(qDebug, _UQ_BLUE)
108-#define UQ_WARNING _UQ_TRACE(qWarning, _UQ_RED)
109+#define UQ_DEBUG _UQ_TRACE(qDebug)
110+#define UQ_WARNING _UQ_TRACE(qWarning)
111
112 // Log a variable name and value
113 #define UQ_VAR(var) UQ_DEBUG << #var ":" << var
114
115=== added file 'libunity-2d-private/src/unity2ddebug.cpp'
116--- libunity-2d-private/src/unity2ddebug.cpp 1970-01-01 00:00:00 +0000
117+++ libunity-2d-private/src/unity2ddebug.cpp 2011-03-08 10:13:05 +0000
118@@ -0,0 +1,127 @@
119+/*
120+ * This file is part of unity-2d
121+ *
122+ * Copyright 2011 Canonical Ltd.
123+ *
124+ * Authors:
125+ * - Aurélien Gâteau <aurelien.gateau@canonical.com>
126+ *
127+ * This program is free software; you can redistribute it and/or modify
128+ * it under the terms of the GNU General Public License as published by
129+ * the Free Software Foundation; version 3.
130+ *
131+ * This program is distributed in the hope that it will be useful,
132+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
133+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134+ * GNU General Public License for more details.
135+ *
136+ * You should have received a copy of the GNU General Public License
137+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
138+ */
139+// Self
140+#include "unity2ddebug.h"
141+
142+// Local
143+
144+// Qt
145+#include <QCoreApplication>
146+#include <QTime>
147+
148+// Glib
149+#include <glib.h>
150+
151+// libc
152+#include <cstdio>
153+#include <cstdlib>
154+
155+namespace Unity2dDebug
156+{
157+
158+static const char* COLOR_BLUE = "\033[34m";
159+static const char* COLOR_RED = "\033[31m";
160+
161+static bool getenvBool(const char* name, bool defaultValue)
162+{
163+ QByteArray value = qgetenv(name);
164+ if (value.isEmpty()) {
165+ return defaultValue;
166+ }
167+ return value != "0";
168+}
169+
170+static void unity2dQtHandlerPrint(const char* color, const char* level, const char* message)
171+{
172+ static bool useColor = isatty(fileno(stderr)) && getenvBool("UNITY2D_DEBUG_COLOR", true);
173+ if (useColor) {
174+ fprintf(stderr, "%s[%s]\033[0m %s\n", color, level, message);
175+ } else {
176+ fprintf(stderr, "[%s] %s\n", level, message);
177+ }
178+}
179+
180+static void unity2dQtHandler(QtMsgType type, const char *message)
181+{
182+ static QByteArray name = QCoreApplication::applicationFilePath().section("/", -1).toLocal8Bit();
183+ static bool useTimeStamp = getenvBool("UNITY2D_DEBUG_TIMESTAMP", false);
184+
185+ // We use fputs here because we don't want stderr to be flushed before the
186+ // end of the line
187+ if (useTimeStamp) {
188+ QString timeStr = QTime::currentTime().toString("HH:mm:ss.zzz: ");
189+ fputs(qPrintable(timeStr), stderr);
190+ }
191+
192+ fputs(name.constData(), stderr);
193+ fputs(": ", stderr);
194+
195+ switch (type) {
196+ case QtDebugMsg:
197+ unity2dQtHandlerPrint(COLOR_BLUE, "DEBUG", message);
198+ break;
199+ case QtWarningMsg:
200+ unity2dQtHandlerPrint(COLOR_RED, "WARNING", message);
201+ break;
202+ case QtCriticalMsg:
203+ unity2dQtHandlerPrint(COLOR_RED, "CRITICAL", message);
204+ break;
205+ case QtFatalMsg:
206+ unity2dQtHandlerPrint(COLOR_RED, "FATAL", message);
207+ abort();
208+ }
209+}
210+
211+static void unity2dGlibHandler(const gchar* domain, GLogLevelFlags level, const gchar* message, gpointer /* user_data */)
212+{
213+ switch (level & G_LOG_LEVEL_MASK) {
214+ case G_LOG_LEVEL_ERROR:
215+ qFatal("%s: %s", domain, message);
216+ break;
217+ case G_LOG_LEVEL_CRITICAL:
218+ qCritical("%s: %s", domain, message);
219+ break;
220+ case G_LOG_LEVEL_WARNING:
221+ qWarning("%s: %s", domain, message);
222+ break;
223+ case G_LOG_LEVEL_MESSAGE:
224+ case G_LOG_LEVEL_INFO:
225+ case G_LOG_LEVEL_DEBUG:
226+ qDebug("%s: %s", domain, message);
227+ break;
228+ default:
229+ qWarning("(Unknown level value %d!) %s: %s", int(level), domain, message);
230+ break;
231+ }
232+
233+ if (level & G_LOG_FLAG_FATAL) {
234+ // Could happen if g_log_set_fatal_mask() has been called.
235+ abort();
236+ }
237+}
238+
239+void installHandlers()
240+{
241+ g_log_set_default_handler(unity2dGlibHandler, 0);
242+ qInstallMsgHandler(unity2dQtHandler);
243+}
244+
245+} // namespace
246
247=== added file 'libunity-2d-private/src/unity2ddebug.h'
248--- libunity-2d-private/src/unity2ddebug.h 1970-01-01 00:00:00 +0000
249+++ libunity-2d-private/src/unity2ddebug.h 2011-03-08 10:13:05 +0000
250@@ -0,0 +1,39 @@
251+/*
252+ * This file is part of unity-2d
253+ *
254+ * Copyright 2011 Canonical Ltd.
255+ *
256+ * Authors:
257+ * - Aurélien Gâteau <aurelien.gateau@canonical.com>
258+ *
259+ * This program is free software; you can redistribute it and/or modify
260+ * it under the terms of the GNU General Public License as published by
261+ * the Free Software Foundation; version 3.
262+ *
263+ * This program is distributed in the hope that it will be useful,
264+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
265+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
266+ * GNU General Public License for more details.
267+ *
268+ * You should have received a copy of the GNU General Public License
269+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
270+ */
271+#ifndef UNITY2DDEBUG_H
272+#define UNITY2DDEBUG_H
273+
274+// Local
275+
276+// Qt
277+#include <QObject>
278+
279+/**
280+ *
281+ */
282+namespace Unity2dDebug
283+{
284+
285+void installHandlers();
286+
287+}
288+
289+#endif /* UNITY2DDEBUG_H */
290
291=== modified file 'panel/app/main.cpp'
292--- panel/app/main.cpp 2011-02-18 15:47:38 +0000
293+++ panel/app/main.cpp 2011-03-08 10:13:05 +0000
294@@ -32,6 +32,7 @@
295 // Unity
296 #include <gettexttranslator.h>
297 #include <gnomesessionclient.h>
298+#include <unity2ddebug.h>
299 #include <unity2dpanel.h>
300 #include <unity2dapplication.h>
301 #include <unity2dstyle.h>
302@@ -88,8 +89,7 @@
303 {
304 ThemeEngineHandler handler;
305
306- QApplication::setApplicationName("Unity 2D Panel");
307- qInstallMsgHandler(globalMessageHandler);
308+ Unity2dDebug::installHandlers();
309
310 /* Forcing graphics system to 'raster' instead of the default 'native'
311 which on X11 is 'XRender'.
312
313=== modified file 'places/app/places.cpp'
314--- places/app/places.cpp 2011-02-24 01:13:42 +0000
315+++ places/app/places.cpp 2011-03-08 10:13:05 +0000
316@@ -35,6 +35,7 @@
317
318 // unity-2d
319 #include <gettexttranslator.h>
320+#include <unity2ddebug.h>
321
322 #include "dashdeclarativeview.h"
323 #include "config.h"
324@@ -70,8 +71,7 @@
325 {
326 /* gtk needs to be inited, otherwise we get an assert failure in gdk */
327 gtk_init(&argc, &argv);
328- QApplication::setApplicationName("Unity 2D Dash");
329- qInstallMsgHandler(globalMessageHandler);
330+ Unity2dDebug::installHandlers();
331
332 /* Forcing graphics system to 'raster' instead of the default 'native'
333 which on X11 is 'XRender'.
334
335=== modified file 'spread/app/CMakeLists.txt'
336--- spread/app/CMakeLists.txt 2011-01-14 20:45:02 +0000
337+++ spread/app/CMakeLists.txt 2011-03-08 10:13:05 +0000
338@@ -29,6 +29,7 @@
339 ${CMAKE_CURRENT_SOURCE_DIR}
340 ${CMAKE_CURRENT_BINARY_DIR}
341 ${GTK_INCLUDE_DIRS}
342+ ${libunity-2d-private_SOURCE_DIR}/src
343 )
344
345 target_link_libraries(unity-2d-spread
346@@ -39,6 +40,7 @@
347 ${GTK_LDFLAGS}
348 ${X11_Xext_LIB}
349 ${X11_X11_LIB}
350+ unity-2d-private
351 )
352
353 # Install
354
355=== modified file 'spread/app/spread.cpp'
356--- spread/app/spread.cpp 2011-02-10 08:40:03 +0000
357+++ spread/app/spread.cpp 2011-03-08 10:13:05 +0000
358@@ -27,6 +27,8 @@
359 #include "spreadview.h"
360 #include "spreadcontrol.h"
361
362+#include <unity2ddebug.h>
363+
364 #include "config.h"
365
366 int main(int argc, char *argv[])
367@@ -35,8 +37,7 @@
368 (gtk_icon_theme_get_default) and requires a call to gtk_init */
369 gtk_init(&argc, &argv);
370
371- QApplication::setApplicationName("Unity 2D Spread");
372- qInstallMsgHandler(globalMessageHandler);
373+ Unity2dDebug::installHandlers();
374
375 /* Forcing graphics system to 'raster' instead of the default 'native'
376 which on X11 is 'XRender'.

Subscribers

People subscribed via source and target branches