OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / logger / logger.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "logger.h"
35
36 #include <qapplication.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include <QTime>
41 #include <QFile>
42
43 #ifdef Q_OS_WIN
44 #include <windows.h>
45 #endif
46
47 QLogger* QLogger::m_instance;
48
49 void QLogger::loggerMessageOutput(QtMsgType type, const char *msg)
50 {
51     switch (type) {
52      case QtDebugMsg:
53          if (!QLogger::instance()->m_silent)
54            QLogger::instance()->output(msg);
55          break;
56      case QtWarningMsg:
57          fprintf(stderr, "Warning: %s\n", msg);
58          break;
59      case QtCriticalMsg:
60          fprintf(stderr, "Critical: %s\n", msg);
61          break;
62      case QtFatalMsg:
63          fprintf(stderr, "Fatal: %s\n", msg);
64          abort();
65     }
66 }
67
68 void QLogger::setLevel(int level)
69 {
70     instance()->m_level = level;
71 }
72
73 void QLogger::setSilent(bool silent)
74 {
75     instance()->m_silent = silent;
76 }
77
78 void QLogger::setModul(const QString &module)
79 {
80     instance()->m_modul = module;
81 }
82
83 void QLogger::setFilename(const QString &filename)
84 {
85     instance()->m_fileName = filename;
86     if (instance()->m_file) {
87         instance()->m_file->close();
88         delete instance()->m_file;
89     }
90     instance()->m_file = new QFile(filename);
91     instance()->m_fileName = filename;
92     instance()->m_file->open(QIODevice::WriteOnly);
93 }
94
95 void QLogger::setEnabled(bool enabled)
96 {
97     instance()->m_enabled = enabled;
98 }
99
100 void QLogger::setFlush(int msec)
101 {
102     instance()->m_flushTime = msec;
103 }
104
105 void QLogger::flush()
106 {
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());
112         }
113         instance()->m_file->flush();
114     } else {
115         foreach ( QString s, instance()->m_buffer) {
116             s += QLatin1String("\n");
117 #ifdef Q_OS_WIN
118             OutputDebugStringW((TCHAR*)s.utf16());
119 #else
120             fprintf(stderr, "Debug: %s\n", s.toAscii().constData());
121 #endif
122         }
123     }
124     instance()->m_buffer.clear();
125 }
126
127 QLogger::QLogger() : m_level(0), m_modul(), m_fileName(), m_file(0), m_enabled(true), m_silent(false), m_flushTime(1000)
128 {
129    qInstallMsgHandler(loggerMessageOutput);
130    m_timer = new QTime();
131    m_timer->start();
132    m_lastFlush = m_timer->elapsed();
133 }
134
135 QLogger::~QLogger()
136 {
137     flush();
138     if (m_file) {
139         m_file->close();
140         delete m_file;
141     }
142     delete m_timer;
143 }
144
145 QLogger* QLogger::instance()
146 {
147     if (!m_instance) {
148         m_instance = new QLogger();
149     }
150     return m_instance;
151 }
152
153 void QLogger::output(const char *msg)
154 {
155
156     QString s = QString::fromAscii(msg);
157     if (m_enabled && (m_modul.isEmpty() || s.contains(m_modul, Qt::CaseInsensitive))) {
158         int level = 0;
159         if (s.contains("LEVEL=1")) {
160             s = s.remove("LEVEL=1");
161             level = 1;
162         } else if (s.contains("LEVEL=2")) {
163             s = s.remove("LEVEL=2");
164             level = 2;
165         } else if (s.contains("LEVEL=3")) {
166             s = s.remove("LEVEL=3");
167             level = 3;
168         }
169         if (m_level >= level)
170             m_buffer.append(s);
171     }
172     int time = m_timer->elapsed();
173     if (time > m_lastFlush + m_flushTime)
174         flush();
175 }