GODT-2015: bridge-gui logs to file until gRPC connection is established.

This commit is contained in:
Xavier Michelon
2022-11-02 17:04:25 +01:00
parent 2747e93316
commit 105752fc65
7 changed files with 83 additions and 6 deletions

View File

@ -17,6 +17,7 @@
#include "Log.h"
#include "../Exception/Exception.h"
namespace bridgepp
@ -204,6 +205,35 @@ bool Log::echoInConsole() const
}
//****************************************************************************************************************************************************
/// \param[in] path The path of the file to write to.
/// \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)
{
QMutexLocker locker(&mutex_);
file_ = std::make_unique<QFile>(path);
if (file_->open(QIODevice::WriteOnly | QIODevice::Text))
return true;
if (outError)
*outError = QString("Could not open log file '%1' for writing.");
file_.reset();
return false;
}
//****************************************************************************************************************************************************
///
//****************************************************************************************************************************************************
void Log::stopWritingToFile()
{
QMutexLocker locker(&mutex_);
file_.reset();
}
//****************************************************************************************************************************************************
/// \param[in] message The message.
//****************************************************************************************************************************************************
@ -278,12 +308,22 @@ void Log::addEntry(Log::Level level, QString const &message)
return;
emit entryAdded(level, message);
if (!(echoInConsole_ || file_))
return;
QString const entryStr = logEntryToString(level, message) + "\n";
if (echoInConsole_)
{
QTextStream &stream = (qint32(level) <= (qint32(Level::Warn))) ? stderr_ : stdout_;
stream << logEntryToString(level, message) << "\n";
stream << entryStr;
stream.flush();
}
if (file_)
{
file_->write(entryStr.toLocal8Bit());
file_->flush();
}
}

View File

@ -63,6 +63,8 @@ public: // member functions.
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.
bool startWritingToFile(QString const& path, QString *outError = nullptr); ///< Start writing the log to file. Concerns only future entries.
void stopWritingToFile();
void registerAsQtMessageHandler(); ///< Install the Qt message handler.
public slots:
@ -83,7 +85,7 @@ private: // data members
mutable QMutex mutex_; ///< The mutex.
Level level_ { defaultLevel }; ///< The log level
bool echoInConsole_ { false }; ///< Set if the log messages should be sent to STDOUT/STDERR.
std::unique_ptr<QFile> file_; ///< The file to write the log to.
QTextStream stdout_; ///< The stdout stream.
QTextStream stderr_; ///< The stderr stream.
};