1/*
2 * Copyright (C) 2007 Alon Bar-Lev <alon.barlev@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17 *
18 */
19
20#include <QFile>
21#include <QTextStream>
22#include <QtCrypto>
23#include <QtPlugin>
24#include <qcaprovider.h>
25
26#include <cstdlib>
27
28using namespace QCA;
29
30namespace loggerQCAPlugin {
31
32class StreamLogger : public QCA::AbstractLogDevice
33{
34 Q_OBJECT
35public:
36 StreamLogger(QTextStream &stream)
37 : QCA::AbstractLogDevice(QStringLiteral("Stream logger"))
38 , _stream(stream)
39 {
40 QCA::logger()->registerLogDevice(logger: this);
41 }
42
43 ~StreamLogger() override
44 {
45 QCA::logger()->unregisterLogDevice(loggerName: name());
46 }
47
48 void logTextMessage(const QString &message, enum QCA::Logger::Severity severity) override
49 {
50 _stream << now() << " " << severityName(severity) << " " << message << Qt::endl;
51 }
52
53 void logBinaryMessage(const QByteArray &blob, enum QCA::Logger::Severity severity) override
54 {
55 Q_UNUSED(blob);
56 _stream << now() << " " << severityName(severity) << " "
57 << "Binary blob not implemented yet" << Qt::endl;
58 }
59
60private:
61 inline const char *severityName(enum QCA::Logger::Severity severity)
62 {
63 if (severity <= QCA::Logger::Debug) {
64 return s_severityNames[severity];
65 } else {
66 return s_severityNames[QCA::Logger::Debug + 1];
67 }
68 }
69
70 inline QString now()
71 {
72 static const QString format = QStringLiteral("yyyy-MM-dd hh:mm:ss");
73 return QDateTime::currentDateTime().toString(format);
74 }
75
76private:
77 static const char *s_severityNames[];
78 QTextStream &_stream;
79};
80
81const char *StreamLogger::s_severityNames[] = {"Q", "M", "A", "C", "E", "W", "N", "I", "D", "U"};
82
83}
84
85using namespace loggerQCAPlugin;
86
87class loggerProvider : public Provider
88{
89private:
90 QFile _logFile;
91 QTextStream _logStream;
92 StreamLogger *_streamLogger;
93 bool _externalConfig;
94
95public:
96 loggerProvider()
97 {
98 _externalConfig = false;
99 _streamLogger = nullptr;
100
101 const QByteArray level = qgetenv(varName: "QCALOGGER_LEVEL");
102 const QByteArray file = qgetenv(varName: "QCALOGGER_FILE");
103
104 if (!level.isEmpty()) {
105 printf(format: "XXXX %s %s\n", level.data(), file.data());
106 _externalConfig = true;
107 createLogger(level: atoi(nptr: level.constData()), file: file.isEmpty() ? QString() : QString::fromUtf8(ba: file));
108 }
109 }
110
111 ~loggerProvider() override
112 {
113 delete _streamLogger;
114 _streamLogger = nullptr;
115 }
116
117public:
118 int qcaVersion() const override
119 {
120 return QCA_VERSION;
121 }
122
123 void init() override
124 {
125 }
126
127 QString name() const override
128 {
129 return QStringLiteral("qca-logger");
130 }
131
132 QStringList features() const override
133 {
134 QStringList list;
135 list += QStringLiteral("log");
136 return list;
137 }
138
139 Context *createContext(const QString &type) override
140 {
141 Q_UNUSED(type);
142 return nullptr;
143 }
144
145 QVariantMap defaultConfig() const override
146 {
147 QVariantMap mytemplate;
148
149 mytemplate[QStringLiteral("formtype")] = QStringLiteral("http://affinix.com/qca/forms/qca-logger#1.0");
150 mytemplate[QStringLiteral("enabled")] = false;
151 mytemplate[QStringLiteral("file")] = QLatin1String("");
152 mytemplate[QStringLiteral("level")] = (int)Logger::Quiet;
153
154 return mytemplate;
155 }
156
157 void configChanged(const QVariantMap &config) override
158 {
159 if (!_externalConfig) {
160 delete _streamLogger;
161 _streamLogger = nullptr;
162
163 if (config[QStringLiteral("enabled")].toBool()) {
164 createLogger(level: config[QStringLiteral("level")].toInt(), file: config[QStringLiteral("file")].toString());
165 }
166 }
167 }
168
169private:
170 void createLogger(const int level, const QString &file)
171 {
172 bool success = false;
173 if (file.isEmpty()) {
174 success = _logFile.open(stderr, ioFlags: QIODevice::WriteOnly | QIODevice::Text | QIODevice::Unbuffered);
175 } else {
176 _logFile.setFileName(file);
177 success = _logFile.open(flags: QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered);
178 }
179
180 if (success) {
181 _logStream.setDevice(&_logFile);
182 logger()->setLevel((Logger::Severity)level);
183 _streamLogger = new StreamLogger(_logStream);
184 }
185 }
186};
187
188class loggerPlugin : public QObject, public QCAPlugin
189{
190 Q_OBJECT
191 Q_PLUGIN_METADATA(IID "com.affinix.qca.Plugin/1.0")
192 Q_INTERFACES(QCAPlugin)
193
194public:
195 Provider *createProvider() override
196 {
197 return new loggerProvider;
198 }
199};
200
201#include "qca-logger.moc"
202

source code of qca/plugins/qca-logger/qca-logger.cpp