2025-02-25 16:33:11 +08:00
|
|
|
|
// custom_printf.h
|
|
|
|
|
|
#ifndef CUSTOM_PRINTF_H
|
|
|
|
|
|
#define CUSTOM_PRINTF_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <stdarg.h>
|
2025-03-03 18:20:00 +08:00
|
|
|
|
#include <QtGlobal> // ✅ 确保 QtMsgType 被正确包含
|
2025-02-26 16:39:10 +08:00
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2025-02-25 16:33:11 +08:00
|
|
|
|
#include <list>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
// 假设这些是你管理输出的列表
|
|
|
|
|
|
extern std::list<std::string> errorList;
|
|
|
|
|
|
extern std::list<std::string> warnList;
|
|
|
|
|
|
extern std::list<std::string> normalList;
|
2025-02-28 16:28:15 +08:00
|
|
|
|
extern std::list<std::string> debugList;
|
2025-02-25 16:33:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 开关
|
|
|
|
|
|
extern bool errorOutputEnabled;
|
|
|
|
|
|
extern bool warnOutputEnabled;
|
|
|
|
|
|
extern bool normalOutputEnabled;
|
2025-02-28 16:28:15 +08:00
|
|
|
|
extern bool debugOutputEnabled;
|
2025-02-25 16:33:11 +08:00
|
|
|
|
|
2025-02-26 16:39:10 +08:00
|
|
|
|
void redirectErrorOutput(bool enabled);
|
|
|
|
|
|
void redirectWarnOutput(bool enabled);
|
|
|
|
|
|
void redirectNormalOutput(bool enabled);
|
2025-03-03 18:20:00 +08:00
|
|
|
|
// ------------------ 重定向函数 ------------------
|
|
|
|
|
|
void redirectDebugOutput(bool enable);
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------ Qt 的消息处理函数 (Qt4) ------------------
|
|
|
|
|
|
// Qt4 使用 qInstallMsgHandler(...),签名: void myMsgHandler(QtMsgType, const char*)
|
|
|
|
|
|
void myQtMsgHandler(QtMsgType type, const char* msg);
|
2025-02-26 16:39:10 +08:00
|
|
|
|
|
2025-02-28 16:28:15 +08:00
|
|
|
|
extern pthread_mutex_t errorListMutex;
|
|
|
|
|
|
extern pthread_mutex_t warnListMutex;
|
|
|
|
|
|
extern pthread_mutex_t normalListMutex;
|
|
|
|
|
|
extern pthread_mutex_t debugListMutex;
|
|
|
|
|
|
|
|
|
|
|
|
// **声明原始 echo_msgX(),让编译器知道它们存在**
|
|
|
|
|
|
void real_echo_msg(const char *format, ...);
|
|
|
|
|
|
|
|
|
|
|
|
// **拦截所有 `echo_msgX()`,让它同时存入 `normalList`**
|
|
|
|
|
|
#define echo_msgX(format, ...) \
|
|
|
|
|
|
do { \
|
|
|
|
|
|
char buffer[1024]; \
|
|
|
|
|
|
snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
|
2025-03-03 18:20:00 +08:00
|
|
|
|
\
|
|
|
|
|
|
/* ✅ 先打印到 Shell,确保不会卡死 */ \
|
|
|
|
|
|
printf("%s", buffer); \
|
|
|
|
|
|
fflush(stdout); /* ✅ 立即刷新,防止标准输出缓存 */ \
|
|
|
|
|
|
\
|
|
|
|
|
|
/* ✅ 先创建日志副本,避免锁住 `normalList` 过久 */ \
|
|
|
|
|
|
std::string logEntry(buffer); \
|
|
|
|
|
|
\
|
|
|
|
|
|
/* ✅ 仅在 `normalList` 操作时加锁,避免死锁 */ \
|
2025-02-28 16:28:15 +08:00
|
|
|
|
pthread_mutex_lock(&normalListMutex); \
|
2025-03-03 18:20:00 +08:00
|
|
|
|
normalList.push_back(logEntry); \
|
2025-02-28 16:28:15 +08:00
|
|
|
|
pthread_mutex_unlock(&normalListMutex); \
|
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
// **宏自动展开不同的 `echo_msg` 变体**
|
|
|
|
|
|
#define echo_msg1 echo_msgX
|
|
|
|
|
|
#define echo_msg2 echo_msgX
|
|
|
|
|
|
#define echo_msg3 echo_msgX
|
|
|
|
|
|
#define echo_msg4 echo_msgX
|
|
|
|
|
|
#define echo_msg5 echo_msgX
|
|
|
|
|
|
#define echo_msg6 echo_msgX
|
|
|
|
|
|
#define echo_msg7 echo_msgX
|
|
|
|
|
|
#define echo_msg8 echo_msgX
|
|
|
|
|
|
#define echo_msg9 echo_msgX
|
|
|
|
|
|
#define echo_msg10 echo_msgX
|
|
|
|
|
|
#define echo_msg11 echo_msgX
|
|
|
|
|
|
// **如果有更多的 `echo_msgX()`,继续定义**
|
2025-02-26 16:39:10 +08:00
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
|
|
{
|
|
|
|
|
|
#endif
|
2025-02-25 16:33:11 +08:00
|
|
|
|
// 自定义的 printf 函数
|
|
|
|
|
|
int customPrintf(const char* format, ...);
|
2025-02-26 16:39:10 +08:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
2025-02-25 16:33:11 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用宏将 printf 替换为 customPrintf
|
|
|
|
|
|
#define printf customPrintf
|
|
|
|
|
|
|
|
|
|
|
|
#endif // CUSTOM_PRINTF_H
|