fix test shell with debug

This commit is contained in:
lnk
2025-03-03 18:20:00 +08:00
parent a1a184a043
commit 87f0a48ad7
4 changed files with 127 additions and 95 deletions

View File

@@ -11825,8 +11825,11 @@ void value_print(const char *variableName, QTcpSocket *clientSocket) {
clientSocket->write(buffer);
} else if (strcmp(variableName, "frontfun") == 0) {
sprintf(buffer, "frontfun = %s", subdir);
clientSocket->write(buffer);
} else {
clientSocket->write(buffer);
} else if (strcmp(variableName, "log") == 0) {
sprintf(buffer, "showinshellflag = %d,debugOutputEnabled = %d,normalOutputEnabled = %d,warnOutputEnabled = %d,errorOutputEnabled = %d", showinshellflag,debugOutputEnabled,normalOutputEnabled,warnOutputEnabled,errorOutputEnabled);
clientSocket->write(buffer);
}else {
clientSocket->write("Unknown variable name\n> ");
}
pthread_mutex_unlock(&mtx); std::cout << "value_print free lock !!!!!!!!!!!" << std::endl;
@@ -11880,6 +11883,7 @@ void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSock
if (input == "q") { // ? <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD> `q`<60><><EFBFBD>˳<EFBFBD><CBB3><EFBFBD>־ģʽ
std::cout << "Received 'q' from shell socket! Exiting viewlog...\n";
stopViewLog = true;
showinshellflag = false;
break;
}
}
@@ -15178,8 +15182,8 @@ void rocketmq_test_300(int mpnum,int front_index) {
ied_usr = (ied_usr_t*)ied->usr_ext;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD>
if(strcmp(ied_usr->terminal_id, "5a7243ae5e0224e9ed18229d6697c3e2") == 0){
std::cout << "5a7243ae5e0224e9ed18229d6697c3e2 use true message " << std::endl;
if(strcmp(ied_usr->terminal_id, "8499c5ae999d392b4e73112ca0d2d778") == 0){
std::cout << "8499c5ae999d392b4e73112ca0d2d778 use true message " << std::endl;
continue;
}
@@ -15261,6 +15265,8 @@ static std::streambuf* g_originalCoutBuf = NULL;
static std::streambuf* g_originalClogBuf = NULL;
static std::streambuf* g_originalCerrBuf = NULL;
// ------------------ <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>ö<EFBFBD>٣<EFBFBD>C++98<39><38> ------------------
enum LogLevel {
LOGERROR,
@@ -15341,67 +15347,63 @@ private:
switch (m_level) {
case LOGERROR:
if (errorOutputEnabled) {
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(m_buffer);
pthread_mutex_unlock(&debugListMutex);
}
else if (normalOutputEnabled) {
pthread_mutex_lock(&normalListMutex);
normalList.push_back(m_buffer);
pthread_mutex_unlock(&normalListMutex);
}
else if (warnOutputEnabled) {
pthread_mutex_lock(&warnListMutex);
warnList.push_back(m_buffer);
pthread_mutex_unlock(&warnListMutex);
}
else if (errorOutputEnabled) {
pthread_mutex_lock(&errorListMutex);
errorList.push_back(m_buffer);
pthread_mutex_unlock(&errorListMutex);
}
if (warnOutputEnabled) {
pthread_mutex_lock(&warnListMutex);
warnList.push_back(m_buffer);
pthread_mutex_unlock(&warnListMutex);
}
if (normalOutputEnabled) {
pthread_mutex_lock(&normalListMutex);
normalList.push_back(m_buffer);
pthread_mutex_unlock(&normalListMutex);
}
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(m_buffer);
pthread_mutex_unlock(&debugListMutex);
}
break;
case LOGWARN:
if (warnOutputEnabled) {
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(m_buffer);
pthread_mutex_unlock(&debugListMutex);
}
else if (normalOutputEnabled) {
pthread_mutex_lock(&normalListMutex);
normalList.push_back(m_buffer);
pthread_mutex_unlock(&normalListMutex);
}
else if (warnOutputEnabled) {
pthread_mutex_lock(&warnListMutex);
warnList.push_back(m_buffer);
pthread_mutex_unlock(&warnListMutex);
}
if (normalOutputEnabled) {
pthread_mutex_lock(&normalListMutex);
normalList.push_back(m_buffer);
pthread_mutex_unlock(&normalListMutex);
}
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(m_buffer);
pthread_mutex_unlock(&debugListMutex);
}
break;
case LOGNORMAL:
if (normalOutputEnabled) {
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(m_buffer);
pthread_mutex_unlock(&debugListMutex);
}
else if (normalOutputEnabled) {
pthread_mutex_lock(&normalListMutex);
normalList.push_back(m_buffer);
pthread_mutex_unlock(&normalListMutex);
}
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(m_buffer);
pthread_mutex_unlock(&debugListMutex);
}
break;
case LOGDEBUG:
// <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD>ؿ<EFBFBD><D8BF>ˣ<EFBFBD><CBA3>ŷŽ<C5B7> debugList
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(m_buffer);
pthread_mutex_unlock(&debugListMutex);
}
break;
break;
}
m_buffer.clear();
@@ -15421,14 +15423,13 @@ private:
static TeeStreamBuf g_errorTeeBuf;
static TeeStreamBuf g_warnTeeBuf;
static TeeStreamBuf g_normalTeeBuf;
static TeeStreamBuf g_debugTeeBuf; // <20><><EFBFBD><EFBFBD> debug Tee
// ------------------ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<E1B9A9><D2BB>ȫ<EFBFBD>ֵ<EFBFBD> debug <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------
// <20><>ԭʼbufĬ<66><C4AC>Ϊ NULL<4C><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>նˣ<D5B6><CBA3><EFBFBD>ֻ<EFBFBD>浽 debugList(<28><><EFBFBD><EFBFBD><EFBFBD>ؿ<EFBFBD>)
static std::ostream g_debug(&g_debugTeeBuf);
//static std::ostream g_debug(&g_debugTeeBuf);
// <20><> qDebug() ӳ<><EFBFBD><E4B5BD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
#define qDebug() g_debug
//#define qDebug() g_debug
// ------------------ <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------
// ֻ<>ڵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD> init(...) <20><>ʼ<EFBFBD><CABC> TeeStreamBuf<75><66>
@@ -15481,24 +15482,45 @@ void redirectNormalOutput(bool enabled)
}
}
// ------------------ <20><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>debug <20>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------
// <20><><EFBFBD><EFBFBD>û<EFBFBD>б<EFBFBD>׼<EFBFBD>ġ<EFBFBD>debug<75><67><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD> debugList<73><74>
// <20><><EFBFBD><EFBFBD> g_debugTeeBuf <20>Ƿ<EFBFBD>ҲҪ<D2B2><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD>(<28><>ѡ)
void redirectDebugOutput(bool enabled)
// ------------------ <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------
void redirectDebugOutput(bool enable)
{
debugOutputEnabled = enabled;
if (enabled) {
// <20><><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD> debug ͬʱҲ<CAB1><D2B2>ʾ<EFBFBD><CABE><EFBFBD>նˣ<D5B6><CBA3><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> std::clog.rdbuf() <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>:
g_debugTeeBuf.init(std::clog.rdbuf(), LOGDEBUG);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1> NULL <20><> <20><><EFBFBD><EFBFBD><EFBFBD>ն<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debugList.
//g_debugTeeBuf.init(NULL, LOGDEBUG);
} else {
// <20>ر<EFBFBD>debug<EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>д debugList(<28><>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD>д buffer, <20><> push_backʱ<6B><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
// <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ָ<EFBFBD>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊû<CEAA>и<EFBFBD>std::ostream<61><6D>ԭ<EFBFBD><D4AD>
g_debugTeeBuf.init(NULL, LOGDEBUG);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>init, <20><> debugOutputEnabled=false <EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>
debugOutputEnabled = enable;
// <20><>ȥ<EFBFBD><C8A5> clog.rdbuf()
// <20><>ȥ install <20>κ<EFBFBD> msg handler
}
// ------------------ Qt4 <20><>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------
void myQtMsgHandler(QtMsgType type, const char *msg)
{
// <20><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Σ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD> debugList
if (debugOutputEnabled) {
pthread_mutex_lock(&debugListMutex);
debugList.push_back(msg); // <20><> const char* ת<><D7AA>Ϊ std::string <20>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
pthread_mutex_unlock(&debugListMutex);
}
// ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD> stderr<72><72><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1>
const char* typeStr = "";
switch (type) {
case QtDebugMsg:
typeStr = "Debug";
break;
case QtWarningMsg:
typeStr = "Warning";
break;
case QtCriticalMsg:
typeStr = "Critical";
break;
case QtFatalMsg:
typeStr = "Fatal";
break;
}
fprintf(stderr, "[%s] %s\n", typeStr, msg);
fflush(stderr);
if (type == QtFatalMsg)
abort();
}
// ------------------ <20>Զ<EFBFBD><D4B6><EFBFBD> printf <20><><EFBFBD><EFBFBD> ------------------
@@ -15530,7 +15552,9 @@ void real_echo_msg(const char *format, ...) {
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
printf("%s", buffer); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>־
printf("%s", buffer);
fflush(stdout); // ? ȷ<><C8B7><EFBFBD><EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˢ<EFBFBD><CBA2>
}
///////////////////////////////////////////////////////////////////////////////

View File

@@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdarg.h>
#include <QtGlobal> // ✅ 确保 QtMsgType 被正确包含
#ifdef __cplusplus
#include <list>
@@ -24,7 +24,12 @@ extern bool debugOutputEnabled;
void redirectErrorOutput(bool enabled);
void redirectWarnOutput(bool enabled);
void redirectNormalOutput(bool enabled);
void redirectDebugOutput(bool enabled);
// ------------------ 重定向函数 ------------------
void redirectDebugOutput(bool enable);
// ------------------ Qt 的消息处理函数 (Qt4) ------------------
// Qt4 使用 qInstallMsgHandler(...),签名: void myMsgHandler(QtMsgType, const char*)
void myQtMsgHandler(QtMsgType type, const char* msg);
extern pthread_mutex_t errorListMutex;
extern pthread_mutex_t warnListMutex;
@@ -37,11 +42,19 @@ void real_echo_msg(const char *format, ...);
// **拦截所有 `echo_msgX()`,让它同时存入 `normalList`**
#define echo_msgX(format, ...) \
do { \
real_echo_msg(format, ##__VA_ARGS__); /* 调用原始 `echo_msgX()` */ \
char buffer[1024]; \
snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
\
/* ✅ 先打印到 Shell确保不会卡死 */ \
printf("%s", buffer); \
fflush(stdout); /* ✅ 立即刷新,防止标准输出缓存 */ \
\
/* ✅ 先创建日志副本,避免锁住 `normalList` 过久 */ \
std::string logEntry(buffer); \
\
/* ✅ 仅在 `normalList` 操作时加锁,避免死锁 */ \
pthread_mutex_lock(&normalListMutex); \
normalList.push_back(buffer); \
normalList.push_back(logEntry); \
pthread_mutex_unlock(&normalListMutex); \
} while (0)

View File

@@ -615,7 +615,7 @@ void KafkaSendThread::run()
debugList.pop_front(); // ֻ<>зǿհ<C7BF><D5B0>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ż<EFBFBD><C5BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
}
pthread_mutex_unlock(&errorListMutex);
pthread_mutex_unlock(&debugListMutex);
}
else if (normalOutputEnabled) {
// <20><><EFBFBD><EFBFBD> normalOutputEnabled Ϊ 1<><31><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD> normalList <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
@@ -1538,42 +1538,32 @@ void parse_log(const std::string& json_str) {
else if (level == "WARN"){
// <20><><EFBFBD>ø澯<C3B8><E6BEAF><EFBFBD><EFBFBD>
redirectWarnOutput(true);
redirectErrorOutput(true);
}
else if (level == "NORMAL"){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
redirectNormalOutput(true);
redirectErrorOutput(true);
redirectWarnOutput(true);
}
else if (level == "DEBUG"){
// <20><><EFBFBD><EFBFBD> debug (ֻ<>浽 debugList<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD>ն<EFBFBD>)
redirectDebugOutput(true);
redirectErrorOutput(true);
redirectWarnOutput(true);
redirectNormalOutput(true);
}
else{
std::cout << "level error" <<std::endl;
}
}
else{
if (level == "ERROR"){
// <20>رմ<D8B1><D5B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
redirectErrorOutput(false);
}
else if (level == "WARN"){
// <20><><EFBFBD>ø澯<C3B8><E6BEAF><EFBFBD><EFBFBD>
redirectWarnOutput(false);
}
else if (level == "NORMAL"){
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
redirectNormalOutput(false);
}
else if (level == "DEBUG"){
redirectDebugOutput(false);
}
else{
redirectDebugOutput(false);
redirectErrorOutput(false);
redirectWarnOutput(false);
redirectNormalOutput(false);
std::cout << "close all log" <<std::endl;
}
redirectDebugOutput(false);
redirectErrorOutput(false);
redirectWarnOutput(false);
redirectNormalOutput(false);
std::cout << "close all log" <<std::endl;
}
}
else{
@@ -2843,6 +2833,9 @@ int try_start_mqtest_thread(int argc, char *argv[])
mqtestThrd.start();
mqtest_thread_created = 1;
}*/
//<2F><>װqt<71><74>ӡ
qInstallMsgHandler(myQtMsgHandler);
QCoreApplication a(argc, argv);
// <20><><EFBFBD><EFBFBD> QThread <20><> Worker <20><><EFBFBD><EFBFBD>

View File

@@ -140,6 +140,8 @@ public slots:
return; // <20><>ֹ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
// <20><><EFBFBD><EFBFBD> QTcpServer <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><C5BA><EFBFBD><EFBFBD><EFBFBD>
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
@@ -241,7 +243,7 @@ private slots:
std::cout << "Received 'q' from shell socket! Exiting viewlog...\n";
if (activeClient == clientSocket) {
stopViewLog = true; // ? <20><> `viewlog` <20>˳<EFBFBD>
showinshellflag = false;
clientSocket->write("\nLog view stopped. Returning to shell.\n> ");
clientSocket->flush();
}
@@ -334,7 +336,7 @@ private:
helpText += "log - Execute rocketmq_test_log\n";
helpText += "ledger <id> - Execute ledger with optional terminal_id\n";
helpText += "viewlog <level> - View logs (ERROR, WARN, NORMAL, DEBUG)\n";
helpText += "value <valuename> - Execute value print with valuename : iedcount frontfun frontindex remtable\n";
helpText += "value <valuename> - Execute value print with valuename : iedcount frontfun frontindex remtable log\n";
helpText += "exit - Exit the shell\n";
helpText += "help - Show this help message\n";
clientSocket->write(helpText.toUtf8());