1 /* Copyright (C) 2006 - 2012 Jan Kundrát <jkt@flaska.net>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or the version 3 of the License.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
25 #include <QTextStream>
26 #include "FileLogger.h"
27 #include "../Imap/Model/Utils.h"
32 FileLogger::FileLogger(QObject
*parent
) :
33 QObject(parent
), m_fileLog(0), m_consoleLog(false), m_autoFlush(false)
37 void FileLogger::slotSetFileLogging(const bool enabled
, const QString
&fileName
)
43 QFile
*logFile
= new QFile(fileName
.isEmpty() ? Imap::Mailbox::persistentLogFileName() : fileName
, this);
44 logFile
->open(QIODevice::Truncate
| QIODevice::WriteOnly
);
45 m_fileLog
= new QTextStream(logFile
);
48 QIODevice
*dev
= m_fileLog
->device();
56 FileLogger::~FileLogger()
61 void FileLogger::escapeCrLf(QString
&s
)
63 s
.replace(QChar('\r'), 0x240d /* SYMBOL FOR CARRIAGE RETURN */)
64 .replace(QChar('\n'), 0x240a /* SYMBOL FOR LINE FEED */);
67 void FileLogger::slotImapLogged(uint parser
, Common::LogMessage message
)
69 if (!m_fileLog
&& !m_consoleLog
)
72 QString formatted
= formatMessage(parser
, message
);
73 escapeCrLf(formatted
);
76 *m_fileLog
<< formatted
<< "\n";
83 if (message
.message
.size() > CUTOFF
) {
84 // Got to reformat the message
85 message
.truncatedBytes
= message
.message
.size() - CUTOFF
;
86 message
.message
= message
.message
.left(CUTOFF
);
87 formatted
= formatMessage(parser
, message
);
88 escapeCrLf(formatted
);
90 qDebug() << formatted
.toUtf8().constData() << "\n";
94 QString
FileLogger::formatMessage(uint parser
, const Common::LogMessage
&message
) const
96 using namespace Common
;
98 switch (message
.kind
) {
100 direction
= QLatin1String(" <<< ");
103 direction
= QLatin1String(" >>> ");
105 case LOG_PARSE_ERROR
:
106 direction
= QLatin1String(" [err] ");
108 case LOG_MAILBOX_SYNC
:
109 direction
= QLatin1String(" [sync] ");
112 direction
= QLatin1String(" [task] ");
115 direction
= QLatin1String(" [msg] ");
118 direction
= QLatin1String(" ");
121 if (message
.truncatedBytes
) {
122 direction
+= QLatin1String("[truncated] ");
124 return message
.timestamp
.toString(QLatin1String("hh:mm:ss.zzz")) + QString::number(parser
) + QLatin1Char(' ') +
125 direction
+ message
.source
+ QLatin1Char(' ') + message
.message
.trimmed();
128 /** @short Enable flushing the on-disk log after each message
130 Automatically flushing the log will make sure that all messages are actually stored in the log even in the event of a program
131 crash, but at the cost of a rather high overhead.
133 void FileLogger::setAutoFlush(const bool autoFlush
)
135 m_autoFlush
= autoFlush
;
136 if (m_autoFlush
&& m_fileLog
)
140 void FileLogger::setConsoleLogging(const bool enabled
)
142 m_consoleLog
= enabled
;