From c86ed49a7989adb3e2e3c42794e44609f12ce493 Mon Sep 17 00:00:00 2001
From: Fabian Bumberger <fbumberger@rim.com>
Date: Tue, 18 Sep 2012 14:15:18 -0400
Subject: [PATCH] Use slogger2 for logging on Blackberry instead of writing to
 stderr

Change-Id: Id0137400f18c8dfe7be7ca44670c16615401d424
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Reviewed-by: Rafael Roquetto <rafael.roquetto@kdab.com>
Reviewed-by: Peter Hartmann <phartmann@rim.com>
---
 config.tests/unix/slog2/slog2.cpp | 48 +++++++++++++++++++++++++
 config.tests/unix/slog2/slog2.pro |  4 +++
 configure                         | 34 ++++++++++++++++++
 qtbase.pro                        |  1 +
 src/corelib/global/global.pri     |  4 +++
 src/corelib/global/qglobal.cpp    | 12 ++++---
 src/corelib/global/qlogging.cpp   | 58 +++++++++++++++++++++++++++++++
 tools/configure/configureapp.cpp  | 22 ++++++++++++
 8 files changed, 178 insertions(+), 5 deletions(-)
 create mode 100644 config.tests/unix/slog2/slog2.cpp
 create mode 100644 config.tests/unix/slog2/slog2.pro

diff --git a/config.tests/unix/slog2/slog2.cpp b/config.tests/unix/slog2/slog2.cpp
new file mode 100644
index 00000000000..900d14a115d
--- /dev/null
+++ b/config.tests/unix/slog2/slog2.cpp
@@ -0,0 +1,48 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 - 2012 Research In Motion
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the config.tests of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** 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.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <slog2.h>
+
+int main(int, char **)
+{
+    slog2_set_default_buffer((slog2_buffer_t)-1);
+    return 0;
+}
diff --git a/config.tests/unix/slog2/slog2.pro b/config.tests/unix/slog2/slog2.pro
new file mode 100644
index 00000000000..9f5b34d6649
--- /dev/null
+++ b/config.tests/unix/slog2/slog2.pro
@@ -0,0 +1,4 @@
+SOURCES = slog2.cpp
+CONFIG -= qt
+mac:CONFIG -= app_bundle
+LIBS += -lslog2
diff --git a/configure b/configure
index 576eeacd168..a80bcdb586c 100755
--- a/configure
+++ b/configure
@@ -784,6 +784,7 @@ CFG_QML_DEBUG=yes
 CFG_JAVASCRIPTCORE_JIT=auto
 CFG_PKGCONFIG=auto
 CFG_STACK_PROTECTOR_STRONG=auto
+CFG_SLOG2=auto
 
 # Target architecture
 CFG_ARCH=
@@ -1784,6 +1785,13 @@ while [ "$#" -gt 0 ]; do
             UNKNOWN_OPT=yes
         fi
         ;;
+    slog2)
+        if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
+            CFG_SLOG2="$VAL"
+        else
+            UNKNOWN_OPT=yes
+        fi
+        ;;
     gstreamer)
         if [ "$VAL" = "yes" ] || [ "$VAL" = "no" ]; then
             CFG_GSTREAMER="$VAL"
@@ -3303,6 +3311,16 @@ EOF
 
 fi
 
+if [ "$XPLATFORM_QNX" = "yes" ]; then
+    cat << EOF
+
+    -no-slog2 .......... Do not compile with slog2 support.
+    -slog2 ............. Compile with slog2 support.
+
+EOF
+
+fi
+
 if [ "$BUILD_ON_MAC" = "yes" ]; then
     cat << EOF
 
@@ -4009,6 +4027,18 @@ if [ "$CFG_ZLIB" = "no" ]; then
     CFG_ZLIB=auto
     ZLIB_FORCED=yes
 fi
