Files
frelay/Gui/Control/LogControl.cpp

137 lines
3.8 KiB
C++
Raw Normal View History

2025-06-19 16:20:01 +08:00
#include "LogControl.h"
2025-06-15 14:31:54 +08:00
#include <QDateTime>
2025-06-19 16:20:01 +08:00
#include <QListWidgetItem>
2026-02-02 21:46:02 +08:00
#include <QMessageBox>
2025-06-15 14:31:54 +08:00
#include <QStandardItem>
#include <chrono>
#include <ctime>
2025-06-15 14:31:54 +08:00
#include <iomanip>
#include <sstream>
#include "ui_LogControl.h"
LogPrint::LogPrint(QWidget* parent) : QWidget(parent), ui(new Ui::LogPrint)
{
ui->setupUi(this);
InitControl();
InitColor();
2025-06-15 14:31:54 +08:00
}
void LogPrint::InitControl()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
isLightMode_ = QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Light;
styleHints_ = QGuiApplication::styleHints();
connect(styleHints_, &QStyleHints::colorSchemeChanged, this, &LogPrint::ColorChange);
#endif
ui->pedText->setReadOnly(true);
2026-02-02 21:46:02 +08:00
connect(ui->btnClear, &QPushButton::clicked, [this]() {
QMessageBox::StandardButton reply = QMessageBox::question(this, tr("确认清空"), tr("确定要清空所有内容吗?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (reply == QMessageBox::Yes) {
ui->pedText->clear();
}
});
2025-06-15 14:31:54 +08:00
}
std::string LogPrint::now_str()
{
auto now = std::chrono::system_clock::now();
auto time_t_now = std::chrono::system_clock::to_time_t(now);
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
// std::ostringstream timestamp;
// timestamp << std::put_time(std::localtime(&time_t_now), "%H:%M:%S") << "." << std::setfill('0') << std::setw(3)
// << milliseconds.count() << " ";
char timeStr[20];
std::strftime(timeStr, sizeof(timeStr), "%H:%M:%S", std::localtime(&time_t_now));
2025-06-15 14:31:54 +08:00
std::ostringstream timestamp;
timestamp << timeStr << "." << std::setfill('0') << std::setw(3) << milliseconds.count() << " ";
2025-06-15 14:31:54 +08:00
return timestamp.str();
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
void LogPrint::ColorChange(Qt::ColorScheme scheme)
{
if (scheme == Qt::ColorScheme::Dark) {
isLightMode_ = false;
} else {
isLightMode_ = true;
}
RePrintLog();
}
#endif
2025-06-15 14:31:54 +08:00
LogPrint::~LogPrint()
{
delete ui;
}
static const QColor DARK_INFO_COLOR = QColor(255, 255, 0);
static const QColor DARK_WARN_COLOR = QColor(255, 102, 0);
static const QColor DARK_ERROR_COLOR = QColor(255, 0, 255);
static const QColor DARK_DEBUG_COLOR = QColor(0, 255, 255);
void LogPrint::InitColor()
{
lightMap_[PT_INFO] = Qt::black;
lightMap_[PT_WARN] = Qt::blue;
lightMap_[PT_ERROR] = Qt::red;
lightMap_[PT_DEBUG] = Qt::gray;
darkMap_[PT_INFO] = DARK_INFO_COLOR;
darkMap_[PT_WARN] = DARK_WARN_COLOR;
darkMap_[PT_ERROR] = DARK_ERROR_COLOR;
darkMap_[PT_DEBUG] = DARK_DEBUG_COLOR;
}
void LogPrint::RePrintLog()
{
{
QMutexLocker locker(&mutex_);
ui->pedText->clear();
}
for (auto& entry : logEntries_) {
Print(entry.message, entry.level, false);
}
}
2025-06-15 14:31:54 +08:00
void LogPrint::Info(const QString& message)
{
Print(message, PT_INFO);
2025-06-15 14:31:54 +08:00
}
void LogPrint::Warn(const QString& message)
{
Print(message, PT_WARN);
2025-06-15 14:31:54 +08:00
}
void LogPrint::Error(const QString& message)
{
Print(message, PT_ERROR);
2025-06-15 14:31:54 +08:00
}
void LogPrint::Debug(const QString& message)
{
Print(message, PT_DEBUG);
2025-06-15 14:31:54 +08:00
}
void LogPrint::Print(const QString& message, PrintType type, bool recordHis)
2025-06-15 14:31:54 +08:00
{
QMutexLocker locker(&mutex_);
QString timeStr = QString("%1%2").arg(QString::fromStdString(now_str())).arg(message);
QString r;
if (isLightMode_) {
r = QString("<span style='color:%1;'>%2</span>").arg(lightMap_[type].color().name()).arg(timeStr.toHtmlEscaped());
} else {
r = QString("<span style='color:%1;'>%2</span>").arg(darkMap_[type].color().name()).arg(timeStr.toHtmlEscaped());
}
if (recordHis) {
logEntries_.append({message, type});
}
ui->pedText->appendHtml(r);
2025-06-15 14:31:54 +08:00
}