SVN_SILENT made messages (.desktop file) - always resolve ours
[kdepim.git] / kleopatra / utils / log.cpp
blob963b7863e1d60e3a500cd45899511a8f15ed04f6
1 /* -*- mode: c++; c-basic-offset:4 -*-
2 utils/log.cpp
4 This file is part of Kleopatra, the KDE keymanager
5 Copyright (c) 2008 Klarälvdalens Datakonsult AB
7 Kleopatra is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Kleopatra is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
33 #include <config-kleopatra.h>
35 #include "log.h"
36 #include "iodevicelogger.h"
38 #include <Libkleo/Exception>
40 #include <KLocalizedString>
41 #include <KRandom>
43 #include <QDateTime>
44 #include <QDir>
45 #include <QFile>
46 #include <QString>
48 #include <boost/weak_ptr.hpp>
49 #include <cassert>
50 #include <cstdio>
52 using namespace boost;
53 using namespace Kleo;
55 class Log::Private
57 Log *const q;
58 public:
59 explicit Private(Log *qq) : q(qq), m_ioLoggingEnabled(false), m_logFile(0) {}
60 ~Private();
61 bool m_ioLoggingEnabled;
62 QString m_outputDirectory;
63 FILE *m_logFile;
66 Log::Private::~Private()
68 if (m_logFile) {
69 fclose(m_logFile);
73 void Log::messageHandler(QtMsgType type, const char *msg)
75 Q_UNUSED(type)
76 FILE *const file = Log::instance()->logFile();
77 if (!file) {
78 fprintf(stderr, "Log::messageHandler[!file]: %s", msg);
79 return;
82 qint64 toWrite = strlen(msg);
83 while (toWrite > 0) {
84 const qint64 written = fprintf(file, "%s", msg);
85 if (written == -1) {
86 return;
88 toWrite -= written;
90 //append newline:
91 while (fprintf(file, "\n") == 0);
92 fflush(file);
95 shared_ptr<const Log> Log::instance()
97 return mutableInstance();
100 shared_ptr<Log> Log::mutableInstance()
102 static weak_ptr<Log> self;
103 try {
104 return shared_ptr<Log>(self);
105 } catch (const bad_weak_ptr &) {
106 const shared_ptr<Log> s(new Log);
107 self = s;
108 return s;
112 Log::Log() : d(new Private(this))
116 Log::~Log()
120 FILE *Log::logFile() const
122 return d->m_logFile;
125 void Log::setIOLoggingEnabled(bool enabled)
127 d->m_ioLoggingEnabled = enabled;
130 bool Log::ioLoggingEnabled() const
132 return d->m_ioLoggingEnabled;
135 QString Log::outputDirectory() const
137 return d->m_outputDirectory;
140 void Log::setOutputDirectory(const QString &path)
142 if (d->m_outputDirectory == path) {
143 return;
145 d->m_outputDirectory = path;
146 assert(!d->m_logFile);
147 const QString lfn = path + QLatin1String("/kleo-log");
148 d->m_logFile = fopen(QDir::toNativeSeparators(lfn).toLocal8Bit().constData(), "a");
149 assert(d->m_logFile);
152 shared_ptr<QIODevice> Log::createIOLogger(const shared_ptr<QIODevice> &io, const QString &prefix, OpenMode mode) const
154 if (!d->m_ioLoggingEnabled) {
155 return io;
158 shared_ptr<IODeviceLogger> logger(new IODeviceLogger(io));
160 const QString timestamp = QDateTime::currentDateTime().toString(QStringLiteral("yyMMdd-hhmmss"));
162 const QString fn = d->m_outputDirectory + QLatin1Char('/') + prefix + QLatin1Char('-') + timestamp + QLatin1Char('-') + KRandom::randomString(4);
163 shared_ptr<QFile> file(new QFile(fn));
165 if (!file->open(QIODevice::WriteOnly)) {
166 throw Exception(gpg_error(GPG_ERR_EIO), i18n("Log Error: Could not open log file \"%1\" for writing.", fn));
169 if (mode & Read) {
170 logger->setReadLogDevice(file);
171 } else { // Write
172 logger->setWriteLogDevice(file);
175 return logger;