+
+if [ "$XPLATFORM_QNX" = "yes" ]; then
+    if [ "$CFG_SLOG2" != "no" ]; then
+        if compileTest unix/slog2 "slog2"; then
+            CFG_SLOG2=yes
+            QMAKE_CONFIG="$QMAKE_CONFIG slog2"
+        else
+            CFG_SLOG2=no
+        fi
+    fi
+fi
+
 if [ "$CFG_ZLIB" = "auto" ]; then
     if compileTest unix/zlib "zlib"; then
        CFG_ZLIB=system
@@ -6204,6 +6234,10 @@ echo "zlib support ........... $CFG_ZLIB"
 echo "Session management ..... $CFG_SM"
 echo "libudev support ........ $CFG_LIBUDEV"
 
+if [ "$XPLATFORM_QNX" = "yes" ]; then
+    echo "SLOG2 support .......... $CFG_SLOG2"
+fi
+
 if [ "$CFG_OPENGL" = "desktop" ]; then
     echo "OpenGL support ......... yes (Desktop OpenGL)"
 elif [ "$CFG_OPENGL" = "es2" ]; then
diff --git a/qtbase.pro b/qtbase.pro
index 8e655462309..48038eb3994 100644
--- a/qtbase.pro
+++ b/qtbase.pro
@@ -29,6 +29,7 @@ unix {
 			(cd config.tests/unix/sqlite2 && $(MAKE) distclean); \
 			(cd config.tests/unix/libjpeg && $(MAKE) distclean); \
 			(cd config.tests/unix/libpng && $(MAKE) distclean); \
+                        (cd config.tests/unix/slog2 && $(MAKE) distclean); \
 			(cd config.tests/x11/xcursor && $(MAKE) distclean); \
 			(cd config.tests/x11/xrender && $(MAKE) distclean); \
 			(cd config.tests/x11/xrandr && $(MAKE) distclean); \
diff --git a/src/corelib/global/global.pri b/src/corelib/global/global.pri
index 28a1a390b78..05a2461f1b6 100644
--- a/src/corelib/global/global.pri
+++ b/src/corelib/global/global.pri
@@ -35,3 +35,7 @@ linux*:!cross_compile:!static:!*-armcc* {
    DEFINES += ELF_INTERPRETER=\\\"$$system(readelf -l /bin/ls | perl -n -e \'$$prog\')\\\"
 }
 
+slog2 {
+    LIBS_PRIVATE += -lslog2
+    DEFINES += QT_USE_SLOG2
+}
diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp
index 805868f4c6f..c1501db33d4 100644
--- a/src/corelib/global/qglobal.cpp
+++ b/src/corelib/global/qglobal.cpp
@@ -2976,10 +2976,9 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
     Calls the message handler with the debug message \a message. If no
     message handler has been installed, the message is printed to
     stderr. Under Windows, the message is sent to the console, if it is a
-    console application; otherwise, it is sent to the debugger. This
-    function does nothing if \c QT_NO_DEBUG_OUTPUT was defined
-    during compilation.
-
+    console application; otherwise, it is sent to the debugger. On Blackberry the
+    message is sent to slogger2. This function does nothing if \c QT_NO_DEBUG_OUTPUT
+    was defined during compilation.
 
     If you pass the function a format string and a list of arguments,
     it works in similar way to the C printf() function. The format
@@ -3012,7 +3011,8 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
 
     Calls the message handler with the warning message \a message. If no
     message handler has been installed, the message is printed to
-    stderr. Under Windows, the message is sent to the debugger. This
+    stderr. Under Windows, the message is sent to the debugger.
+    On Blackberry the message is sent to slogger2. This
     function does nothing if \c QT_NO_WARNING_OUTPUT was defined
     during compilation; it exits if the environment variable \c
     QT_FATAL_WARNINGS is defined.
@@ -3046,6 +3046,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
     Calls the message handler with the critical message \a message. If no
     message handler has been installed, the message is printed to
     stderr. Under Windows, the message is sent to the debugger.
+    On Blackberry the message is sent to slogger2.
 
     This function takes a format string and a list of arguments,
     similar to the C printf() function. The format should be a Latin-1
@@ -3076,6 +3077,7 @@ bool QInternal::activateCallbacks(Callback cb, void **parameters)
     Calls the message handler with the fatal message \a message. If no
     message handler has been installed, the message is printed to
     stderr. Under Windows, the message is sent to the debugger.
+    On Blackberry the message is sent to slogger2.
 
     If you are using the \b{default message handler} this function will
     abort on Unix systems to create a core dump. On Windows, for debug builds,
diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp
index aec71112078..13bbdbead03 100644
--- a/src/corelib/global/qlogging.cpp
+++ b/src/corelib/global/qlogging.cpp
@@ -53,6 +53,9 @@
 #ifdef Q_OS_WIN
 #include <qt_windows.h>
 #endif
+#ifdef QT_USE_SLOG2
+#include <slog2.h>
+#endif
 
 #include <stdio.h>
 
@@ -660,6 +663,56 @@ void QMessagePattern::setPattern(const QString &pattern)
     memcpy(literals, literalsVar.constData(), literalsVar.size() * sizeof(const char*));
 }
 
