summaryrefslogtreecommitdiff
path: root/src/plugins/qmldesigner/components/logger/logger.cpp
blob: 01fc8391eebbdf0bbd9dd3d54aa70d4bcffddee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "logger.h"

#include <qapplication.h>
#include <stdio.h>
#include <stdlib.h>

#include <QTime>
#include <QFile>

#ifdef Q_OS_WIN
#include <windows.h>
#endif

QLogger* QLogger::m_instance;

void QLogger::loggerMessageOutput(QtMsgType type, const char *msg)
{
    switch (type) {
     case QtDebugMsg:
         if (!QLogger::instance()->m_silent)
           QLogger::instance()->output(msg);
         break;
     case QtWarningMsg:
         fprintf(stderr, "Warning: %s\n", msg);
         break;
     case QtCriticalMsg:
         fprintf(stderr, "Critical: %s\n", msg);
         break;
     case QtFatalMsg:
         fprintf(stderr, "Fatal: %s\n", msg);
         abort();
    }
}

void QLogger::setLevel(int level)
{
    instance()->m_level = level;
}

void QLogger::setSilent(bool silent)
{
    instance()->m_silent = silent;
}

void QLogger::setModul(const QString &module)
{
    instance()->m_modul = module;
}

void QLogger::setFilename(const QString &filename)
{
    instance()->m_fileName = filename;
    if (instance()->m_file) {
        instance()->m_file->close();
        delete instance()->m_file;
    }
    instance()->m_file = new QFile(filename);
    instance()->m_fileName = filename;
    instance()->m_file->open(QIODevice::WriteOnly);
}

void QLogger::setEnabled(bool enabled)
{
    instance()->m_enabled = enabled;
}

void QLogger::setFlush(int msec)
{
    instance()->m_flushTime = msec;
}

void QLogger::flush()
{
    instance()->m_lastFlush = QTime::currentTime().elapsed();
    if (instance()->m_file) {
        foreach (QString s, instance()->m_buffer) {
            s += QLatin1String("\n");
            instance()->m_file->write (s.toAscii());
        }
        instance()->m_file->flush();
    } else {
        foreach ( QString s, instance()->m_buffer) {
            s += QLatin1String("\n");
#ifdef Q_OS_WIN
            OutputDebugStringW((TCHAR*)s.utf16());
#else
            fprintf(stderr, "Debug: %s\n", s.toAscii().constData());
#endif
        }
    }
    instance()->m_buffer.clear();
}

QLogger::QLogger() : m_level(0), m_modul(), m_fileName(), m_file(0), m_enabled(true), m_silent(false), m_flushTime(1000)
{
   qInstallMsgHandler(loggerMessageOutput);
   m_timer = new QTime();
   m_timer->start();
   m_lastFlush = m_timer->elapsed();
}

QLogger::~QLogger()
{
    flush();
    if (m_file) {
        m_file->close();
        delete m_file;
    }
    delete m_timer;
}

QLogger* QLogger::instance()
{
    if (!m_instance) {
        m_instance = new QLogger();
    }
    return m_instance;
}

void QLogger::output(const char *msg)
{

    QString s = QString::fromAscii(msg);
    if (m_enabled && (m_modul.isEmpty() || s.contains(m_modul, Qt::CaseInsensitive))) {
        int level = 0;
        if (s.contains("LEVEL=1")) {
            s = s.remove("LEVEL=1");
            level = 1;
        } else if (s.contains("LEVEL=2")) {
            s = s.remove("LEVEL=2");
            level = 2;
        } else if (s.contains("LEVEL=3")) {
            s = s.remove("LEVEL=3");
            level = 3;
        }
        if (m_level >= level)
            m_buffer.append(s);
    }
    int time = m_timer->elapsed();
    if (time > m_lastFlush + m_flushTime)
        flush();
}