Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt6 fixes #36

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)

SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand Down
6 changes: 6 additions & 0 deletions include/FileAppender.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class CUTELOGGERSHARED_EXPORT FileAppender : public AbstractStringAppender
QString fileName() const;
void setFileName(const QString&);

bool flushOnWrite() const;
void setFlushOnWrite(bool);

bool flush();

bool reopenFile();

protected:
Expand All @@ -42,6 +47,7 @@ class CUTELOGGERSHARED_EXPORT FileAppender : public AbstractStringAppender

private:
QFile m_logFile;
bool m_flushOnWrite;
QTextStream m_logStream;
mutable QMutex m_logFileMutex;
};
Expand Down
3 changes: 2 additions & 1 deletion include/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QString>
#include <QDebug>
#include <QDateTime>
#include <QElapsedTimer>

// Local
#include "CuteLogger_global.h"
Expand Down Expand Up @@ -224,7 +225,7 @@ class CUTELOGGERSHARED_EXPORT LoggerTimingHelper

private:
Logger* m_logger;
QTime m_time;
QElapsedTimer m_time;
Logger::LogLevel m_logLevel;
Logger::TimingMode m_timingMode;
const char* m_file;
Expand Down
11 changes: 6 additions & 5 deletions src/AbstractStringAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <QReadLocker>
#include <QWriteLocker>
#include <QDateTime>
#include <QRegExp>
#include <QRegularExpression>
#include <QCoreApplication>
#include <QThread>

Expand Down Expand Up @@ -155,12 +155,13 @@ QByteArray AbstractStringAppender::qCleanupFuncinfo(const char* name)
}

bool hasLambda = false;
QRegExp lambdaRegex("::<lambda\\(.*\\)>");
int lambdaIndex = lambdaRegex.indexIn(QString::fromLatin1(info));
QRegularExpression lambdaRegex("::<lambda\\(.*?\\)>");
QRegularExpressionMatch match = lambdaRegex.match(QString::fromLatin1(info));
int lambdaIndex = match.capturedStart();
if (lambdaIndex != -1)
{
hasLambda = true;
info.remove(lambdaIndex, lambdaRegex.matchedLength());
info.remove(lambdaIndex, match.capturedLength());
}

// operator names with '(', ')', '<', '>' in it
Expand Down Expand Up @@ -405,7 +406,7 @@ QString AbstractStringAppender::formattedString(const QDateTime& timeStamp, Logg

// Filename without a path
else if (command == QLatin1String("file"))
chunk = QString(QLatin1String(file)).section(QRegExp("[/\\\\]"), -1);
chunk = QString(QLatin1String(file)).section(QRegularExpression("[/\\\\]"), -1);

// Source line number
else if (command == QLatin1String("line"))
Expand Down
35 changes: 34 additions & 1 deletion src/FileAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

//! Constructs the new file appender assigned to file with the given name.
FileAppender::FileAppender(const QString& fileName)
: m_flushOnWrite(false)
{
setFileName(fileName);
}
Expand Down Expand Up @@ -65,6 +66,37 @@ void FileAppender::setFileName(const QString& s)
}


bool FileAppender::flushOnWrite() const
{
return m_flushOnWrite;
}


//! Allows FileAppender to flush file immediately after writing a log record.
/**
* Default value is false. This could result in substantial app slowdown when writing massive amount of log records
* with FileAppender on a rather slow file system due to FileAppender blocking until the data would be phisically
* written.
*
* Leaving this as is may result in some log data not being written if the application crashes.
*/
void FileAppender::setFlushOnWrite(bool flush)
{
m_flushOnWrite = flush;
}


//! Force-flush any remaining buffers to file system. Returns true if successful, otherwise returns false.
bool FileAppender::flush()
{
QMutexLocker locker(&m_logFileMutex);
if (m_logFile.isOpen())
return m_logFile.flush();
else
return true;
}


bool FileAppender::reopenFile()
{
closeFile();
Expand Down Expand Up @@ -104,7 +136,8 @@ void FileAppender::append(const QDateTime& timeStamp, Logger::LogLevel logLevel,
{
m_logStream << formattedString(timeStamp, logLevel, file, line, function, category, message);
m_logStream.flush();
m_logFile.flush();
if (m_flushOnWrite)
m_logFile.flush();
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <QSemaphore>
#include <QDateTime>
#include <QIODevice>
#include <QTextCodec>

#if defined(Q_OS_ANDROID)
# include <android/log.h>
Expand Down Expand Up @@ -600,8 +599,14 @@ Logger::~Logger()

// Cleanup appenders
QMutexLocker appendersLocker(&d->loggerMutex);
#if QT_VERSION >= 0x050e00
QSet<AbstractAppender*> deleteList(d->appenders.begin(), d->appenders.end());
auto cal = d->categoryAppenders.values();
deleteList.unite(QSet<AbstractAppender*>(cal.begin(), cal.end()));
#else
QSet<AbstractAppender*> deleteList(QSet<AbstractAppender*>::fromList(d->appenders));
deleteList.unite(QSet<AbstractAppender*>::fromList(d->categoryAppenders.values()));
#endif
qDeleteAll(deleteList);

appendersLocker.unlock();
Expand Down Expand Up @@ -1032,7 +1037,11 @@ void LoggerTimingHelper::start(const char* msg, ...)
{
va_list va;
va_start(va, msg);
#if QT_VERSION >= 0x050500
m_block = QString().vasprintf(msg, va);
#else
m_block = QString().vsprintf(msg, va);
#endif
va_end(va);

m_time.start();
Expand Down Expand Up @@ -1062,7 +1071,7 @@ LoggerTimingHelper::~LoggerTimingHelper()
else
message = QString(QLatin1String("\"%1\" finished in ")).arg(m_block);

int elapsed = m_time.elapsed();
qint64 elapsed = m_time.elapsed();
if (elapsed >= 10000 && m_timingMode == Logger::TimingAuto)
message += QString(QLatin1String("%1 s.")).arg(elapsed / 1000);
else
Expand Down
4 changes: 4 additions & 0 deletions src/RollingFileAppender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,11 @@ void RollingFileAppender::computeRollOverTime()
break;
default:
Q_ASSERT_X(false, "DailyRollingFileAppender::computeInterval()", "Invalid datePattern constant");
#if QT_VERSION >= 0x050800
m_rollOverTime = QDateTime::fromSecsSinceEpoch(0);
#else
m_rollOverTime = QDateTime::fromTime_t(0);
#endif
}

m_rollOverSuffix = start.toString(m_datePatternString);
Expand Down