+#if defined(QT_USE_SLOG2)
+#ifndef QT_LOG_CODE
+#define QT_LOG_CODE 9000
+#endif
+
+extern char *__progname;
+
+static void slog2_default_handler(QtMsgType msgType, const char *message)
+{
+    if (slog2_set_default_buffer((slog2_buffer_t)-1) == 0) {
+        slog2_buffer_set_config_t buffer_config;
+        slog2_buffer_t buffer_handle;
+
+        buffer_config.buffer_set_name = __progname;
+        buffer_config.num_buffers = 1;
+        buffer_config.verbosity_level = SLOG2_INFO;
+        buffer_config.buffer_config[0].buffer_name = "default";
+        buffer_config.buffer_config[0].num_pages = 8;
+
+        if (slog2_register(&buffer_config, &buffer_handle, 0) == -1) {
+            fprintf(stderr, "Error registering slogger2 buffer!\n");
+            fprintf(stderr, "%s", message);
+            fflush(stderr);
+            return;
+        }
+
+        // Set as the default buffer
+        slog2_set_default_buffer(buffer_handle);
+    }
+    int severity;
+    //Determines the severity level
+    switch (msgType) {
+    case QtDebugMsg:
+        severity = SLOG2_INFO;
+        break;
+    case QtWarningMsg:
+        severity = SLOG2_NOTICE;
+        break;
+    case QtCriticalMsg:
+        severity = SLOG2_WARNING;
+        break;
+    case QtFatalMsg:
+        severity = SLOG2_ERROR;
+        break;
+    }
+    //writes to the slog2 buffer
+    slog2c(NULL, QT_LOG_CODE, severity, message);
+}
+#endif // QT_USE_SLOG2
+
 Q_GLOBAL_STATIC(QMessagePattern, qMessagePattern)
 
 /*!
@@ -747,8 +800,13 @@ static void qDefaultMessageHandler(QtMsgType type, const QMessageLogContext &con
         return;
     }
 #endif // Q_OS_WIN
+
+#if defined(QT_USE_SLOG2)
+    slog2_default_handler(type, logMessage.toLocal8Bit().constData());
+#else
     fprintf(stderr, "%s", logMessage.toLocal8Bit().constData());
     fflush(stderr);
+#endif
 }
 
 /*!
diff --git a/tools/configure/configureapp.cpp b/tools/configure/configureapp.cpp
index d1244f0f597..f24782f2a76 100644
--- a/tools/configure/configureapp.cpp
+++ b/tools/configure/configureapp.cpp
@@ -228,6 +228,7 @@ Configure::Configure(int& argc, char** argv)
     dictionary[ "QT_ICONV" ]        = "auto";
     dictionary[ "QT_CUPS" ]         = "auto";
     dictionary[ "CFG_GCC_SYSROOT" ] = "yes";
+    dictionary[ "SLOG2" ]           = "no";
 
     //Only used when cross compiling.
     dictionary[ "QT_INSTALL_SETTINGS" ] = "/etc/xdg";
@@ -871,6 +872,10 @@ void Configure::parseCmdLine()
             dictionary[ "PLUGIN_MANIFESTS" ] = "no";
         } else if (configCmdLine.at(i) == "-plugin-manifests") {
             dictionary[ "PLUGIN_MANIFESTS" ] = "yes";
+        } else if (configCmdLine.at(i) == "-no-slog2") {
+            dictionary[ "SLOG2" ] = "no";
+        } else if (configCmdLine.at(i) == "-slog2") {
+            dictionary[ "SLOG2" ] = "yes";
         }
 
         // Work around compiler nesting limitation
@@ -1506,6 +1511,7 @@ void Configure::applySpecSpecifics()
         dictionary["DECORATIONS"]           = "default windows styled";
     } else if ((platform() == QNX) || (platform() == BLACKBERRY)) {
         dictionary["STACK_PROTECTOR_STRONG"] = "auto";
+        dictionary["SLOG2"]                 = "auto";
     }
 }
 
@@ -1713,6 +1719,11 @@ bool Configure::displayHelp()
         desc("FREETYPE", "no",   "-no-freetype",        "Do not compile in Freetype2 support.");
         desc("FREETYPE", "yes",  "-qt-freetype",        "Use the libfreetype bundled with Qt.");
         desc("FREETYPE", "yes",  "-system-freetype",    "Use the libfreetype provided by the system.");
+
+        if ((platform() == QNX) || (platform() == BLACKBERRY)) {
+            desc("SLOG2", "yes",  "-slog2",             "Compile with slog2 support.");
+            desc("SLOG2", "no",  "-no-slog2",           "Do not compile with slog2 support.");
+        }
 #endif
         // Qt\Windows only options go below here --------------------------------------------------------------------------------
         desc("\nQt for Windows only:\n\n");
@@ -2045,6 +2056,8 @@ bool Configure::checkAvailability(const QString &part)
         available = (platform() != WINDOWS) && (platform() != WINDOWS_CE) && tryCompileProject("unix/cups");
     } else if (part == "STACK_PROTECTOR_STRONG") {
         available = (platform() == QNX || platform() == BLACKBERRY) && compilerSupportsFlag("qcc -fstack-protector-strong");
+    } else if (part == "SLOG2") {
+        available = tryCompileProject("unix/slog2");
     }
 
     return available;
@@ -2161,6 +2174,10 @@ void Configure::autoDetection()
     if (dictionary["STACK_PROTECTOR_STRONG"] == "auto")
         dictionary["STACK_PROTECTOR_STRONG"] = checkAvailability("STACK_PROTECTOR_STRONG") ? "yes" : "no";
 
+    if ((platform() == QNX || platform() == BLACKBERRY) && dictionary["SLOG2"] == "auto") {
+        dictionary["SLOG2"] = checkAvailability("SLOG2") ? "yes" : "no";
+    }
+
     // Mark all unknown "auto" to the default value..
     for (QMap<QString,QString>::iterator i = dictionary.begin(); i != dictionary.end(); ++i) {
         if (i.value() == "auto")
@@ -2925,6 +2942,9 @@ void Configure::generateQConfigPri()
         if (dictionary["CROSS_COMPILE"] == "yes")
             configStream << " cross_compile";
 
+        if (dictionary[ "SLOG2" ] == "yes")
+            configStream << " slog2";
+
         if (dictionary["DIRECTWRITE"] == "yes")
             configStream << "directwrite";
 
@@ -3322,6 +3342,8 @@ void Configure::displayConfig()
     sout << "    FreeType support........" << dictionary[ "FREETYPE" ] << endl << endl;
     sout << "    PCRE support............" << dictionary[ "PCRE" ] << endl;
     sout << "    ICU support............." << dictionary[ "ICU" ] << endl;
+    if ((platform() == QNX) || (platform() == BLACKBERRY))
+        sout << "    SLOG2 support..........." << dictionary[ "SLOG2" ] << endl;
 
     sout << "Styles:" << endl;
     sout << "    Windows................." << dictionary[ "STYLE_WINDOWS" ] << endl;
-- 
GitLab