2018-12-20 23:30:35 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include <QDateTime>
|
2018-12-27 01:18:33 +00:00
|
|
|
#include <QDir>
|
|
|
|
#include <QStandardPaths>
|
2019-05-05 21:11:00 +01:00
|
|
|
#include <QSysInfo>
|
|
|
|
|
|
|
|
// Enabling this does not seem to be simple to color console output
|
|
|
|
// on Windows for all CLIs without external libraries or extreme bloat.
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
#define ERROR_COLOR ""
|
|
|
|
#define WARNING_COLOR ""
|
|
|
|
#define INFO_COLOR ""
|
|
|
|
#define CLEAR_COLOR ""
|
|
|
|
#else
|
|
|
|
#define ERROR_COLOR "\033[31;1m"
|
|
|
|
#define WARNING_COLOR "\033[1;33m"
|
|
|
|
#define INFO_COLOR "\033[32m"
|
|
|
|
#define CLEAR_COLOR "\033[0m"
|
|
|
|
#endif
|
2018-12-20 23:30:35 +00:00
|
|
|
|
|
|
|
void logInfo(QString message) {
|
|
|
|
log(message, LogType::LOG_INFO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void logWarn(QString message) {
|
|
|
|
log(message, LogType::LOG_WARN);
|
|
|
|
}
|
|
|
|
|
2020-02-12 17:23:28 +00:00
|
|
|
static QString mostRecentError;
|
|
|
|
|
2018-12-20 23:30:35 +00:00
|
|
|
void logError(QString message) {
|
2020-02-12 17:23:28 +00:00
|
|
|
mostRecentError = message;
|
2018-12-20 23:30:35 +00:00
|
|
|
log(message, LogType::LOG_ERROR);
|
|
|
|
}
|
|
|
|
|
2019-05-05 21:11:00 +01:00
|
|
|
QString colorizeMessage(QString message, LogType type) {
|
|
|
|
QString colorized = message;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case LogType::LOG_INFO:
|
|
|
|
colorized = colorized.replace("INFO", INFO_COLOR "INFO" CLEAR_COLOR);
|
|
|
|
break;
|
|
|
|
case LogType::LOG_WARN:
|
|
|
|
colorized = colorized.replace("WARN", WARNING_COLOR "WARN" CLEAR_COLOR);
|
|
|
|
break;
|
|
|
|
case LogType::LOG_ERROR:
|
|
|
|
colorized = colorized.replace("ERROR", ERROR_COLOR "ERROR" CLEAR_COLOR);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return colorized;
|
|
|
|
}
|
|
|
|
|
2018-12-20 23:30:35 +00:00
|
|
|
void log(QString message, LogType type) {
|
|
|
|
QString now = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
QString typeString = "";
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case LogType::LOG_INFO:
|
|
|
|
typeString = " [INFO]";
|
|
|
|
break;
|
|
|
|
case LogType::LOG_WARN:
|
|
|
|
typeString = " [WARN]";
|
|
|
|
break;
|
|
|
|
case LogType::LOG_ERROR:
|
|
|
|
typeString = "[ERROR]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
message = QString("%1 %2 %3").arg(now).arg(typeString).arg(message);
|
2018-12-27 01:18:33 +00:00
|
|
|
|
2020-02-12 17:23:28 +00:00
|
|
|
qDebug().noquote() << colorizeMessage(message, type);
|
|
|
|
QFile outFile(getLogPath());
|
|
|
|
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
|
|
|
|
QTextStream ts(&outFile);
|
2020-08-27 01:42:42 +01:00
|
|
|
ts << message << Qt::endl;
|
2020-02-12 17:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString getLogPath() {
|
2018-12-27 01:18:33 +00:00
|
|
|
QString settingsPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
QDir dir(settingsPath);
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkpath(settingsPath);
|
2020-02-12 17:23:28 +00:00
|
|
|
return dir.absoluteFilePath("porymap.log");
|
|
|
|
}
|
2018-12-27 01:18:33 +00:00
|
|
|
|
2020-02-12 17:23:28 +00:00
|
|
|
QString getMostRecentError() {
|
|
|
|
return mostRecentError;
|
2018-12-20 23:30:35 +00:00
|
|
|
}
|
2020-10-10 19:06:51 +01:00
|
|
|
|
|
|
|
bool cleanupLargeLog() {
|
|
|
|
QFile logFile(getLogPath());
|
|
|
|
if (logFile.size() < 20000000)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool removed = logFile.remove();
|
|
|
|
if (removed)
|
|
|
|
logWarn(QString("Previous log file %1 was cleared due to being over 20MB in size.").arg(getLogPath()));
|
|
|
|
return removed;
|
|
|
|
}
|