upload all files

This commit is contained in:
2026-01-07 21:26:28 +08:00
parent 1d35a422ec
commit 6d7544e0a3
17 changed files with 2987 additions and 0 deletions

72
Modules/Logger.cpp Normal file
View File

@@ -0,0 +1,72 @@
#include "Logger.h"
#include "global.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <array>
#include <string_view>
#include <mutex>
#include <QDebug> // For Qt logging
structIs is;
std::string colorOut_256(std::string_view s, int ForeColor = 7)
{
std::ostringstream oss;
oss << "\033[38;5;" << ForeColor << "m" << s << "\033[m";
return oss.str();
}
std::string getSystemTime() {
using namespace std::chrono;
const auto now = system_clock::now();
const auto now_time_t = system_clock::to_time_t(now);
const auto ms = duration_cast<milliseconds>(now.time_since_epoch()).count() % 1000;
std::tm ltm{};
localtime_s(&ltm, &now_time_t);
std::ostringstream oss;
oss << std::put_time(&ltm, "%Y-%m-%d %H:%M:%S")
<< '.' << std::setw(3) << std::setfill('0') << ms;
return oss.str();
}
// Logger mutex to ensure whole log lines are emitted atomically
static std::mutex loggerMutex;
void postLog(const std::string& message, int level) {
const auto timeNow = getSystemTime();
constexpr std::array<const char*, 5> levelNames = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
constexpr std::array<int, 5> levelColors = {34, 27, 220, 196, 124};
const int idx = (level >= 0 && level < static_cast<int>(levelNames.size())) ? level : 1; // default to INFO
// Lock to make reading isDebug and all output atomic across threads
std::scoped_lock lock(loggerMutex);
if (is.debug == false) {
if (idx == 0) return; // skip DEBUG when debug is off
qInfo() << "[" << QString::fromStdString(timeNow) << " - " << QString::fromStdString(levelNames[idx]) << "] " << QString::fromStdString(message);
return;
}
// isDebug == 1 -> colored output
const std::string levelDisplay = colorOut_256(levelNames[idx], levelColors[idx]);
QString levelDisplayQt = QString::fromStdString(levelDisplay);
if (idx == 0) { // DEBUG level
qDebug("[%s - %s] %s", QString::fromStdString(timeNow).toUtf8().data(), levelDisplayQt.toUtf8().data(), QString::fromStdString(message).toUtf8().data());
} else if (idx == 1) { // INFO level
qInfo("[%s - %s] %s", QString::fromStdString(timeNow).toUtf8().data(), levelDisplayQt.toUtf8().data(), QString::fromStdString(message).toUtf8().data());
} else if (idx == 2) { // WARNING level
qWarning("[%s - %s] %s", QString::fromStdString(timeNow).toUtf8().data(), levelDisplayQt.toUtf8().data(), QString::fromStdString(message).toUtf8().data());
} else if (idx == 3) { // ERROR level
qCritical("[%s - %s] %s", QString::fromStdString(timeNow).toUtf8().data(), levelDisplayQt.toUtf8().data(), QString::fromStdString(message).toUtf8().data());
} else if (idx == 4) { // FATAL level
qFatal("[%s - %s] %s", QString::fromStdString(timeNow).toUtf8().data(), levelDisplayQt.toUtf8().data(), QString::fromStdString(message).toUtf8().data());
}
}