首页 > 编程知识 正文

qt中qstring转int,qt设置

时间:2023-05-06 07:40:04 阅读:164729 作者:1953

文章编目请求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 是对颜色进行调用的。
33[ ; m …… 33[0m
  颜色的控制通过ESC字符(33)加”[“加颜色代码加”m”实现。ESC的ASCII码是十进制的27,八进制的033(33)

在 ANSI 兼容终端里,可以用彩色显示文本而不仅仅是黑白。但是我们自己编写的程序能否输出彩色的字符呢?当然答案是肯定的。下面的语句就输出高亮的黑色背景的绿色字。
33 声明了转义序列的开始,然后是 [ 开始定义颜色。后面的 1 定义了高亮显示字符。然后是背景颜色,这里面是40,表示黑色背景。接着是前景颜色,这里面是32,表示绿色。我们用 33[0m 关闭转义序列, 33[0m 是终端默认颜色。通过上面的介绍,就知道了如何输出彩色字符了。

printf(“33[1;40;32m color!!! 33[0m hellon”);

————————————————
原文链接:https://blog.csdn.net/u014470361/article/details/81512330

写入文件

自定义写入文件接口,将log保存至文件。

htQLog::debug(logInfo); htQLog::info(logInfo);htQLog::warning(logInfo);htQLog::critical(logInfo);htQLog::fatal(logInfo); QFile file("log.txt"); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream text_stream(&file); text_stream << message << "rn"; file.flush(); file.close();多线程log日志多线程下I/O操作枷锁void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg){ // 加锁 static QMutex mutex; mutex.lock();/** log 组包*/ ... // 解锁 mutex.unlock();}开源log库

上述写法适用与小型项目,对实时性要求不高。 但是读写文件和获取时间其实是耗时的,再加上锁后,可能会都系统性能要求高的项目有影响。

log4qt、spdlog、log4cpp

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。