mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-24 02:46:44 +00:00
Other: move frontend C++ code ini a subfolder.
This commit is contained in:
272
internal/frontend/bridge-gui/bridgepp/bridgepp/Log/Log.cpp
Normal file
272
internal/frontend/bridge-gui/bridgepp/bridgepp/Log/Log.cpp
Normal file
@ -0,0 +1,272 @@
|
||||
// Copyright (c) 2022 Proton AG
|
||||
//
|
||||
// This file is part of Proton Mail Bridge.
|
||||
//
|
||||
// Proton Mail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Proton Mail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
#include "Log.h"
|
||||
|
||||
|
||||
namespace bridgepp
|
||||
{
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
Log *qtHandlerLog { nullptr }; ///< The log instance handling qt logs.
|
||||
QMutex qtHandlerMutex; ///< A mutex used to access qtHandlerLog.
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] log The log handling qt log entries. Can be null.
|
||||
//****************************************************************************************************************************************************
|
||||
void setQtMessageHandlerLog(Log *log)
|
||||
{
|
||||
QMutexLocker locker(&qtHandlerMutex);
|
||||
qtHandlerLog = log;
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \return The log handling qt log entries. Can be null.
|
||||
//****************************************************************************************************************************************************
|
||||
Log *qtMessageHandlerLog()
|
||||
{
|
||||
QMutexLocker locker(&qtHandlerMutex);
|
||||
return qtHandlerLog;
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] type The message type.
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void qtMessageHandler(QtMsgType type, QMessageLogContext const &, QString const &message)
|
||||
{
|
||||
Log *log = qtMessageHandlerLog();
|
||||
if (!log)
|
||||
return;
|
||||
switch (type)
|
||||
{
|
||||
case QtDebugMsg:
|
||||
log->debug(message);
|
||||
break;
|
||||
|
||||
case QtWarningMsg:
|
||||
log->warn(message);
|
||||
break;
|
||||
|
||||
case QtCriticalMsg:
|
||||
case QtFatalMsg:
|
||||
log->error(message);
|
||||
break;
|
||||
|
||||
case QtInfoMsg:
|
||||
default:
|
||||
log->info(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] level The level.
|
||||
/// \return A string describing the level.
|
||||
//****************************************************************************************************************************************************
|
||||
QString logLevelToString(Log::Level level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case Log::Level::Panic:
|
||||
return "PANIC";
|
||||
case Log::Level::Fatal:
|
||||
return "FATAL";
|
||||
case Log::Level::Error:
|
||||
return "ERROR";
|
||||
case Log::Level::Warn:
|
||||
return "WARN";
|
||||
case Log::Level::Info:
|
||||
return "INFO";
|
||||
case Log::Level::Debug:
|
||||
return "DEBUG";
|
||||
case Log::Level::Trace:
|
||||
return "TRACE";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \brief return a string representing the log entry
|
||||
///
|
||||
/// \param[in] level The log entry level.
|
||||
/// \param[in] message The log entry message.
|
||||
/// \return The string for the log entry
|
||||
//****************************************************************************************************************************************************
|
||||
QString Log::logEntryToString(Log::Level level, QString const &message)
|
||||
{
|
||||
return QString("[%1] %2").arg(logLevelToString(level), message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// the message handle process the message from the Qt logging system.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::registerAsQtMessageHandler()
|
||||
{
|
||||
setQtMessageHandlerLog(this);
|
||||
qInstallMessageHandler(qtMessageHandler);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
//
|
||||
//****************************************************************************************************************************************************
|
||||
Log::Log()
|
||||
: QObject()
|
||||
, stdout_(stdout)
|
||||
, stderr_(stderr)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] level The log level.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::setLevel(Log::Level level)
|
||||
{
|
||||
QMutexLocker locker(&mutex_);
|
||||
level_ = level;
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \return The log level.
|
||||
//****************************************************************************************************************************************************
|
||||
Log::Level Log::level() const
|
||||
{
|
||||
QMutexLocker locker(&mutex_);
|
||||
return Log::Level::Debug;
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] value Should the log entries be sent to STDOUT/STDERR.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::setEchoInConsole(bool value)
|
||||
{
|
||||
QMutexLocker locker(&mutex_);
|
||||
echoInConsole_ = value;
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \return true iff the log entries be should sent to STDOUT/STDERR.
|
||||
//****************************************************************************************************************************************************
|
||||
bool Log::echoInConsole() const
|
||||
{
|
||||
QMutexLocker locker(&mutex_);
|
||||
return echoInConsole_;
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::panic(QString const &message)
|
||||
{
|
||||
return this->addEntry(Level::Panic, message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::fatal(QString const &message)
|
||||
{
|
||||
return this->addEntry(Level::Fatal, message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::error(QString const &message)
|
||||
{
|
||||
return this->addEntry(Level::Error, message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::warn(QString const &message)
|
||||
{
|
||||
return this->addEntry(Level::Warn, message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::info(QString const &message)
|
||||
{
|
||||
return this->addEntry(Level::Info, message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::debug(QString const &message)
|
||||
{
|
||||
return this->addEntry(Level::Debug, message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::trace(QString const &message)
|
||||
{
|
||||
return this->addEntry(Level::Trace, message);
|
||||
}
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \param[in] level The level.
|
||||
/// \param[in] message The message.
|
||||
//****************************************************************************************************************************************************
|
||||
void Log::addEntry(Log::Level level, QString const &message)
|
||||
{
|
||||
QMutexLocker locker(&mutex_);
|
||||
if (qint32(level) > qint32(level_))
|
||||
return;
|
||||
emit entryAdded(level, message);
|
||||
|
||||
if (echoInConsole_)
|
||||
{
|
||||
QTextStream &stream = (qint32(level) <= (qint32(Level::Warn))) ? stderr_ : stdout_;
|
||||
stream << logEntryToString(level, message) << "\n";
|
||||
stream.flush();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace bridgepp
|
||||
91
internal/frontend/bridge-gui/bridgepp/bridgepp/Log/Log.h
Normal file
91
internal/frontend/bridge-gui/bridgepp/bridgepp/Log/Log.h
Normal file
@ -0,0 +1,91 @@
|
||||
// Copyright (c) 2022 Proton AG
|
||||
//
|
||||
// This file is part of Proton Mail Bridge.
|
||||
//
|
||||
// Proton Mail Bridge is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Proton Mail Bridge is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
#ifndef BRIDGE_PP_LOG_H
|
||||
#define BRIDGE_PP_LOG_H
|
||||
|
||||
|
||||
namespace bridgepp
|
||||
{
|
||||
|
||||
|
||||
//****************************************************************************************************************************************************
|
||||
/// \brief Basic log class. No logging to file. Four levels. Rebroadcast received log entries via Qt signals.
|
||||
//****************************************************************************************************************************************************
|
||||
class Log : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public: // data types.
|
||||
/// \brief Log level class. The list matches [loggers log levels](https://pkg.go.dev/github.com/sirupsen/logrus).
|
||||
enum class Level
|
||||
{
|
||||
Panic, ///< Panic log level.
|
||||
Fatal, ///< Fatal log level.
|
||||
Error, ///< Error log level.
|
||||
Warn, ///< Warn log level.
|
||||
Info, ///< Info log level.
|
||||
Debug, ///< Debug log level.
|
||||
Trace ///< Trace log level.
|
||||
};
|
||||
|
||||
public: // static member functions.
|
||||
static QString logEntryToString(Log::Level level, QString const &message); ///< Return a string describing a log entry.
|
||||
|
||||
|
||||
public: // member functions.
|
||||
Log(); ///< Default constructor.
|
||||
Log(Log const &) = delete; ///< Disabled copy-constructor.
|
||||
Log(Log &&) = delete; ///< Disabled assignment copy-constructor.
|
||||
~Log() override = default; ///< Destructor.
|
||||
Log &operator=(Log const &) = delete; ///< Disabled assignment operator.
|
||||
Log &operator=(Log &&) = delete; ///< Disabled move assignment operator.
|
||||
|
||||
void setLevel(Level level); ///< Set the log level.
|
||||
Level level() const; ///< Get the log level.
|
||||
void setEchoInConsole(bool value); ///< Set if the log entries should be echoed in STDOUT/STDERR.
|
||||
bool echoInConsole() const; ///< Check if the log entries should be echoed in STDOUT/STDERR.
|
||||
void registerAsQtMessageHandler(); ///< Install the Qt message handler.
|
||||
|
||||
public slots:
|
||||
void panic(QString const &message); ///< Adds an panic entry to the log.
|
||||
void fatal(QString const &message); ///< Adds an fatal entry to the log.
|
||||
void error(QString const &message); ///< Adds an error entry to the log.
|
||||
void warn(QString const &message); ///< Adds a warn entry to the log.
|
||||
void info(QString const &message); ///< Adds an info entry to the log.
|
||||
void debug(QString const &message); ///< Adds a debug entry in the log.
|
||||
void trace(QString const &message); ///< Adds a trace entry in the log.
|
||||
|
||||
void addEntry(Log::Level level, QString const &message); ///< Adds a trace entry in the log.
|
||||
|
||||
signals:
|
||||
void entryAdded(Log::Level entry, QString const &); ///< Signal emitted when a log entry is added.
|
||||
|
||||
private: // data members
|
||||
mutable QMutex mutex_; ///< The mutex.
|
||||
Level level_{Level::Debug}; ///< The log level
|
||||
bool echoInConsole_{false}; ///< Set if the log messages should be sent to STDOUT/STDERR.
|
||||
|
||||
QTextStream stdout_; ///< The stdout stream.
|
||||
QTextStream stderr_; ///< The stderr stream.
|
||||
};
|
||||
|
||||
|
||||
} // namespace bridgepp
|
||||
|
||||
|
||||
#endif //BRIDGE_PP_LOG_H
|
||||
Reference in New Issue
Block a user