How to use the Qt Runtime logging class
This article mainly introduces "how to use Qt runtime record class". In daily operation, I believe many people have doubts about how to use Qt runtime record class. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to use Qt runtime record class"! Next, please follow the small series to learn together!
I. Foreword
In early software development, especially software written by beginners, software running for a long time, it is inevitable to encounter unexpected crashes, but most of the running equipment may be in the field customers, you need to remember each time from the software startup to the software before the unexpected shutdown of the running time, the information to be recorded includes: Number + start time + end time + running time, only one record is generated for each complete running process, and the current record can be updated after each running time change. In this way, you can know exactly whether the real operation of the software in the field is bad. If there is no such record (of course, you can choose to store the record in the database), the program will restart and recover, and you don't know how long it runs each time, from a few points to a few points. In order to write simple points without disturbing the original database file, I generally choose to output to a text file.
Main functions:
Services can be started and stopped, starting when needed.
You can specify a directory where log files are stored.
You can specify the time log output interval.
A separate record can be appended to the log file.
The log is in text format and is clear and legible.
void SaveRunTime::getDiffValue(const QDateTime &startTime, const QDateTime &endTime, int &day, int &hour, int &minute){ qint64 sec = startTime.secsTo(endTime); day = hour = minute = 0; int seconds = 0; while (sec > 0) { seconds++; if (seconds == 60) { minute++; seconds = 0; } if (minute == 60) { hour++; minute = 0; } if (hour == 24) { day++; hour = 0; } sec--; }}void SaveRunTime::initLog(){ //Determine whether the notepad file of the current year exists, if it does not exist, create a new one and write the title. //if it exists, it automatically reads the id number of the last line of notepad file format content //Building No. start time end time elapsed time //1 2016-01-01 12:33:33 2016-02-05 12:12:12 day: 0 hour: 0 minute: 0 logFile = QString("%1/%2_runtime_%3.txt").arg(path).arg(name).arg(QDate::currentDate().year()); QFile file(logFile); if (file.size() == 0) { if (file.open(QFile::WriteOnly | QFile::Text)) { QString strID = QString("%1t").arg("Number"); QString strStartTime = QString("%1tt").arg("Start Time"); QString EndstrTime = QString("%1tt").arg("End Time"); QString strRunTime = QString("%1").arg("elapsed time"); QString line = strID + strStartTime + strEndTime + strRunTime; QTextStream stream(&file); stream