|
|
@ -28,6 +28,8 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "filelogger.h" |
|
|
|
#include "filelogger.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <chrono> |
|
|
|
|
|
|
|
|
|
|
|
#include <QDateTime> |
|
|
|
#include <QDateTime> |
|
|
|
#include <QDir> |
|
|
|
#include <QDir> |
|
|
|
#include <QTextStream> |
|
|
|
#include <QTextStream> |
|
|
@ -36,11 +38,16 @@ |
|
|
|
#include "base/logger.h" |
|
|
|
#include "base/logger.h" |
|
|
|
#include "base/utils/fs.h" |
|
|
|
#include "base/utils/fs.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const std::chrono::seconds FLUSH_INTERVAL {2}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
FileLogger::FileLogger(const QString &path, const bool backup, const int maxSize, const bool deleteOld, const int age, const FileLogAgeType ageType) |
|
|
|
FileLogger::FileLogger(const QString &path, const bool backup, const int maxSize, const bool deleteOld, const int age, const FileLogAgeType ageType) |
|
|
|
: m_backup(backup) |
|
|
|
: m_backup(backup) |
|
|
|
, m_maxSize(maxSize) |
|
|
|
, m_maxSize(maxSize) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_flusher.setInterval(0); |
|
|
|
m_flusher.setInterval(FLUSH_INTERVAL); |
|
|
|
m_flusher.setSingleShot(true); |
|
|
|
m_flusher.setSingleShot(true); |
|
|
|
connect(&m_flusher, &QTimer::timeout, this, &FileLogger::flushLog); |
|
|
|
connect(&m_flusher, &QTimer::timeout, this, &FileLogger::flushLog); |
|
|
|
|
|
|
|
|
|
|
@ -114,23 +121,24 @@ void FileLogger::addLogMessage(const Log::Msg &msg) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!m_logFile.isOpen()) return; |
|
|
|
if (!m_logFile.isOpen()) return; |
|
|
|
|
|
|
|
|
|
|
|
QTextStream str(&m_logFile); |
|
|
|
QTextStream stream(&m_logFile); |
|
|
|
|
|
|
|
stream.setCodec("UTF-8"); |
|
|
|
|
|
|
|
|
|
|
|
switch (msg.type) { |
|
|
|
switch (msg.type) { |
|
|
|
case Log::INFO: |
|
|
|
case Log::INFO: |
|
|
|
str << "(I) "; |
|
|
|
stream << "(I) "; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case Log::WARNING: |
|
|
|
case Log::WARNING: |
|
|
|
str << "(W) "; |
|
|
|
stream << "(W) "; |
|
|
|
break; |
|
|
|
break; |
|
|
|
case Log::CRITICAL: |
|
|
|
case Log::CRITICAL: |
|
|
|
str << "(C) "; |
|
|
|
stream << "(C) "; |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
str << "(N) "; |
|
|
|
stream << "(N) "; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
str << QDateTime::fromMSecsSinceEpoch(msg.timestamp).toString(Qt::ISODate) << " - " << msg.message << endl; |
|
|
|
stream << QDateTime::fromMSecsSinceEpoch(msg.timestamp).toString(Qt::ISODate) << " - " << msg.message << '\n'; |
|
|
|
|
|
|
|
|
|
|
|
if (m_backup && (m_logFile.size() >= m_maxSize)) { |
|
|
|
if (m_backup && (m_logFile.size() >= m_maxSize)) { |
|
|
|
closeLogFile(); |
|
|
|
closeLogFile(); |
|
|
@ -146,6 +154,7 @@ void FileLogger::addLogMessage(const Log::Msg &msg) |
|
|
|
openLogFile(); |
|
|
|
openLogFile(); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
|
|
|
|
if (!m_flusher.isActive()) |
|
|
|
m_flusher.start(); |
|
|
|
m_flusher.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|