Skip to content

Commit

Permalink
fix: tweak some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Candinya committed Apr 25, 2024
1 parent dfca0a1 commit f3bef88
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
4 changes: 2 additions & 2 deletions ipdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ IPDB::IPDB() { // 构造函数
openDatabaseStatus = MMDB_open(GEOIP2_CITY_MMDB, MMDB_MODE_MMAP, &CityDB);
if (openDatabaseStatus != MMDB_SUCCESS) {
// Open failed
qFatal(QString("Failed to open GeoIP2 City database from %1 with error: %2").arg(GEOIP2_CITY_MMDB, MMDB_strerror(openDatabaseStatus)).toStdString().c_str());
qCritical() << QString("Failed to open GeoIP2 City database from %1 with error: %2").arg(GEOIP2_CITY_MMDB, MMDB_strerror(openDatabaseStatus));
}
openDatabaseStatus = MMDB_open(GEOIP2_ISP_MMDB, MMDB_MODE_MMAP, &ISPDB);
if (openDatabaseStatus != MMDB_SUCCESS) {
// Open failed
qFatal(QString("Failed to open GeoIP2 ISP database from %1 with error: %2").arg(GEOIP2_ISP_MMDB, MMDB_strerror(openDatabaseStatus)).toStdString().c_str());
qCritical() << QString("Failed to open GeoIP2 ISP database from %1 with error: %2").arg(GEOIP2_ISP_MMDB, MMDB_strerror(openDatabaseStatus));
}

}
Expand Down
40 changes: 30 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,46 @@ void customMessageHandler(QtMsgType type, const QMessageLogContext &context, con
level = QString("Critical");
break;
case QtFatalMsg:
level = QString("Fatal");
break;
// 特殊处理: Fatal 是完全无法恢复、需要立刻停止级别的错误,所以不应该进入正常处理流程,而应该尽快结束程序。
// 应用程序级别的致命问题请使用 qCritical 来输出。
// 并且也不应该使用程序本身的 UI 来报错,而应该创建崩溃转储信息。

// 创建时间戳
QString crashTs = QDateTime::currentDateTime().toString("yyyy-MM-dd_hh-mm-ss");

// 创建以时间戳命名的崩溃日志,存储到系统临时目录中
QString crashReportFileName = QDir::cleanPath(QDir::tempPath() + QDir::separator() +QString("NyaTrace_CrashReport_%1.log").arg(crashTs));

// 打开文件,创建写入流
QFile crashReportFile(crashReportFileName);
QTextStream crashReportTextStream(&crashReportFile);

// 写入数据
crashReportTextStream << msg;

// 终止一切执行
abort();

// 立刻返回
return;
}

QString ts = QDateTime::currentDateTime().toString("hh:mm:ss");

// 输出调试日志
qDebug() << "新的日志到来" << level << msg;
qDebug() << "New log arrive:" << level << msg;

// 检查日志窗口是否开启
if (ntlw != nullptr) {
if (!ntlw->isActiveWindow()) {
// 没有开启
if (type == QtFatalMsg) {
if (type == QtCriticalMsg) {
// 因为是 fatal 日志,所以必须让用户感知到
qDebug() << "显示日志窗口以让用户看到日志";
qDebug() << "Show log dialog to let users know";
ntlw->show();
}
}

qDebug() << "向日志窗口输出日志";

// 启用互斥锁
logMutex.lock();

Expand All @@ -64,9 +82,11 @@ void customMessageHandler(QtMsgType type, const QMessageLogContext &context, con
logMutex.unlock();
}

if (type == QtFatalMsg) {
// 销毁主窗口,仅保留日志窗口
delete ntgw;
if (type == QtCriticalMsg) {
// 尝试销毁主窗口
if (ntgw != nullptr) {
delete ntgw;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion nyatrace_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ NyaTraceGUI::NyaTraceGUI(QWidget *parent)
// WinSock2 相关初始化
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { // 进行相应的socket库绑定,MAKEWORD(2,2)表示使用WINSOCK2版本
//emit setMessage(QString("WinSock2 动态链接库初始化失败,错误代码: %1 。").arg(WSAGetLastError())); // 提示信息
qFatal(QString("Failed to start winsocks2 library with error: %1").arg(WSAGetLastError()).toStdString().c_str());
qCritical() << QString("Failed to start winsocks2 library with error: %1").arg(WSAGetLastError());
}

// 建立路由追踪线程
Expand Down
6 changes: 3 additions & 3 deletions tracing_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TracingCore::TracingCore() {
if (hIcmpDll == NULL) {
//emit setMessage(QString("icmp.dll 动态链接库加载失败"));
WSACleanup();
qFatal("Failed to load ICMP module");
qCritical() << "Failed to load ICMP module";
}

// 从动态链接库中获取所需的函数入口地址
Expand All @@ -34,12 +34,12 @@ TracingCore::TracingCore() {
if ((hIcmp = IcmpCreateFile()) == INVALID_HANDLE_VALUE) {
//emit setMessage(QString("ICMP 句柄打开失败"));
WSACleanup();
qFatal("Failed to open ICMP handle");
qCritical() << "Failed to open ICMP handle";
}
if ((hIcmp6 = Icmp6CreateFile()) == INVALID_HANDLE_VALUE) {
//emit setMessage(QString("ICMP 句柄打开失败"));
WSACleanup();
qFatal("Failed to open ICMP6 handle");
qCritical() << "Failed to open ICMP6 handle";
}

// 新建一个线程池
Expand Down

0 comments on commit f3bef88

Please sign in to comment.