mirror of
https://github.com/nomic-ai/gpt4all.git
synced 2024-09-11 13:25:35 +03:00
Implemented logging mechanism (#785)
This commit is contained in:
parent
243c762411
commit
ddb5fa718d
@ -71,6 +71,7 @@ qt_add_executable(chat
|
||||
localdocs.h localdocs.cpp localdocsmodel.h localdocsmodel.cpp
|
||||
llm.h llm.cpp
|
||||
server.h server.cpp
|
||||
logger.h logger.cpp
|
||||
sysinfo.h
|
||||
)
|
||||
|
||||
|
61
gpt4all-chat/logger.cpp
Normal file
61
gpt4all-chat/logger.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include "logger.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
#include <QStandardPaths>
|
||||
#include <QDateTime>
|
||||
|
||||
class MyLogger: public Logger { };
|
||||
Q_GLOBAL_STATIC(MyLogger, loggerInstance)
|
||||
Logger *Logger::globalInstance()
|
||||
{
|
||||
return loggerInstance();
|
||||
}
|
||||
|
||||
Logger::Logger()
|
||||
{
|
||||
// Get log file dir
|
||||
auto dir = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
|
||||
// Remove old log file
|
||||
QFile::remove(dir+"/log-prev.txt");
|
||||
QFile::rename(dir+"/log.txt", dir+"/log-prev.txt");
|
||||
// Open new log file
|
||||
m_file.setFileName(dir+"/log.txt");
|
||||
if (!m_file.open(QIODevice::NewOnly | QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
qWarning() << "Failed to open log file, logging to stdout...";
|
||||
m_file.open(stdout, QIODevice::WriteOnly | QIODevice::Text);
|
||||
}
|
||||
// On success, install message handler
|
||||
qInstallMessageHandler(Logger::messageHandler);
|
||||
}
|
||||
|
||||
void Logger::messageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg)
|
||||
{
|
||||
auto logger = globalInstance();
|
||||
// Get message type as string
|
||||
QString typeString;
|
||||
switch (type) {
|
||||
case QtDebugMsg:
|
||||
typeString = "Debug";
|
||||
break;
|
||||
case QtInfoMsg:
|
||||
typeString = "Info";
|
||||
break;
|
||||
case QtWarningMsg:
|
||||
typeString = "Warning";
|
||||
break;
|
||||
case QtCriticalMsg:
|
||||
typeString = "Critical";
|
||||
break;
|
||||
case QtFatalMsg:
|
||||
typeString = "Fatal";
|
||||
break;
|
||||
default:
|
||||
typeString = "???";
|
||||
}
|
||||
// Get time and date
|
||||
auto timestamp = QDateTime::currentDateTime().toString();
|
||||
// Write message
|
||||
logger->m_file.write(QString("[%1] (%2): %4\n").arg(typeString, timestamp, msg).toStdString().c_str());
|
||||
logger->m_file.flush();
|
||||
}
|
18
gpt4all-chat/logger.h
Normal file
18
gpt4all-chat/logger.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef LOGGER_H
|
||||
#define LOGGER_H
|
||||
#include <QFile>
|
||||
|
||||
class Logger
|
||||
{
|
||||
QFile m_file;
|
||||
|
||||
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
||||
|
||||
public:
|
||||
static Logger *globalInstance();
|
||||
|
||||
explicit Logger();
|
||||
friend class MyLogger;
|
||||
};
|
||||
|
||||
#endif // LOGGER_H
|
@ -10,6 +10,7 @@
|
||||
#include "download.h"
|
||||
#include "network.h"
|
||||
#include "config.h"
|
||||
#include "logger.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
@ -19,6 +20,8 @@ int main(int argc, char *argv[])
|
||||
QCoreApplication::setApplicationVersion(APP_VERSION);
|
||||
QSettings::setDefaultFormat(QSettings::IniFormat);
|
||||
|
||||
Logger::globalInstance();
|
||||
|
||||
QGuiApplication app(argc, argv);
|
||||
QQmlApplicationEngine engine;
|
||||
qmlRegisterSingletonInstance("llm", 1, 0, "LLM", LLM::globalInstance());
|
||||
|
Loading…
Reference in New Issue
Block a user