QT单例多文件日志类

  1 #ifndef TLOGGER_H
  2 #define TLOGGER_H
  3 
  4 #include <QDebug>
  5 #include <QFile>
  6 #include <QMutexLocker>
  7 
  8 
  9 #define TLOG_OPEN(x,y) TLogger::getInstance()->openLog(x,y);
 10 #define TLOG_PRINT(y,z) TLogger::getInstance()->printLog(y,z);
 11 
 12 class TLogger
 13 {
 14 private:
 15     TLogger()
 16     {
 17         qDebug() << "Singleton construction";
 18     }
 19 
 20     TLogger(const TLogger&);
 21     TLogger& operator = (const TLogger&);
 22 
 23     class TGC
 24     {
 25     public:
 26         TGC()
 27         {
 28             qDebug() << "TGC construction";
 29         }
 30         ~TGC()
 31         {
 32             qDebug() << "TGC destruction, m_pInstance=" << m_pInstance;
 33             if(m_pInstance)
 34             {
 35                 m_pInstance->clearFile();
 36 
 37                 delete m_pInstance;
 38                 m_pInstance = 0;
 39                 qDebug() << "TLogger destruction";
 40             }
 41             qDebug() << "TGC destruction, m_pInstance=" << m_pInstance;
 42         }
 43     };
 44 
 45     static TLogger* m_pInstance;
 46     static TGC      m_garbage_collection;
 47     static QMutex   m_mutex;
 48     QMap<QString, QFile*>   m_files;
 49 public:
 50     static TLogger* getInstance()
 51     {
 52         qDebug() << "m_pInstance =" << m_pInstance;
 53         if(0==m_pInstance)
 54         {
 55             QMutexLocker lock(&m_mutex);
 56             m_pInstance = new TLogger();
 57         }
 58         qDebug() << "m_pInstance =" << m_pInstance;
 59 
 60         return m_pInstance;
 61     }
 62 
 63     void openLog(const QString& path, const QString& alias)
 64     {
 65         if(!m_files.contains(alias))
 66         {
 67             QFile* file = new QFile(path);
 68             if(file->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
 69             {
 70                 m_files.insert(alias, file);
 71             }
 72         }
 73     }
 74 
 75     void printLog(const QString& alias, const QString& data)
 76     {
 77         if(m_files.contains(alias))
 78         {
 79             QMutexLocker lock(&m_mutex);
 80             QFile* file = m_files[alias];
 81             if(file && file->isOpen())
 82             {
 83                 QTextStream out(file);
 84                 out << data << endl;
 85             }
 86         }
 87     }
 88 
 89     void clearFile()
 90     {
 91         for(QFile* file : m_files.values())
 92         {
 93             if(file)
 94             {
 95                 file->close();
 96                 delete file;
 97             }
 98         }
 99     }
100 };
101 
102 #include <QThread>
103 class QThreadLog : public QThread
104 {
105 protected:
106     void run()
107     {
108         for(int i=0;i<10000;i++)
109         {
110             QString id = QString::number((quint64)currentThreadId());
111             QString file = QString("d:/11__%1.txt").arg(id);
112             TLOG_OPEN(file, id);
113             TLOG_PRINT(id, "abcdefghijklmnopqrstuvwxyz"+id);
114             usleep(10);
115         }
116     }
117 };
118 
119 class QLogTest
120 {
121 public:
122     void forTest()
123     {
124         for(int i=0;i<10;i++)
125         {
126             QThreadLog* t = new QThreadLog();
127             if(t)
128             {
129                 t->start();
130             }
131         }
132     }
133 };
134 
135 #endif // TLOGGER_H
1 #include "tlogger.h"
2 
3 TLogger* TLogger::m_pInstance = 0;
4 TLogger::TGC TLogger::m_garbage_collection;
5 QMutex TLogger::m_mutex;
原文地址:https://www.cnblogs.com/jobgeo/p/6868230.html