aboutsummaryrefslogtreecommitdiffstats
path: root/src/app/shared
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@digia.com>2013-01-31 18:23:59 +0100
committerChristian Kandeler <christian.kandeler@digia.com>2013-02-06 10:36:05 +0100
commitaea6cbfa9d96f3f76bbc3b81362c54c9322b3fcb (patch)
treec140b905b53b01e509ad923a12e5b31f043f3361 /src/app/shared
parente5545733e8f63f62d499e73a27a72fc898a58e4e (diff)
Remove global log sink.
Applications may not want to use the same log sink for all builds. The logging facility is also decoupled from the command-line client in other ways: - The LogWriter modifiers for output channel and text color are gone, since this type of decision should not be made by low-level code. Instead, the "highlight" string can be forwarded to the log sink. - The console logger now lives in app/shared, as it must never be used by library code. Change-Id: I8863a554c9b74577320ef23f6f934a74e0f0cbb0 Reviewed-by: Joerg Bornemann <joerg.bornemann@digia.com>
Diffstat (limited to 'src/app/shared')
-rw-r--r--src/app/shared/logging/coloredoutput.cpp91
-rw-r--r--src/app/shared/logging/coloredoutput.h62
-rw-r--r--src/app/shared/logging/consolelogger.cpp101
-rw-r--r--src/app/shared/logging/consolelogger.h77
-rw-r--r--src/app/shared/logging/logging.pri2
5 files changed, 333 insertions, 0 deletions
diff --git a/src/app/shared/logging/coloredoutput.cpp b/src/app/shared/logging/coloredoutput.cpp
new file mode 100644
index 000000000..61be733db
--- /dev/null
+++ b/src/app/shared/logging/coloredoutput.cpp
@@ -0,0 +1,91 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Build Suite.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#include "coloredoutput.h"
+#include <QtGlobal>
+#ifdef Q_OS_WIN32
+# include <qt_windows.h>
+#endif
+
+#include <cstdarg>
+
+#ifdef Q_OS_UNIX
+#include <unistd.h>
+#endif
+
+void printfColored(TextColor color, const char *str, va_list vl)
+{
+ fprintfColored(color, stdout, str, vl);
+}
+
+void printfColored(TextColor color, const char *str, ...)
+{
+ va_list vl;
+ va_start(vl, str);
+ printfColored(color, str, vl);
+ va_end(vl);
+}
+
+void fprintfColored(TextColor color, FILE *file, const char *str, va_list vl)
+{
+#if defined(Q_OS_UNIX)
+ if (color != TextColorDefault && isatty(fileno(file))) {
+ unsigned char bright = (color & TextColorBright) >> 3;
+ fprintf(file, "\033[%d;%dm", bright, 30 + (color & ~TextColorBright));
+ vfprintf(file, str, vl);
+ fprintf(stdout, "\033[0m");
+ fprintf(stderr, "\033[0m");
+ } else
+#elif defined(Q_OS_WIN32)
+ HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
+ CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
+ if (color != TextColorDefault
+ && hStdout != INVALID_HANDLE_VALUE
+ && GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
+ {
+ WORD bgrColor = ((color & 1) << 2) | (color & 2) | ((color & 4) >> 2); // BGR instead of RGB.
+ if (color & TextColorBright)
+ bgrColor += FOREGROUND_INTENSITY;
+ SetConsoleTextAttribute(hStdout, (csbiInfo.wAttributes & 0xf0) | bgrColor);
+ vfprintf(file, str, vl);
+ SetConsoleTextAttribute(hStdout, csbiInfo.wAttributes);
+ } else
+#endif
+ {
+ vfprintf(file, str, vl);
+ }
+}
+
+void fprintfColored(TextColor color, FILE *file, const char *str, ...)
+{
+ va_list vl;
+ va_start(vl, str);
+ fprintfColored(color, file, str, vl);
+ va_end(vl);
+}
diff --git a/src/app/shared/logging/coloredoutput.h b/src/app/shared/logging/coloredoutput.h
new file mode 100644
index 000000000..57677a79f
--- /dev/null
+++ b/src/app/shared/logging/coloredoutput.h
@@ -0,0 +1,62 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Build Suite.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#ifndef QBS_COLOREDOUTPUT_H
+#define QBS_COLOREDOUTPUT_H
+
+#include <cstdio>
+#include <cstdarg>
+
+// http://en.wikipedia.org/wiki/ANSI_escape_code#Colors
+enum TextColor {
+ TextColorDefault = -1,
+ TextColorBlack = 0,
+ TextColorDarkRed = 1,
+ TextColorDarkGreen = 2,
+ TextColorDarkBlue = 4,
+ TextColorDarkCyan = TextColorDarkGreen | TextColorDarkBlue,
+ TextColorDarkMagenta = TextColorDarkRed | TextColorDarkBlue,
+ TextColorDarkYellow = TextColorDarkRed | TextColorDarkGreen,
+ TextColorGray = 7,
+ TextColorBright = 8,
+ TextColorRed = TextColorDarkRed | TextColorBright,
+ TextColorGreen = TextColorDarkGreen | TextColorBright,
+ TextColorBlue = TextColorDarkBlue | TextColorBright,
+ TextColorCyan = TextColorDarkCyan | TextColorBright,
+ TextColorMagenta = TextColorDarkMagenta | TextColorBright,
+ TextColorYellow = TextColorDarkYellow | TextColorBright,
+ TextColorWhite = TextColorGray | TextColorBright
+};
+
+void printfColored(TextColor color, const char *str, va_list vl);
+void printfColored(TextColor color, const char *str, ...);
+void fprintfColored(TextColor color, FILE *file, const char *str, va_list vl);
+void fprintfColored(TextColor color, FILE *file, const char *str, ...);
+
+#endif // QBS_COLOREDOUTPUT_H
diff --git a/src/app/shared/logging/consolelogger.cpp b/src/app/shared/logging/consolelogger.cpp
new file mode 100644
index 000000000..5faf0b4c5
--- /dev/null
+++ b/src/app/shared/logging/consolelogger.cpp
@@ -0,0 +1,101 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Build Suite.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#include "consolelogger.h"
+
+#include <tools/preferences.h>
+#include <tools/settings.h>
+
+#include <cstdio>
+
+static QHash<QString, TextColor> setupColorTable()
+{
+ QHash<QString, TextColor> colorTable;
+ colorTable["compiler"] = TextColorDefault;
+ colorTable["linker"] = TextColorDarkGreen;
+ colorTable["codegen"] = TextColorDarkYellow;
+ colorTable["filegen"] = TextColorDarkYellow;
+ return colorTable;
+}
+
+ConsoleLogSink::ConsoleLogSink() : m_coloredOutputEnabled(true), m_enabled(true)
+{
+}
+
+void ConsoleLogSink::doPrintMessage(qbs::LoggerLevel level, const QString &message,
+ const QString &tag)
+{
+ if (!m_enabled)
+ return;
+
+ FILE * const file = level == qbs::LoggerInfo ? stdout : stderr;
+
+ const QByteArray data = logLevelTag(level);
+ TextColor color = TextColorDefault;
+ switch (level) {
+ case qbs::LoggerError:
+ color = TextColorRed;
+ break;
+ case qbs::LoggerWarning:
+ color = TextColorYellow;
+ break;
+ default:
+ break;
+ }
+
+ fprintfWrapper(color, file, data);
+ static QHash<QString, TextColor> colorTable = setupColorTable();
+ fprintfWrapper(colorTable.value(tag, TextColorDefault), file, "%s\n",
+ message.toLocal8Bit().constData());
+ fflush(file);
+}
+
+void ConsoleLogSink::fprintfWrapper(TextColor color, FILE *file, const char *str, ...)
+{
+ va_list vl;
+ va_start(vl, str);
+ if (m_coloredOutputEnabled)
+ fprintfColored(color, file, str, vl);
+ else
+ vfprintf(file, str, vl);
+ va_end(vl);
+}
+
+
+ConsoleLogger &ConsoleLogger::instance(qbs::Settings *settings)
+{
+ static ConsoleLogger logger(settings);
+ return logger;
+}
+
+ConsoleLogger::ConsoleLogger(qbs::Settings *settings) : Logger(&m_logSink)
+{
+ if (settings)
+ m_logSink.setColoredOutputEnabled(qbs::Preferences(settings).useColoredOutput());
+}
diff --git a/src/app/shared/logging/consolelogger.h b/src/app/shared/logging/consolelogger.h
new file mode 100644
index 000000000..9e55c8d73
--- /dev/null
+++ b/src/app/shared/logging/consolelogger.h
@@ -0,0 +1,77 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Build Suite.
+**
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+****************************************************************************/
+
+#ifndef QBS_LOGSINK_H
+#define QBS_LOGSINK_H
+
+#include "coloredoutput.h"
+
+#include <logging/logger.h>
+
+namespace qbs { class Settings; }
+
+class ConsoleLogSink : public qbs::ILogSink
+{
+public:
+ ConsoleLogSink();
+
+ void setColoredOutputEnabled(bool enabled) { m_coloredOutputEnabled = enabled; }
+ void setEnabled(bool enabled) { m_enabled = enabled; }
+
+private:
+ void doPrintMessage(qbs::LoggerLevel level, const QString &message, const QString &tag);
+ void fprintfWrapper(TextColor color, FILE *file, const char *str, ...);
+
+private:
+ bool m_coloredOutputEnabled;
+ bool m_enabled;
+};
+
+
+class ConsoleLogger : public qbs::Internal::Logger
+{
+public:
+ static ConsoleLogger &instance(qbs::Settings *settings = 0);
+ ConsoleLogSink *logSink() { return &m_logSink; }
+
+private:
+ ConsoleLogger(qbs::Settings *settings);
+
+ ConsoleLogSink m_logSink;
+};
+
+inline qbs::Internal::LogWriter qbsError() {
+ return ConsoleLogger::instance().qbsLog(qbs::LoggerError);
+}
+inline qbs::Internal::LogWriter qbsWarning() { return ConsoleLogger::instance().qbsWarning(); }
+inline qbs::Internal::LogWriter qbsInfo() { return ConsoleLogger::instance().qbsInfo(); }
+inline qbs::Internal::LogWriter qbsDebug() { return ConsoleLogger::instance().qbsDebug(); }
+inline qbs::Internal::LogWriter qbsTrace() { return ConsoleLogger::instance().qbsTrace(); }
+
+#endif // QBS_LOGSINK_H
diff --git a/src/app/shared/logging/logging.pri b/src/app/shared/logging/logging.pri
new file mode 100644
index 000000000..e24f33e10
--- /dev/null
+++ b/src/app/shared/logging/logging.pri
@@ -0,0 +1,2 @@
+HEADERS += $$PWD/consolelogger.h $$PWD/coloredoutput.h
+SOURCES += $$PWD/consolelogger.cpp $$PWD/coloredoutput.cpp