1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
36 #include <qapplication.h>
47 QLogger* QLogger::m_instance;
49 void QLogger::loggerMessageOutput(QtMsgType type, const char *msg)
53 if (!QLogger::instance()->m_silent)
54 QLogger::instance()->output(msg);
57 fprintf(stderr, "Warning: %s\n", msg);
60 fprintf(stderr, "Critical: %s\n", msg);
63 fprintf(stderr, "Fatal: %s\n", msg);
68 void QLogger::setLevel(int level)
70 instance()->m_level = level;
73 void QLogger::setSilent(bool silent)
75 instance()->m_silent = silent;
78 void QLogger::setModul(const QString &module)
80 instance()->m_modul = module;
83 void QLogger::setFilename(const QString &filename)
85 instance()->m_fileName = filename;
86 if (instance()->m_file) {
87 instance()->m_file->close();
88 delete instance()->m_file;
90 instance()->m_file = new QFile(filename);
91 instance()->m_fileName = filename;
92 instance()->m_file->open(QIODevice::WriteOnly);
95 void QLogger::setEnabled(bool enabled)
97 instance()->m_enabled = enabled;
100 void QLogger::setFlush(int msec)
102 instance()->m_flushTime = msec;
105 void QLogger::flush()
107 instance()->m_lastFlush = QTime::currentTime().elapsed();
108 if (instance()->m_file) {
109 foreach (QString s, instance()->m_buffer) {
110 s += QLatin1String("\n");
111 instance()->m_file->write (s.toAscii());
113 instance()->m_file->flush();
115 foreach ( QString s, instance()->m_buffer) {
116 s += QLatin1String("\n");
118 OutputDebugStringW((TCHAR*)s.utf16());
120 fprintf(stderr, "Debug: %s\n", s.toAscii().constData());
124 instance()->m_buffer.clear();
127 QLogger::QLogger() : m_level(0), m_modul(), m_fileName(), m_file(0), m_enabled(true), m_silent(false), m_flushTime(1000)
129 qInstallMsgHandler(loggerMessageOutput);
130 m_timer = new QTime();
132 m_lastFlush = m_timer->elapsed();
145 QLogger* QLogger::instance()
148 m_instance = new QLogger();
153 void QLogger::output(const char *msg)
156 QString s = QString::fromAscii(msg);
157 if (m_enabled && (m_modul.isEmpty() || s.contains(m_modul, Qt::CaseInsensitive))) {
159 if (s.contains("LEVEL=1")) {
160 s = s.remove("LEVEL=1");
162 } else if (s.contains("LEVEL=2")) {
163 s = s.remove("LEVEL=2");
165 } else if (s.contains("LEVEL=3")) {
166 s = s.remove("LEVEL=3");
169 if (m_level >= level)
172 int time = m_timer->elapsed();
173 if (time > m_lastFlush + m_flushTime)