X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Flogger.cpp;h=870af5f0347d3c3e5528b01fd056b45131051f62;hb=13dedf19c628b9967e4c4106ab0fd967f1df0b90;hp=358b3146b3bb6e789670adc1203d0524956de764;hpb=6fb7963d523b32158fc3d4b88a933825f8535a0b;p=evaf
diff --git a/src/libs/Common/logger.cpp b/src/libs/Common/logger.cpp
index 358b314..870af5f 100644
--- a/src/libs/Common/logger.cpp
+++ b/src/libs/Common/logger.cpp
@@ -40,7 +40,7 @@
//-------------------------------------------------------------------
-void eVaf::Common::Internal::defFatalMsgHandler(QString const & msg, QString const & source, QString const & where)
+[[noreturn]] void eVaf::Common::Internal::defFatalMsgHandler(QString const & msg, QString const & source, QString const & where)
{
Q_UNUSED(source);
@@ -58,10 +58,18 @@ void eVaf::Common::Internal::defFatalMsgHandler(QString const & msg, QString con
using namespace eVaf::Common;
+namespace
+{
+ static Internal::Logger * singleton = nullptr;
+}
+
iLogger * iLogger::instance()
{
- static Internal::Logger singleton;
- return &singleton;
+ if (nullptr == singleton)
+ {
+ singleton = new Internal::Logger;
+ }
+ return singleton;
}
@@ -101,28 +109,31 @@ void LoggerSource::init(QString const & source)
IniFile ini(confFileName, QIODevice::ReadOnly);
// Default values for all sources
- maxSize = 1024 * ini.getValue(".default/log_size", maxSize / 1024).toInt();
- maxCount = ini.getValue(".default/log_count", maxCount).toInt();
+ maxSize = 1024 * ini.getValue(".default/log_size", maxSize / 1024).toUInt();
+ maxCount = ini.getValue(".default/log_count", maxCount).toUInt();
// Default values for this source
- maxSize = 1024 * ini.getValue(source.toLatin1() + "/log_size", maxSize / 1024).toInt();
- maxCount = ini.getValue(source.toLatin1() + "/log_count", maxCount).toInt();
+ maxSize = 1024 * ini.getValue(source.toLatin1() + "/log_size", maxSize / 1024).toUInt();
+ maxCount = ini.getValue(source.toLatin1() + "/log_count", maxCount).toUInt();
}
}
//-------------------------------------------------------------------
-/// Recursively renames backup files
-void renameBackupFile(QDir & dir, QString const & baseName, int idx)
+namespace
{
- QString f1 = QString("%1.%2").arg(baseName).arg(idx);
- QString f2 = QString("%1.%2").arg(baseName).arg(idx + 1);
+ /// Recursively renames backup files
+ void renameBackupFile(QDir & dir, QString const & baseName, int idx)
+ {
+ QString f1 = QString("%1.%2").arg(baseName).arg(idx);
+ QString f2 = QString("%1.%2").arg(baseName).arg(idx + 1);
- if (dir.exists(f2))
- renameBackupFile(dir, baseName, idx + 1);
+ if (dir.exists(f2))
+ renameBackupFile(dir, baseName, idx + 1);
- dir.rename(f1, f2);
+ dir.rename(f1, f2);
+ }
}
void LoggerWorker::writeToLogFile(LoggerSource const & src, QString const & msg)
@@ -168,13 +179,20 @@ void LoggerWorker::writeToLogFile(LoggerSource const & src, QString const & msg)
//-------------------------------------------------------------------
+void Logger::destroyInstance()
+{
+ if (singleton != nullptr)
+ {
+ delete singleton;
+ singleton = nullptr;
+ }
+}
+
Logger::Logger()
: iLogger()
, mReady(false)
, mFatalMsgHandler(defFatalMsgHandler)
, mConsoleSeverity(iLogger::Fatal)
- , mThread(0)
- , mWorker(0)
{
setObjectName(QString("%1-iLogger").arg(VER_MODULE_NAME_STR));
@@ -184,25 +202,25 @@ Logger::Logger()
mDefaultSource = new LoggerSource;
mDefaultSource->name = "common";
- write(Info, QString("%1 created").arg(objectName()), 0, printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__));
+ write(Info, QString("%1 created").arg(objectName()), QString(), printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__));
}
Logger::~Logger()
{
// Disconnect any potential receivers from this object
- disconnect(this, SIGNAL(loggerEvent(Common::iLogger::Severity,QString,QString,QString)), 0, 0);
+ disconnect(this, SIGNAL(loggerEvent(Common::iLogger::Severity,QString,QString,QString)), nullptr, nullptr);
// Destroy the worker thread
if (mWorker) {
- delete mWorker;
+ mWorker.reset();
if (mThread) {
mThread->quit();
mThread->wait();
- delete mThread;
+ mThread.reset();
}
}
- write(Info, QString("%1 destroyed").arg(objectName()), 0, printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__));
+ write(Info, QString("%1 destroyed").arg(objectName()), QString(), printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__));
}
bool Logger::init()
@@ -228,25 +246,21 @@ bool Logger::init()
setMaxCount(v.toUInt());
// Destroy the previous worker thread
- if (mWorker) {
- delete mWorker;
- if (mThread) {
- mThread->quit();
- mThread->wait();
- delete mThread;
- }
+ if (mThread) {
+ mThread->quit();
+ mThread->wait();
}
// Create the worker thread
- mWorker = new LoggerWorker;
- mThread = new QThread;
- mWorker->moveToThread(mThread);
+ mWorker.reset(new LoggerWorker);
+ mThread.reset(new QThread);
+ mWorker->moveToThread(mThread.data());
mThread->start(QThread::IdlePriority);
- connect(this, SIGNAL(writeToLogFile(LoggerSource,QString)), mWorker, SLOT(writeToLogFile(LoggerSource,QString)), Qt::QueuedConnection);
+ connect(this, SIGNAL(writeToLogFile(LoggerSource,QString)), mWorker.data(), SLOT(writeToLogFile(LoggerSource,QString)), Qt::QueuedConnection);
mReady = true;
- write(Info, QString("%1 initialized").arg(objectName()), 0, printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__));
+ write(Info, QString("%1 initialized").arg(objectName()), QString(), printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__));
return true;
}
@@ -346,25 +360,25 @@ void Logger::write(Severity severity, QString const & msg, QString const & sourc
FILE * f = (severity < iLogger::Info) ? stderr : stdout;
// Set text colors
-#ifdef Q_OS_LINUX
+#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
switch (severity) {
case iLogger::Info:
- fprintf(f, "\e[32m"); // Green
+ fprintf(f, "\033[32m"); // Green
break;
case iLogger::Warning:
- fprintf(f, "\e[1m"); // Bold
+ fprintf(f, "\033[1m"); // Bold
break;
case iLogger::Error:
- fprintf(f, "\e[31m"); // Red
+ fprintf(f, "\033[31m"); // Red
break;
case iLogger::Fatal:
- fprintf(f, "\e[31m\e[1m"); // Bold Red
+ fprintf(f, "\033[31m\033[1m"); // Bold Red
break;
default:
- fprintf(f, "\e[34m"); // Blue
+ fprintf(f, "\033[34m"); // Blue
break;
}
-#elif defined Q_OS_WIN32
+#elif defined(Q_OS_WIN32)
switch (severity) {
case iLogger::Info:
setColor(FOREGROUND_GREEN);
@@ -392,9 +406,9 @@ void Logger::write(Severity severity, QString const & msg, QString const & sourc
fprintf(f, "\t(occurred in %s)\n\n", qPrintable(where));
// Reset text colors
-#ifdef Q_OS_LINUX
- fputs("\e[0m", f);
-#elif defined Q_OS_WIN32
+#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
+ fputs("\033[0m", f);
+#elif defined(Q_OS_WIN32)
setColor(7);
#endif
@@ -415,7 +429,7 @@ QString Logger::printf(char const * const fmt, ...) const
#ifdef Q_OS_WIN32
char str[4096];
#else
- char * str = 0;
+ char * str = nullptr;
#endif
va_list ap;