In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Qt log redirect output class how to use", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use the Qt log redirect output class.
I. Preface
Business programs have been developed with qt for ten years, and at least dozens of programs have been developed one after another. except for some gadgets that count as projects, most programs need to have a log output function, hoping to save the running state of the program to a text file or database or do other processing, etc. Qt also encapsulates this log output very well, in which Qt4 is qInstallMsgHandler,Qt5, with this artifact As long as all the qdebug qinfo output log information in your project will be redirected and received, the demo written by most people on the Internet will receive the output print log and store it in a text file. In fact, this brings misunderstanding to many people, and it is easy to think that the log can only be output to a text file. In fact, after installing the log hook, you get all the debugging and printing information. You can use it to store in the database + html files with color-differentiated format + network forwarding output (especially suitable for embedded linux interface-free programs, on-site inconvenient external debugging and printing devices). Of all the projects I have done, there are both Qt4 and Qt5. I generally keep four versions, 4.8.7. In order to be compatible with qt4, 5.7.0, the last version that supports XP, the latest long-term supported version 5.9.7, the highest new version 5.12. There is no doubt that the log class I want to encapsulate will also support 4x5 and provide a friendly interface.
Main functions:
Support for dynamic start and stop.
Directories that support log storage.
Support the network to send out and print logs.
Qt4+Qt5 is supported and can be used out of the box.
Support for multithreading.
To make it the easiest to use, start can.
2. Code idea / / Log redirection # if (QT_VERSION save (content);} QScopedPointer SaveLog::self;SaveLog * SaveLog::Instance () {if (self.isNull ()) {static QMutex mutex; QMutexLocker locker (& mutex); if (self.isNull ()) {self.reset (new SaveLog);}} return self.data () } SaveLog::SaveLog (QObject * parent): QObject (parent) {/ / must be in the form of signal slot, otherwise it is suggested that QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread / / estimates that the log hook may have opened a separate thread connect (this, SIGNAL (send (QString)), SendLog::Instance (), SLOT (send (QString)); file = new QFile (this); toNet = false / / default application root directory path = qApp- > applicationDirPath (); / / default application executable name QString str = qApp- > applicationFilePath (); QStringList list = str.split ("/"); name = list.at (list.count ()-1). Split ("."). At (0); fileName = ";} SaveLog::~SaveLog () {file- > close () } / / install log hook, output debug information to file, facilitate debugging void SaveLog::start () {# if (QT_VERSION fileName = fileName; if (file- > isOpen ()) {file- > close ();} file- > setFileName (fileName); file- > open (QIODevice::WriteOnly | QIODevice::Append | QFile::Text) } QTextStream logStream (file); logStream
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.