12 #include <QTimerEvent>
17 using namespace stefanfrings;
19 void FileLogger::refreshSettings()
23 QString oldFileName=fileName;
27 fileName=settings->value(
"fileName").toString();
30 if (QDir::isRelativePath(fileName) && settings->format()!=QSettings::NativeFormat)
32 if (QDir::isRelativePath(fileName))
35 QFileInfo configFile(settings->fileName());
36 fileName=QFileInfo(configFile.absolutePath(),fileName).absoluteFilePath();
38 maxSize=settings->value(
"maxSize",0).toLongLong();
39 maxBackups=settings->value(
"maxBackups",0).toInt();
40 msgFormat=settings->value(
"msgFormat",
"{timestamp} {type} {msg}").toString();
41 timestampFormat=settings->value(
"timestampFormat",
"yyyy-MM-dd hh:mm:ss.zzz").toString();
42 bufferSize=settings->value(
"bufferSize",0).toInt();
45 QByteArray minLevelStr = settings->value(
"minLevel",
"ALL").toByteArray();
46 if (minLevelStr==
"ALL" || minLevelStr==
"DEBUG" || minLevelStr==
"0")
50 else if (minLevelStr==
"WARNING" || minLevelStr==
"WARN" || minLevelStr==
"1")
54 else if (minLevelStr==
"ERROR" || minLevelStr==
"CRITICAL" || minLevelStr==
"2")
58 else if (minLevelStr==
"FATAL" || minLevelStr==
"3")
62 else if (minLevelStr==
"INFO" || minLevelStr==
"4")
68 if (oldFileName!=fileName)
70 fprintf(stderr,
"Logging to %s\n",qPrintable(fileName));
81 Q_ASSERT(settings!=
nullptr);
82 Q_ASSERT(refreshInterval>=0);
83 this->settings=settings;
85 if (refreshInterval>0)
87 refreshTimer.start(refreshInterval,
this);
89 flushTimer.start(1000,
this);
111 if (logMessage->
getType()>=QtCriticalMsg)
119 qWarning(
"Cannot write to log file %s: %s",qPrintable(fileName),qPrintable(file->errorString()));
133 void FileLogger::open()
135 if (fileName.isEmpty())
137 qWarning(
"Name of logFile is empty");
140 file=
new QFile(fileName);
141 if (!file->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
143 qWarning(
"Cannot open log file %s: %s",qPrintable(fileName),qPrintable(file->errorString()));
150 void FileLogger::close()
160 void FileLogger::rotate() {
165 QFile bakFile(QString(
"%1.%2").arg(fileName).arg(count+1));
166 if (bakFile.exists())
177 while (maxBackups>0 && count>=maxBackups)
179 QFile::remove(QString(
"%1.%2").arg(fileName).arg(count));
184 for (
int i=count; i>0; --i) {
185 QFile::rename(QString(
"%1.%2").arg(fileName).arg(i),QString(
"%1.%2").arg(fileName).arg(i+1));
189 QFile::rename(fileName,fileName+
".1");
199 else if (event->timerId()==refreshTimer.timerId())
203 else if (event->timerId()==flushTimer.timerId() && file)
211 if (maxSize>0 && file->size()>=maxSize)
void timerEvent(QTimerEvent *event)
Handler for timer events.
FileLogger(QSettings *settings, const int refreshInterval=10000, QObject *parent=nullptr)
Constructor.
virtual void write(const LogMessage *logMessage)
Write a message to the log file.
virtual ~FileLogger()
Destructor.
Represents a single log message together with some data that are used to decorate the log message.
QString toString(const QString &msgFormat, const QString ×tampFormat) const
Returns the log message as decorated string.
QtMsgType getType() const
Get the message type.
Decorates and writes log messages to the console, stderr.
QtMsgType minLevel
Minimum level of message types that are written out directly or trigger writing the buffered content.
QString timestampFormat
Format string of timestamps.
virtual void write(const LogMessage *logMessage)
Decorate and write a log message to stderr.
static QMutex mutex
Used to synchronize access of concurrent threads.
int bufferSize
Size of backtrace buffer, number of messages per thread.
QString msgFormat
Format string for message decoration.