File indexing completed on 2024-06-23 05:14:16

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     iodevicelogger.cpp
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include <config-kleopatra.h>
0011 
0012 #include "iodevicelogger.h"
0013 
0014 using namespace Kleo;
0015 
0016 class IODeviceLogger::Private
0017 {
0018     IODeviceLogger *const q;
0019 
0020 public:
0021     static bool write(const std::shared_ptr<QIODevice> &dev, const char *data, qint64 max);
0022 
0023     explicit Private(const std::shared_ptr<QIODevice> &io_, IODeviceLogger *qq)
0024         : q(qq)
0025         , io(io_)
0026         , writeLog()
0027         , readLog()
0028     {
0029         Q_ASSERT(io);
0030         connect(io.get(), &QIODevice::aboutToClose, q, &QIODevice::aboutToClose);
0031         connect(io.get(), &QIODevice::bytesWritten, q, &QIODevice::bytesWritten);
0032         connect(io.get(), &QIODevice::readyRead, q, &QIODevice::readyRead);
0033         q->setOpenMode(io->openMode());
0034     }
0035 
0036     ~Private()
0037     {
0038     }
0039 
0040     const std::shared_ptr<QIODevice> io;
0041     std::shared_ptr<QIODevice> writeLog;
0042     std::shared_ptr<QIODevice> readLog;
0043 };
0044 
0045 bool IODeviceLogger::Private::write(const std::shared_ptr<QIODevice> &dev, const char *data, qint64 max)
0046 {
0047     Q_ASSERT(dev);
0048     Q_ASSERT(data);
0049     Q_ASSERT(max >= 0);
0050     qint64 toWrite = max;
0051     while (toWrite > 0) {
0052         const qint64 written = dev->write(data, toWrite);
0053         if (written < 0) {
0054             return false;
0055         }
0056         toWrite -= written;
0057     }
0058     return true;
0059 }
0060 
0061 IODeviceLogger::IODeviceLogger(const std::shared_ptr<QIODevice> &iod, QObject *parent)
0062     : QIODevice(parent)
0063     , d(new Private(iod, this))
0064 {
0065 }
0066 
0067 IODeviceLogger::~IODeviceLogger()
0068 {
0069 }
0070 
0071 void IODeviceLogger::setWriteLogDevice(const std::shared_ptr<QIODevice> &dev)
0072 {
0073     d->writeLog = dev;
0074 }
0075 
0076 void IODeviceLogger::setReadLogDevice(const std::shared_ptr<QIODevice> &dev)
0077 {
0078     d->readLog = dev;
0079 }
0080 
0081 bool IODeviceLogger::atEnd() const
0082 {
0083     return d->io->atEnd();
0084 }
0085 
0086 qint64 IODeviceLogger::bytesAvailable() const
0087 {
0088     return d->io->bytesAvailable();
0089 }
0090 
0091 qint64 IODeviceLogger::bytesToWrite() const
0092 {
0093     return d->io->bytesToWrite();
0094 }
0095 
0096 bool IODeviceLogger::canReadLine() const
0097 {
0098     return d->io->canReadLine();
0099 }
0100 
0101 void IODeviceLogger::close()
0102 {
0103     d->io->close();
0104 }
0105 
0106 bool IODeviceLogger::isSequential() const
0107 {
0108     return d->io->isSequential();
0109 }
0110 
0111 bool IODeviceLogger::open(OpenMode mode)
0112 {
0113     QIODevice::open(mode);
0114     return d->io->open(mode);
0115 }
0116 
0117 qint64 IODeviceLogger::pos() const
0118 {
0119     return d->io->pos();
0120 }
0121 
0122 bool IODeviceLogger::reset()
0123 {
0124     return d->io->reset();
0125 }
0126 
0127 bool IODeviceLogger::seek(qint64 pos)
0128 {
0129     return d->io->seek(pos);
0130 }
0131 
0132 qint64 IODeviceLogger::size() const
0133 {
0134     return d->io->size();
0135 }
0136 
0137 bool IODeviceLogger::waitForBytesWritten(int msecs)
0138 {
0139     return d->io->waitForBytesWritten(msecs);
0140 }
0141 
0142 bool IODeviceLogger::waitForReadyRead(int msecs)
0143 {
0144     return d->io->waitForReadyRead(msecs);
0145 }
0146 
0147 qint64 IODeviceLogger::readData(char *data, qint64 maxSize)
0148 {
0149     const qint64 num = d->io->read(data, maxSize);
0150     if (num > 0 && d->readLog) {
0151         Private::write(d->readLog, data, num);
0152     }
0153     return num;
0154 }
0155 
0156 qint64 IODeviceLogger::writeData(const char *data, qint64 maxSize)
0157 {
0158     const qint64 num = d->io->write(data, maxSize);
0159     if (num > 0 && d->writeLog) {
0160         Private::write(d->writeLog, data, num);
0161     }
0162     return num;
0163 }
0164 
0165 qint64 IODeviceLogger::readLineData(char *data, qint64 maxSize)
0166 {
0167     const qint64 num = d->io->readLine(data, maxSize);
0168     if (num > 0 && d->readLog) {
0169         Private::write(d->readLog, data, num);
0170     }
0171     return num;
0172 }
0173 
0174 #include "moc_iodevicelogger.cpp"