Other: C++ Code reformat.

This commit is contained in:
Xavier Michelon
2023-01-05 08:37:38 +01:00
parent bb07138fb0
commit 8790d3cfcf
58 changed files with 1348 additions and 1692 deletions

View File

@ -20,12 +20,10 @@
#include "../Exception/Exception.h"
namespace bridgepp
{
namespace bridgepp {
namespace
{
namespace {
Log *qtHandlerLog { nullptr }; ///< The log instance handling qt logs.
QMutex qtHandlerMutex; ///< A mutex used to access qtHandlerLog.
@ -45,8 +43,7 @@ QList<QPair<Log::Level, QString>> const logLevelStrings {
//****************************************************************************************************************************************************
/// \param[in] log The log handling qt log entries. Can be null.
//****************************************************************************************************************************************************
void setQtMessageHandlerLog(Log *log)
{
void setQtMessageHandlerLog(Log *log) {
QMutexLocker locker(&qtHandlerMutex);
qtHandlerLog = log;
}
@ -55,8 +52,7 @@ void setQtMessageHandlerLog(Log *log)
//****************************************************************************************************************************************************
/// \return The log handling qt log entries. Can be null.
//****************************************************************************************************************************************************
Log *qtMessageHandlerLog()
{
Log *qtMessageHandlerLog() {
QMutexLocker locker(&qtHandlerMutex);
return qtHandlerLog;
}
@ -66,13 +62,12 @@ Log *qtMessageHandlerLog()
/// \param[in] type The message type.
/// \param[in] message The message.
//****************************************************************************************************************************************************
void qtMessageHandler(QtMsgType type, QMessageLogContext const &, QString const &message)
{
void qtMessageHandler(QtMsgType type, QMessageLogContext const &, QString const &message) {
Log *log = qtMessageHandlerLog();
if (!log)
if (!log) {
return;
switch (type)
{
}
switch (type) {
case QtDebugMsg:
log->debug(message);
break;
@ -102,8 +97,7 @@ void qtMessageHandler(QtMsgType type, QMessageLogContext const &, QString const
/// \param[in] message The log entry message.
/// \return The string for the log entry
//****************************************************************************************************************************************************
QString Log::logEntryToString(Log::Level level, QDateTime const &dateTime, QString const &message)
{
QString Log::logEntryToString(Log::Level level, QDateTime const &dateTime, QString const &message) {
return QString("%1[%2] %3").arg(levelToString(level).left(4).toUpper(), dateTime.toString("MMM dd HH:mm:ss.zzz"), message);
}
@ -112,8 +106,7 @@ QString Log::logEntryToString(Log::Level level, QDateTime const &dateTime, QStri
/// \param[in] level The level.
/// \return A string describing the level.
//****************************************************************************************************************************************************
QString Log::levelToString(Log::Level level)
{
QString Log::levelToString(Log::Level level) {
QList<QPair<Log::Level, QString>>::const_iterator it = std::find_if(logLevelStrings.begin(), logLevelStrings.end(),
[&level](QPair<Log::Level, QString> const &pair) -> bool {
return pair.first == level;
@ -129,16 +122,16 @@ QString Log::levelToString(Log::Level level)
/// \param[out] outLevel The log level parsed. if not found, the value of the variable is not modified.
/// \return true iff parsing was successful.
//****************************************************************************************************************************************************
bool Log::stringToLevel(QString const &str, Log::Level &outLevel)
{
bool Log::stringToLevel(QString const &str, Log::Level &outLevel) {
QList<QPair<Log::Level, QString>>::const_iterator it = std::find_if(logLevelStrings.begin(), logLevelStrings.end(),
[&str](QPair<Log::Level, QString> const &pair) -> bool {
return 0 == QString::compare(str, pair.second, Qt::CaseInsensitive);
});
bool const found = (it != logLevelStrings.end());
if (found)
if (found) {
outLevel = it->first;
}
return found;
}
@ -147,8 +140,7 @@ bool Log::stringToLevel(QString const &str, Log::Level &outLevel)
//****************************************************************************************************************************************************
/// the message handle process the message from the Qt logging system.
//****************************************************************************************************************************************************
void Log::registerAsQtMessageHandler()
{
void Log::registerAsQtMessageHandler() {
setQtMessageHandlerLog(this);
qInstallMessageHandler(qtMessageHandler);
}
@ -160,16 +152,14 @@ void Log::registerAsQtMessageHandler()
Log::Log()
: QObject()
, stdout_(stdout)
, stderr_(stderr)
{
, stderr_(stderr) {
}
//****************************************************************************************************************************************************
/// \param[in] level The log level.
//****************************************************************************************************************************************************
void Log::setLevel(Log::Level level)
{
void Log::setLevel(Log::Level level) {
QMutexLocker locker(&mutex_);
level_ = level;
}
@ -178,8 +168,7 @@ void Log::setLevel(Log::Level level)
//****************************************************************************************************************************************************
/// \return The log level.
//****************************************************************************************************************************************************
Log::Level Log::level() const
{
Log::Level Log::level() const {
QMutexLocker locker(&mutex_);
return level_;
}
@ -188,8 +177,7 @@ Log::Level Log::level() const
//****************************************************************************************************************************************************
/// \param[in] value Should the log entries be sent to STDOUT/STDERR.
//****************************************************************************************************************************************************
void Log::setEchoInConsole(bool value)
{
void Log::setEchoInConsole(bool value) {
QMutexLocker locker(&mutex_);
echoInConsole_ = value;
}
@ -198,8 +186,7 @@ void Log::setEchoInConsole(bool value)
//****************************************************************************************************************************************************
/// \return true iff the log entries be should sent to STDOUT/STDERR.
//****************************************************************************************************************************************************
bool Log::echoInConsole() const
{
bool Log::echoInConsole() const {
QMutexLocker locker(&mutex_);
return echoInConsole_;
}
@ -210,15 +197,16 @@ bool Log::echoInConsole() const
/// \param[out] outError if an error occurs and this pointer in not null, on exit it contains a description of the error.
/// \return true if and only if the operation was successful.
//****************************************************************************************************************************************************
bool Log::startWritingToFile(QString const &path, QString *outError)
{
bool Log::startWritingToFile(QString const &path, QString *outError) {
QMutexLocker locker(&mutex_);
file_ = std::make_unique<QFile>(path);
if (file_->open(QIODevice::WriteOnly | QIODevice::Text))
if (file_->open(QIODevice::WriteOnly | QIODevice::Text)) {
return true;
}
if (outError)
if (outError) {
*outError = QString("Could not open log file '%1' for writing.");
}
file_.reset();
return false;
}
@ -227,8 +215,7 @@ bool Log::startWritingToFile(QString const &path, QString *outError)
//****************************************************************************************************************************************************
///
//****************************************************************************************************************************************************
void Log::stopWritingToFile()
{
void Log::stopWritingToFile() {
QMutexLocker locker(&mutex_);
file_.reset();
}
@ -237,8 +224,7 @@ void Log::stopWritingToFile()
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::panic(QString const &message)
{
void Log::panic(QString const &message) {
return this->addEntry(Level::Panic, message);
}
@ -246,8 +232,7 @@ void Log::panic(QString const &message)
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::fatal(QString const &message)
{
void Log::fatal(QString const &message) {
return this->addEntry(Level::Fatal, message);
}
@ -255,8 +240,7 @@ void Log::fatal(QString const &message)
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::error(QString const &message)
{
void Log::error(QString const &message) {
return this->addEntry(Level::Error, message);
}
@ -264,8 +248,7 @@ void Log::error(QString const &message)
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::warn(QString const &message)
{
void Log::warn(QString const &message) {
return this->addEntry(Level::Warn, message);
}
@ -273,8 +256,7 @@ void Log::warn(QString const &message)
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::info(QString const &message)
{
void Log::info(QString const &message) {
return this->addEntry(Level::Info, message);
}
@ -282,8 +264,7 @@ void Log::info(QString const &message)
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::debug(QString const &message)
{
void Log::debug(QString const &message) {
return this->addEntry(Level::Debug, message);
}
@ -291,8 +272,7 @@ void Log::debug(QString const &message)
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::trace(QString const &message)
{
void Log::trace(QString const &message) {
return this->addEntry(Level::Trace, message);
}
@ -301,28 +281,27 @@ void Log::trace(QString const &message)
/// \param[in] level The level.
/// \param[in] message The message.
//****************************************************************************************************************************************************
void Log::addEntry(Log::Level level, QString const &message)
{
void Log::addEntry(Log::Level level, QString const &message) {
QDateTime const dateTime = QDateTime::currentDateTime();
QMutexLocker locker(&mutex_);
if (qint32(level) > qint32(level_))
if (qint32(level) > qint32(level_)) {
return;
}
emit entryAdded(level, message);
if (!(echoInConsole_ || file_))
if (!(echoInConsole_ || file_)) {
return;
}
QString const entryStr = logEntryToString(level, dateTime, message) + "\n";
if (echoInConsole_)
{
if (echoInConsole_) {
QTextStream &stream = (qint32(level) <= (qint32(Level::Warn))) ? stderr_ : stdout_;
stream << entryStr;
stream.flush();
}
if (file_)
{
if (file_) {
file_->write(entryStr.toLocal8Bit());
file_->flush();
}