xtuple: optional logging into console and/or a log file
[trojita.git] / src / Common / FileLogger.cpp
blobf1f33cfd29530d32f6ff520fc5b986c92cc3096d
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.
22 #include <QDateTime>
23 #include <QDebug>
24 #include <QFile>
25 #include <QTextStream>
26 #include "FileLogger.h"
27 #include "../Imap/Model/Utils.h"
29 namespace Common
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)
39 if (enabled) {
40 if (m_fileLog)
41 return;
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);
46 } else {
47 if (m_fileLog) {
48 QIODevice *dev = m_fileLog->device();
49 delete m_fileLog;
50 delete dev;
51 m_fileLog = 0;
56 FileLogger::~FileLogger()
58 delete m_fileLog;
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)
70 return;
72 QString formatted = formatMessage(parser, message);
73 escapeCrLf(formatted);
75 if (m_fileLog) {
76 *m_fileLog << formatted << "\n";
77 if (m_autoFlush)
78 m_fileLog->flush();
81 enum {CUTOFF=200};
82 if (m_consoleLog) {
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;
97 QString direction;
98 switch (message.kind) {
99 case LOG_IO_READ:
100 direction = QLatin1String(" <<< ");
101 break;
102 case LOG_IO_WRITTEN:
103 direction = QLatin1String(" >>> ");
104 break;
105 case LOG_PARSE_ERROR:
106 direction = QLatin1String(" [err] ");
107 break;
108 case LOG_MAILBOX_SYNC:
109 direction = QLatin1String(" [sync] ");
110 break;
111 case LOG_TASKS:
112 direction = QLatin1String(" [task] ");
113 break;
114 case LOG_MESSAGES:
115 direction = QLatin1String(" [msg] ");
116 break;
117 case LOG_OTHER:
118 direction = QLatin1String(" ");
119 break;
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)
137 m_fileLog->flush();
140 void FileLogger::setConsoleLogging(const bool enabled)
142 m_consoleLog = enabled;