我有一个简单的调试打印控制台,可以打印几个调试消息,如错误,警告,信息等。到目前为止,我使用简单的消息,最坏的情况只有一个参数。 但是当我尝试使用多个参数时,它不起作用,它为字符串提供null,为数字提供一些垃圾。
enum MsgLevel {eINFO, eWARNING, eERROR, eDEBUG, eTRACE};
#define PRINT_ERROR(...) logMsg(eERROR, __VA_ARGS__)
#define PRINT_WARNING(...) logMsg(eWARNING, __VA_ARGS__)
#define PRINT_INFO(...) logMsg(eINFO, __VA_ARGS__)
static const char* LogLevel[] = { "INFO : ", "WARNING : ", "ERROR : ", "DEBUG : ", "TRACE : " };
void printLogMsg(MsgLevel eLevel, const char *ptrMsg);
static const int MsgBufSize = 300;
static char MsgBuf[MsgBufSize];
void logMsg(MsgLevel eLevel, const char *format, ...){
//method-1
va_list args1;
va_start(args1, format);
va_list args2;
va_copy(args2, args1);
std::vector<char> buf(1 + std::vsnprintf(NULL, 0, format, args1));
va_end(args1);
std::vsnprintf(buf.data(), buf.size(), format, args2);
va_end(args2);
printLogMsg(eLevel,buf.data());
// Method -1
memset(MsgBuf, '\0', sizeof(MsgBuf));
va_list arglist;
va_start(arglist, format);
vsprintf(MsgBuf, format, arglist);
printLogMsg(eLevel, MsgBuf);
va_end(arglist);
}
void printLogMsg(MsgLevel eLevel, const char *format){
fprintf(stderr, "\n%s", LogLevel[eLevel]);
fprintf(stderr,format);
fflush(stderr);
}
Subscription(uint64_t SrcOpid, std::string Name, uint64_t DesIpid)
{
PRINT_INFO("Subscription Output(%u) : Name(%s) : Input(%u)",SrcOpid, Name.c_str(),DesIpid);
}