文章编目请求Qt调试信息默认环境下输出的调试信息安装自定义消息处理程序自定义输出格式写入超级终端字体背景和颜色显示文件多线程log日志开源log库
需求GUI log部分注册GUI log写入文件GUI log的文件大小被限制为每1MGUI log个文件。 回滚覆盖始终填写写入GUI log的最新日志xxx.0.log,按xxx.1.log、xxx.2.log的顺序填写。 Qt调试信息qdebug(//调试消息qInfo )//信息消息qWarning )//警告消息qCritical )//错误消息qFatal )//致命错误消息在默认环境下输出q调试(thisisadebugmessage ); qinfo(thisisainfomessage ); q警告(thisisawarningmessage ); 临界消息(qcritical ); q故障(thisisafatalmessage ); return a.exec (; }
输出结果如下。
this is a debug消息
this is a debug消息
This is a info message
This is a warning message
This is a critical message
This is a fatal message
要安装定制的消息处理程序,请首先查看官方给出的示例
# include qapplication.h # include stdio.h # include stdlib.hvoidmymessageoutput (qtmsgtypetype,const qmessagelogelogcogcontext cout context.file : '; const char * function=context.function? context.function : '; switch(type ) caseqtdebugmsg:fprintf ) stderr,' debug:%s(%s:%u,%s )n ',localMsg.constData break caseqtinfomsg : fprintf (stderr,' info:%s(%s:%u,%s ) )、localMsg.constData )、file和context break caseqtwarningmsg : fprintf (stderr,' warning:%s(%s:%u,%s ) (n ),localMsg.constData ),文件,conse caseqtcriticalmsg : fprintf (stderr,' critical:%s(%s:%u,%s ) (n ),localMsg.constData ),文件,brie caseqtfatalmsg : fprintf (stderr,' fatal:%s(%s:%u,%s ) (n ),localMsg.constData ),file,content Bt }intmain(intargv,char *argv[] ) qcoreapplicationa (argc,argv ); //消息处理程序自动前端=qinstallmessagehandler (mymessageoutput ); //打印信息qDebug () ' This is a debug message ); q调试(thisisadebugmessage ); qinfo(thisisainfomessage ); q警告(thisisawarningmessage ); 临界消息(qcritical );
qFatal("This is a fatal message"); return a.exec();}输出结果Debug: This is a debug message (…main.cpp:89, int __cdecl main(int,char *[]))Info: This is a info message (…main.cpp:90, int __cdecl main(int,char *[]))Warning: This is a warning message (…main.cpp:91, int __cdecl main(int,char *[]))Critical: This is a critical message (…main.cpp:92, int __cdecl main(int,char *[]))Fatal: This is a fatal message (…main.cpp:93, int __cdecl main(int,char *[]))自定义输出格式通过上述输出结果,可以发现,通过自定义格式,可以让log按找自己想要的结果来记录,比如time, pid, tid, fun, line等信息
enum class LogType { Reset = 0, Bold, Unbold, FrontBlack, FrontRed, FrontGreen, FrontYellow, FrontBlue, FrontPurple, FrontCyan, FrontWhite, BackBlack, BackRed, BackGreen, BackYellow, BackBlue, BackPurple, BackCyan, BackWhite, TypeCount }; static const char *logCommands[] = { " 33[0m", " 33[1m", " 33[2m", " 33[30m", " 33[31m", " 33[32m", " 33[33m", " 33[34m", " 33[35m", " 33[36m", " 33[37m", " 33[40m", " 33[41m", " 33[42m", " 33[43m", " 33[44m", " 33[45m", " 33[46m", " 33[47m", };//自定义消息类型字符串static const char *msgType[] = {"Debug", "Warning", "Critical", "Fatal", "Info"};//自定义消息处理函数void customMessageHandler(QtMsgType type, const QMessageLogContext &context,const QString &msg) { const char *file = context.file ? context.file : "default"; const char *function = context.function ? context.function : "default"; if (msg.isNull() || msg.isEmpty()) { return; } //获取进程码 static auto pid = getpid(); //获取线程PID static thread_local auto tid = syscall(__NR_gettid); QString logInfo = QString::fromLocal8Bit("[%1] %2 %3 [%4] [%5] [%6] [%7] %8") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")) .arg(pid) .arg(tid) .arg(msgType[type]) .arg(function) .arg(context.line) .arg(msg.toUtf8().data()); switch (type) { case QtDebugMsg: qInfo().noquote() << logCommands[enumToInt(LogType::FrontPurple)] << logInfo << logCommands[0]; htQLog::debug(logInfo); //自定义的处理函数 break; case QtInfoMsg: qInfo().noquote() << logCommands[enumToInt(LogType::FrontGreen)] << logInfo << logCommands[0]; htQLog::info(logInfo); break; case QtWarningMsg: qInfo().noquote() << logCommands[enumToInt(LogType::FrontYellow)] << logInfo << logCommands[0]; htQLog::warning(logInfo); break; case QtCriticalMsg: qInfo().noquote() << logCommands[enumToInt(LogType::FrontRed)] << logCommands[enumToInt(LogType::Bold)] << logInfo << logCommands[0]; htQLog::critical(logInfo); break; case QtFatalMsg: qInfo().noquote() << logCommands[enumToInt(LogType::FrontRed)] << logCommands[enumToInt(LogType::Bold)] << logInfo << logCommands[0]; htQLog::fatal(logInfo); break; default: break; } }超级终端的字体背景和颜色显示# man console_codes //查看支持哪些颜色printf(“ 33[0;30;41m color!!! 33[0m Hello n”);其中41的位置代表字的背景色, 30的位置是代表字的颜色,0 是字的一些特殊属性,0代表默认关闭,一些其他属性如闪烁、下划线等。ascii code 是对颜色进行调用的。