File indexing completed on 2024-12-01 10:39:47

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ruqolalogger.h"
0008 #include "ruqola_debug.h"
0009 #include <QCoreApplication>
0010 #include <QDateTime>
0011 #include <QFileInfo>
0012 
0013 RuqolaLogger::RuqolaLogger(const QString &accountName)
0014     : RocketChatRestApi::AbstractLogger()
0015 {
0016     static quint64 nextIdentifier = 0;
0017     mIdentifier = ++nextIdentifier;
0018 
0019     const QString filename = QLatin1String(qgetenv("RUQOLA_LOGFILE")) + QLatin1Char('-') + accountName + QLatin1Char('.')
0020         + QString::number(QCoreApplication::applicationPid()) + QLatin1Char('.') + QString::number(mIdentifier);
0021 
0022     mLoggerFile.setFileName(filename);
0023     if (!mLoggerFile.open(QFile::WriteOnly)) {
0024         qCWarning(RUQOLA_LOG) << "Could not open log file for writing:" << mLoggerFile.fileName();
0025     }
0026 
0027     mRestApiLogFile.setFileName(QStringLiteral("RESTAPI_") + filename);
0028     if (!mRestApiLogFile.open(QFile::WriteOnly)) {
0029         qCWarning(RUQOLA_LOG) << "Could not open log file for restapi:" << mRestApiLogFile.fileName();
0030     }
0031 
0032     // No convert to qCDebug as we want to see this info.
0033     qDebug() << "Log in file: " << QFileInfo(mLoggerFile.fileName()).absoluteFilePath();
0034     qDebug() << "Log send RESTAPI in file: " << QFileInfo(mRestApiLogFile.fileName()).absoluteFilePath();
0035 }
0036 
0037 RuqolaLogger::~RuqolaLogger()
0038 {
0039     mLoggerFile.close();
0040     mRestApiLogFile.close();
0041 }
0042 
0043 void RuqolaLogger::dataSent(DataType type, const QByteArray &label, const QByteArray &data)
0044 {
0045     switch (type) {
0046     case Unknown:
0047     case DDPType:
0048         break;
0049     case RESTApiType:
0050         mRestApiLogFile.write('[' + QDateTime::currentDateTime().toString().toUtf8() + "] " + data + '\n');
0051         mRestApiLogFile.flush();
0052         break;
0053     }
0054     dataSent(label + ' ' + data);
0055 }
0056 
0057 void RuqolaLogger::dataSent(const QByteArray &data)
0058 {
0059     mLoggerFile.write("Sent: " + data.trimmed() + '\n');
0060     mLoggerFile.flush();
0061 }
0062 
0063 void RuqolaLogger::dataReceived(const QByteArray &data)
0064 {
0065     mLoggerFile.write("Received: " + data.trimmed() + '\n');
0066     mLoggerFile.flush();
0067 }