add echo to testshell and modify mqsend
This commit is contained in:
@@ -11833,6 +11833,79 @@ void value_print(const char *variableName, QTcpSocket *clientSocket) {
|
||||
clientSocket->flush();
|
||||
}
|
||||
|
||||
std::list<std::string>* getLogList(const QString& level) {
|
||||
if (level == "ERROR") return &errorList;
|
||||
if (level == "WARN") return &warnList;
|
||||
if (level == "NORMAL") return &normalList;
|
||||
if (level == "DEBUG") return &debugList;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pthread_mutex_t* getLogMutex(const QString& level) {
|
||||
if (level == "ERROR") return &errorListMutex;
|
||||
if (level == "WARN") return &warnListMutex;
|
||||
if (level == "NORMAL") return &normalListMutex;
|
||||
if (level == "DEBUG") return &debugListMutex;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Worker::handleViewLogCommand(const QString& command, QTcpSocket* clientSocket) {
|
||||
QStringList parts = command.split(" ");
|
||||
if (parts.size() != 2) {
|
||||
clientSocket->write("Usage: viewlog [ERROR|WARN|NORMAL|DEBUG]\n> ");
|
||||
clientSocket->flush();
|
||||
return;
|
||||
}
|
||||
|
||||
QString logLevel = parts[1].toUpper();
|
||||
std::list<std::string>* logList = getLogList(logLevel);
|
||||
pthread_mutex_t* logMutex = getLogMutex(logLevel);
|
||||
|
||||
if (!logList || !logMutex) {
|
||||
clientSocket->write("Invalid log level! Use ERROR, WARN, NORMAL, or DEBUG.\n> ");
|
||||
clientSocket->flush();
|
||||
return;
|
||||
}
|
||||
|
||||
stopViewLog = false;
|
||||
activeClient = clientSocket; // <20><>¼<EFBFBD><C2BC>ǰ shell socket
|
||||
|
||||
clientSocket->write(QString("Viewing logs for level: %1 (Press 'q' to exit)\n> ").arg(logLevel).toUtf8());
|
||||
clientSocket->flush();
|
||||
|
||||
while (!stopViewLog) {
|
||||
// **1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>룬<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD> `q` <20>˳<EFBFBD>**
|
||||
if (clientSocket->waitForReadyRead(500)) { // ? <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
QByteArray input = clientSocket->readAll().trimmed();
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// **2. <20><>ȡ<EFBFBD><C8A1>־<EFBFBD><D6BE><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD><EFBFBD>**
|
||||
pthread_mutex_lock(logMutex);
|
||||
if (!logList->empty()) {
|
||||
std::string logEntry = logList->front();
|
||||
logList->pop_front();
|
||||
pthread_mutex_unlock(logMutex);
|
||||
|
||||
if (!logEntry.empty()) {
|
||||
clientSocket->write((logEntry + "\n").c_str());
|
||||
clientSocket->flush();
|
||||
}
|
||||
} else {
|
||||
pthread_mutex_unlock(logMutex);
|
||||
usleep(500000); // ? <20><>ֹ CPU 100% ռ<><D5BC>
|
||||
}
|
||||
}
|
||||
|
||||
// **3. <20>˳<EFBFBD> `viewlog`<60><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Shell**
|
||||
clientSocket->write("\nLog view stopped. Returning to shell.\n> ");
|
||||
clientSocket->flush();
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// <20><><EFBFBD><EFBFBD> JSON <20>ĺ<EFBFBD><C4BA><EFBFBD> <20><>ǰ<EFBFBD>ö<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>
|
||||
int terminal_ledger_web(QMap<QString, terminal_dev*>* terminal_dev_map,
|
||||
@@ -15170,15 +15243,18 @@ void rocketmq_test_300(int mpnum,int front_index) {
|
||||
std::list<std::string> errorList;
|
||||
std::list<std::string> warnList;
|
||||
std::list<std::string> normalList;
|
||||
std::list<std::string> debugList; // <20><><EFBFBD><EFBFBD> debugList
|
||||
|
||||
pthread_mutex_t errorListMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t warnListMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t normalListMutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
pthread_mutex_t debugListMutex = PTHREAD_MUTEX_INITIALIZER; // <20><><EFBFBD><EFBFBD> debugList <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
// ------------------ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------
|
||||
bool errorOutputEnabled = false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD> error <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4> errorList
|
||||
bool warnOutputEnabled = false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD> warn <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4> warnList
|
||||
bool normalOutputEnabled = false; // <20>Ƿ<EFBFBD><C7B7><EFBFBD> normal <20><><EFBFBD><EFBFBD>д<EFBFBD><D0B4> normalList
|
||||
bool debugOutputEnabled = false; // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD>
|
||||
|
||||
// ------------------ <20><><EFBFBD>ڻָ<DABB>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------
|
||||
static std::streambuf* g_originalCoutBuf = NULL;
|
||||
@@ -15187,9 +15263,10 @@ static std::streambuf* g_originalCerrBuf = NULL;
|
||||
|
||||
// ------------------ <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>ö<EFBFBD>٣<EFBFBD>C++98<39><38> ------------------
|
||||
enum LogLevel {
|
||||
LOG_ERROR,
|
||||
LOG_WARN,
|
||||
LOG_NORMAL
|
||||
LOGERROR,
|
||||
LOGWARN,
|
||||
LOGNORMAL,
|
||||
LOGDEBUG // <20><><EFBFBD><EFBFBD> debug <20><><EFBFBD><EFBFBD>
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
@@ -15200,7 +15277,7 @@ class TeeStreamBuf : public std::streambuf
|
||||
public:
|
||||
// Ĭ<>Ϲ<EFBFBD><CFB9>죺<EFBFBD>Ȱ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ΪNULL
|
||||
TeeStreamBuf()
|
||||
: m_originalBuf(NULL), m_level(LOG_NORMAL)
|
||||
: m_originalBuf(NULL), m_level(LOGNORMAL)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -15263,7 +15340,7 @@ private:
|
||||
// <20><><EFBFBD>ݵȼ<DDB5> + <20><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD> <20><> д<>ļ<EFBFBD><C4BC><EFBFBD>list
|
||||
switch (m_level) {
|
||||
|
||||
case LOG_ERROR:
|
||||
case LOGERROR:
|
||||
if (errorOutputEnabled) {
|
||||
pthread_mutex_lock(&errorListMutex);
|
||||
errorList.push_back(m_buffer);
|
||||
@@ -15279,9 +15356,14 @@ private:
|
||||
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 LOG_WARN:
|
||||
case LOGWARN:
|
||||
if (warnOutputEnabled) {
|
||||
pthread_mutex_lock(&warnListMutex);
|
||||
warnList.push_back(m_buffer);
|
||||
@@ -15292,22 +15374,41 @@ private:
|
||||
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 LOG_NORMAL:
|
||||
case LOGNORMAL:
|
||||
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;
|
||||
}
|
||||
|
||||
m_buffer.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
// <20><>ֹ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ɵĸ<C9B5>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD> (C++98<39><38><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
||||
// <20><>ֹ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ɵĸ<C9B5>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
|
||||
TeeStreamBuf& operator=(const TeeStreamBuf&); // <20><>ʵ<EFBFBD><CAB5>
|
||||
|
||||
private:
|
||||
@@ -15320,6 +15421,14 @@ 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);
|
||||
|
||||
// <20><> qDebug() ӳ<>䵽<EFBFBD><E4B5BD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>
|
||||
#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>
|
||||
@@ -15330,7 +15439,7 @@ void redirectErrorOutput(bool enabled)
|
||||
if (enabled) {
|
||||
if (g_originalCerrBuf == NULL) {
|
||||
g_originalCerrBuf = std::cerr.rdbuf();
|
||||
g_errorTeeBuf.init(g_originalCerrBuf, LOG_ERROR);
|
||||
g_errorTeeBuf.init(g_originalCerrBuf, LOGERROR);
|
||||
}
|
||||
std::cerr.rdbuf(&g_errorTeeBuf);
|
||||
} else {
|
||||
@@ -15346,7 +15455,7 @@ void redirectWarnOutput(bool enabled)
|
||||
if (enabled) {
|
||||
if (g_originalClogBuf == NULL) {
|
||||
g_originalClogBuf = std::clog.rdbuf();
|
||||
g_warnTeeBuf.init(g_originalClogBuf, LOG_WARN);
|
||||
g_warnTeeBuf.init(g_originalClogBuf, LOGWARN);
|
||||
}
|
||||
std::clog.rdbuf(&g_warnTeeBuf);
|
||||
} else {
|
||||
@@ -15362,7 +15471,7 @@ void redirectNormalOutput(bool enabled)
|
||||
if (enabled) {
|
||||
if (g_originalCoutBuf == NULL) {
|
||||
g_originalCoutBuf = std::cout.rdbuf();
|
||||
g_normalTeeBuf.init(g_originalCoutBuf, LOG_NORMAL);
|
||||
g_normalTeeBuf.init(g_originalCoutBuf, LOGNORMAL);
|
||||
}
|
||||
std::cout.rdbuf(&g_normalTeeBuf);
|
||||
} else {
|
||||
@@ -15372,6 +15481,26 @@ void redirectNormalOutput(bool enabled)
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>debug <20>ض<EFBFBD><D8B6><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)
|
||||
{
|
||||
debugOutputEnabled = enabled;
|
||||
if (enabled) {
|
||||
// <20><><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3> 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><D5B6><EFBFBD><EFBFBD><EFBFBD>, ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD> debugList.
|
||||
//g_debugTeeBuf.init(NULL, LOGDEBUG);
|
||||
} else {
|
||||
// <20>ر<EFBFBD>debug<75><67> <20><><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 <20><><EFBFBD>ɡ<EFBFBD>
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------ <20>Զ<EFBFBD><D4B6><EFBFBD> printf <20><><EFBFBD><EFBFBD> ------------------
|
||||
// <20><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> normal <20><><EFBFBD><EFBFBD> => <20><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>normalList(<28><><EFBFBD><EFBFBD><EFBFBD>ؿ<EFBFBD>)
|
||||
int customPrintf(const char* format, ...)
|
||||
@@ -15394,6 +15523,15 @@ int customPrintf(const char* format, ...)
|
||||
return written;
|
||||
}
|
||||
|
||||
// **ԭʼ `echo_msgX()` <20><>ʵ<EFBFBD><CAB5>**
|
||||
void real_echo_msg(const char *format, ...) {
|
||||
char buffer[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buffer, sizeof(buffer), format, args);
|
||||
va_end(args);
|
||||
printf("%s", buffer); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>־
|
||||
}
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
@@ -13,16 +13,51 @@
|
||||
extern std::list<std::string> errorList;
|
||||
extern std::list<std::string> warnList;
|
||||
extern std::list<std::string> normalList;
|
||||
extern std::list<std::string> debugList;
|
||||
|
||||
// 开关
|
||||
extern bool errorOutputEnabled;
|
||||
extern bool warnOutputEnabled;
|
||||
extern bool normalOutputEnabled;
|
||||
extern bool debugOutputEnabled;
|
||||
|
||||
void redirectErrorOutput(bool enabled);
|
||||
void redirectWarnOutput(bool enabled);
|
||||
void redirectNormalOutput(bool enabled);
|
||||
void redirectDebugOutput(bool enabled);
|
||||
|
||||
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 { \
|
||||
real_echo_msg(format, ##__VA_ARGS__); /* 调用原始 `echo_msgX()` */ \
|
||||
char buffer[1024]; \
|
||||
snprintf(buffer, sizeof(buffer), format, ##__VA_ARGS__); \
|
||||
pthread_mutex_lock(&normalListMutex); \
|
||||
normalList.push_back(buffer); \
|
||||
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()`,继续定义**
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user