1 /* -*- mode: c++; c-basic-offset:4 -*-
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
33 #include <config-kleopatra.h>
36 #include "iodevicelogger.h"
38 #include <Libkleo/Exception>
40 #include <KLocalizedString>
48 #include <boost/weak_ptr.hpp>
52 using namespace boost
;
59 explicit Private(Log
*qq
) : q(qq
), m_ioLoggingEnabled(false), m_logFile(0) {}
61 bool m_ioLoggingEnabled
;
62 QString m_outputDirectory
;
66 Log::Private::~Private()
73 void Log::messageHandler(QtMsgType type
, const char *msg
)
76 FILE *const file
= Log::instance()->logFile();
78 fprintf(stderr
, "Log::messageHandler[!file]: %s", msg
);
82 qint64 toWrite
= strlen(msg
);
84 const qint64 written
= fprintf(file
, "%s", msg
);
91 while (fprintf(file
, "\n") == 0);
95 shared_ptr
<const Log
> Log::instance()
97 return mutableInstance();
100 shared_ptr
<Log
> Log::mutableInstance()
102 static weak_ptr
<Log
> self
;
104 return shared_ptr
<Log
>(self
);
105 } catch (const bad_weak_ptr
&) {
106 const shared_ptr
<Log
> s(new Log
);
112 Log::Log() : d(new Private(this))
120 FILE *Log::logFile() const
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
) {
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
) {
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
));
170 logger
->setReadLogDevice(file
);
172 logger
->setWriteLogDevice(file
);