summaryrefslogtreecommitdiffstats
path: root/examples/logger/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/logger/main.cpp')
-rw-r--r--examples/logger/main.cpp143
1 files changed, 143 insertions, 0 deletions
diff --git a/examples/logger/main.cpp b/examples/logger/main.cpp
new file mode 100644
index 0000000..1443433
--- /dev/null
+++ b/examples/logger/main.cpp
@@ -0,0 +1,143 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
+**
+** This file is part of the demonstration applications of the logger module
+** of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** 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, Nokia gives you certain additional
+** rights. These rights are described in the Nokia 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.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtCore/QCoreApplication>
+#include <QtCore/QMutexLocker>
+#include <QtCore/QMutex>
+#include <QtCore/QFile>
+#include <QtCore/QThread>
+#include <QtCore/QDebug>
+#include <QtCore/QObject>
+#include <QtLogger/QtLogger>
+
+QT_LOG_CATEGORY(My_Category_A, "My.Category.A")
+QT_LOG_CATEGORY(My_Category_B, "My.Category.B")
+QT_LOG_CATEGORY(My_Category_C, "My.Category.C")
+
+#define LOGOUTPUTFILE "./logs.txt"
+#define LOGRULESFILE "./logrules.txt"
+
+//![1]
+QT_LOG_CATEGORY(NOKIA_DRIVER_USB, "Nokia.driver.usb")
+QT_LOG_CATEGORY(NOKIA_DRIVER_EVENT, "Nokia.driver.event")
+//![1]
+
+QMutex mutex;
+QMessageHandler oldMessageHandler;
+
+QT_LOGGER_USE_NAMESPACE
+
+//Messagehandler to write into a file
+static void myCustomMessageHandler(QtMsgType type, const QMessageLogContext &context, const char *msg)
+{
+ //Lock against other threads accessing the file
+ QMutexLocker locker(&mutex);
+ QFile outFile(LOGOUTPUTFILE);
+ if (outFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
+ QByteArray message;
+// message.append("<");
+// message.append(context.category);
+// message.append(">");
+
+ message.append(msg);
+ message.append('\n');
+ outFile.write(message);
+ }
+ oldMessageHandler(type, context, msg);
+}
+
+class LogThread : public QThread
+{
+public:
+ LogThread() {}
+
+protected:
+ void run();
+};
+
+void LogThread::run()
+{
+ for (int i = 0; i < 60; i++) {
+ qDebug() << "loop " << i;
+//![3]
+ qCDebug(NOKIA_DRIVER_USB) << "USB legacy loaded";
+//![3]
+ sleep(1);
+ qWarning() << "loop " << i;
+ sleep(1);
+ qCritical() << "loop " << i;
+ sleep(1);
+ qCDebug(My_Category_A) << "loop " << i;
+ qCDebug(My_Category_B) << "loop " << i;
+ qCDebug(My_Category_C) << "loop " << i;
+ sleep(1);
+ qCWarning(My_Category_A) << "loop " << i;
+ qCWarning(My_Category_B) << "loop " << i;
+ qCWarning(My_Category_C) << "loop " << i;
+ sleep(1);
+ qCCritical(My_Category_A) << "loop " << i;
+ qCCritical(My_Category_B) << "loop " << i;
+ qCCritical(My_Category_C) << "loop " << i;
+ sleep(1);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication a(argc, argv);
+
+// qputenv ("QT_MESSAGE_PATTERN", QString("%{category}: %{type},%{message}").toLatin1());
+ qputenv ("QT_MESSAGE_PATTERN", QString("%{type} %{message}").toLatin1());
+ oldMessageHandler = qInstallMessageHandler(myCustomMessageHandler);
+
+ //delete old logoutput file
+ QFile::remove(LOGOUTPUTFILE);
+
+ fprintf(stdout, "%s\r\n", "Now you can open the logrules.txt file and de/activate categories");
+//![2]
+ QtLogger::qSetLoggingRulesFile("./logrules.txt");
+//![2]
+ LogThread logthread;
+ logthread.start();
+
+ return a.exec();
+}