mirror of
https://github.com/nomic-ai/gpt4all.git
synced 2025-08-08 11:27:14 +00:00
guard logger QFile access with a mutex
This fixes the following assertion failure in Qt debug builds: ``` ASSERT: "bytes <= bufferSize" in file C:\Users\qt\work\qt\qtbase\src\corelib\tools\qringbuffer.cpp, line 75 ``` The message handler is called from the thread that sends the message, so we need to make sure only one thread can access the file at a time, since QFile is not inherently threadsafe. Signed-off-by: Jared Van Bortel <jared@nomic.ai>
This commit is contained in:
parent
350bd0a793
commit
4e18c81107
@ -4,6 +4,7 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QGlobalStatic>
|
#include <QGlobalStatic>
|
||||||
#include <QIODevice>
|
#include <QIODevice>
|
||||||
|
#include <QMutexLocker>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
@ -62,8 +63,11 @@ void Logger::messageHandler(QtMsgType type, const QMessageLogContext &, const QS
|
|||||||
}
|
}
|
||||||
// Get time and date
|
// Get time and date
|
||||||
auto timestamp = QDateTime::currentDateTime().toString();
|
auto timestamp = QDateTime::currentDateTime().toString();
|
||||||
// Write message
|
|
||||||
const std::string out = u"[%1] (%2): %3\n"_s.arg(typeString, timestamp, msg).toStdString();
|
const std::string out = u"[%1] (%2): %3\n"_s.arg(typeString, timestamp, msg).toStdString();
|
||||||
|
|
||||||
|
// Write message
|
||||||
|
QMutexLocker locker(&logger->m_mutex);
|
||||||
logger->m_file.write(out.c_str());
|
logger->m_file.write(out.c_str());
|
||||||
logger->m_file.flush();
|
logger->m_file.flush();
|
||||||
std::cerr << out;
|
std::cerr << out;
|
||||||
|
@ -2,19 +2,23 @@
|
|||||||
#define LOGGER_H
|
#define LOGGER_H
|
||||||
|
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QMutex>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QtLogging>
|
#include <QtLogging>
|
||||||
|
|
||||||
class Logger
|
class Logger {
|
||||||
{
|
|
||||||
QFile m_file;
|
|
||||||
|
|
||||||
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
explicit Logger();
|
||||||
|
|
||||||
static Logger *globalInstance();
|
static Logger *globalInstance();
|
||||||
|
|
||||||
explicit Logger();
|
private:
|
||||||
|
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QFile m_file;
|
||||||
|
QMutex m_mutex;
|
||||||
|
|
||||||
friend class MyLogger;
|
friend class MyLogger;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user