File: qca-logger.cpp

package info (click to toggle)
qca2 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,884 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (201 lines) | stat: -rw-r--r-- 5,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (C) 2007  Alon Bar-Lev <alon.barlev@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 */

#include <QFile>
#include <QTextStream>
#include <QtCrypto>
#include <QtPlugin>
#include <qcaprovider.h>

#include <cstdlib>

using namespace QCA;

namespace loggerQCAPlugin {

class StreamLogger : public QCA::AbstractLogDevice
{
    Q_OBJECT
public:
    StreamLogger(QTextStream &stream)
        : QCA::AbstractLogDevice(QStringLiteral("Stream logger"))
        , _stream(stream)
    {
        QCA::logger()->registerLogDevice(this);
    }

    ~StreamLogger() override
    {
        QCA::logger()->unregisterLogDevice(name());
    }

    void logTextMessage(const QString &message, enum QCA::Logger::Severity severity) override
    {
        _stream << now() << " " << severityName(severity) << " " << message << Qt::endl;
    }

    void logBinaryMessage(const QByteArray &blob, enum QCA::Logger::Severity severity) override
    {
        Q_UNUSED(blob);
        _stream << now() << " " << severityName(severity) << " "
                << "Binary blob not implemented yet" << Qt::endl;
    }

private:
    inline const char *severityName(enum QCA::Logger::Severity severity)
    {
        if (severity <= QCA::Logger::Debug) {
            return s_severityNames[severity];
        } else {
            return s_severityNames[QCA::Logger::Debug + 1];
        }
    }

    inline QString now()
    {
        static const QString format = QStringLiteral("yyyy-MM-dd hh:mm:ss");
        return QDateTime::currentDateTime().toString(format);
    }

private:
    static const char *s_severityNames[];
    QTextStream       &_stream;
};

const char *StreamLogger::s_severityNames[] = {"Q", "M", "A", "C", "E", "W", "N", "I", "D", "U"};

}

using namespace loggerQCAPlugin;

class loggerProvider : public Provider
{
private:
    QFile         _logFile;
    QTextStream   _logStream;
    StreamLogger *_streamLogger;
    bool          _externalConfig;

public:
    loggerProvider()
    {
        _externalConfig = false;
        _streamLogger   = nullptr;

        const QByteArray level = qgetenv("QCALOGGER_LEVEL");
        const QByteArray file  = qgetenv("QCALOGGER_FILE");

        if (!level.isEmpty()) {
            printf("XXXX %s %s\n", level.data(), file.data());
            _externalConfig = true;
            createLogger(atoi(level.constData()), file.isEmpty() ? QString() : QString::fromUtf8(file));
        }
    }

    ~loggerProvider() override
    {
        delete _streamLogger;
        _streamLogger = nullptr;
    }

public:
    int qcaVersion() const override
    {
        return QCA_VERSION;
    }

    void init() override
    {
    }

    QString name() const override
    {
        return QStringLiteral("qca-logger");
    }

    QStringList features() const override
    {
        QStringList list;
        list += QStringLiteral("log");
        return list;
    }

    Context *createContext(const QString &type) override
    {
        Q_UNUSED(type);
        return nullptr;
    }

    QVariantMap defaultConfig() const override
    {
        QVariantMap mytemplate;

        mytemplate[QStringLiteral("formtype")] = QStringLiteral("http://affinix.com/qca/forms/qca-logger#1.0");
        mytemplate[QStringLiteral("enabled")]  = false;
        mytemplate[QStringLiteral("file")]     = QLatin1String("");
        mytemplate[QStringLiteral("level")]    = (int)Logger::Quiet;

        return mytemplate;
    }

    void configChanged(const QVariantMap &config) override
    {
        if (!_externalConfig) {
            delete _streamLogger;
            _streamLogger = nullptr;

            if (config[QStringLiteral("enabled")].toBool()) {
                createLogger(config[QStringLiteral("level")].toInt(), config[QStringLiteral("file")].toString());
            }
        }
    }

private:
    void createLogger(const int level, const QString &file)
    {
        bool success = false;
        if (file.isEmpty()) {
            success = _logFile.open(stderr, QIODevice::WriteOnly | QIODevice::Text | QIODevice::Unbuffered);
        } else {
            _logFile.setFileName(file);
            success = _logFile.open(QIODevice::Append | QIODevice::Text | QIODevice::Unbuffered);
        }

        if (success) {
            _logStream.setDevice(&_logFile);
            logger()->setLevel((Logger::Severity)level);
            _streamLogger = new StreamLogger(_logStream);
        }
    }
};

class loggerPlugin : public QObject, public QCAPlugin
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "com.affinix.qca.Plugin/1.0")
    Q_INTERFACES(QCAPlugin)

public:
    Provider *createProvider() override
    {
        return new loggerProvider;
    }
};

#include "qca-logger.moc"