TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttklogoutput.cpp
Go to the documentation of this file.
1 #include "ttklogoutput.h"
2 #include "ttksingleton.h"
3 
4 #include <QDir>
5 #include <QApplication>
6 
7 #define LOG_MAXSIZE 5 * 1024 * 1024
8 
9 #if !TTK_QT_VERSION_CHECK(5,0,0)
11 using QtMessageHandler = QtMsgHandler;
12 #define qInstallMessageHandler qInstallMsgHandler
13 #endif
14 
16 {
18 public:
22  void initialize(const QString &module);
26  void install();
30  void uninstall();
31 
35 #if TTK_QT_VERSION_CHECK(5,0,0)
36  static void loggerHandler(QtMsgType type, const QMessageLogContext &context, const QString &message);
37 #else
38  static void loggerHandler(QtMsgType type, const char *message);
39 #endif
40 
41 private:
45  TTKLogOutput();
49  ~TTKLogOutput();
50 
54  void open();
58  void save(const QString &message);
62  void write(QtMsgType type, const QMessageLogContext &context, const QString &message);
63 
64 private:
65  QFile m_file;
66  QString m_module, m_dateTime;
67  QMutex m_mutex;
69 
70 };
71 
72 #define LOG_DIR_PATH QApplication::applicationDirPath() + "/log/"
73 
75  : m_file(),
76  m_dateTime(),
77  m_mutex(),
78  m_defaultHandler(nullptr)
79 {
80 
81 }
82 
84 {
85  if(m_file.isOpen())
86  {
87  m_file.close();
88  }
89 }
90 
91 void TTKLogOutput::initialize(const QString &module)
92 {
93  if(!module.isEmpty())
94  {
95  m_module = module + "_";
96  }
97  else
98  {
99  m_module.clear();
100  }
101 }
102 
104 {
105  const QString &path = LOG_DIR_PATH;
106  QDir dir(path);
107  if(!dir.exists())
108  {
109  dir.mkdir(path);
110  }
111 
112  open();
113 
114  if(!m_defaultHandler)
115  {
117  }
118 }
119 
121 {
122  if(m_defaultHandler)
123  {
124  m_defaultHandler = nullptr;
126 
127  m_file.close();
128  }
129 }
130 
131 #if TTK_QT_VERSION_CHECK(5,0,0)
132 void TTKLogOutput::loggerHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
133 {
134  TTKSingleton<TTKLogOutput>::instance()->write(type, context, message);
135 }
136 #else
137 void TTKLogOutput::loggerHandler(QtMsgType type, const char *message)
138 {
139  TTKSingleton<TTKLogOutput>::instance()->write(type, {}, message);
140 }
141 #endif
142 
144 {
145  m_dateTime = QDate::currentDate().toString(TTK_DATE_FORMAT);
146  const QString &fileName = LOG_DIR_PATH + m_module + m_dateTime;
147 
148  int index = 1;
149  do
150  {
151  m_file.setFileName(fileName + QString("_%1.log").arg(index++));
152  }
153  while(m_file.size() >= LOG_MAXSIZE);
154 
155  if(!m_file.open(QIODevice::WriteOnly | QIODevice::Append))
156  {
157  TTK_ERROR_STREAM("Open log file failed: " << m_file.fileName());
158  }
159 }
160 
161 void TTKLogOutput::save(const QString &message)
162 {
163  if(m_file.isOpen())
164  {
165  QTextStream out(&m_file);
166  out << message << TTK_WLINEFEED;
167  m_file.flush();
168  }
169 }
170 
171 void TTKLogOutput::write(QtMsgType type, const QMessageLogContext &context, const QString &message)
172 {
173  Q_UNUSED(type);
174  Q_UNUSED(context);
175 
176  m_mutex.lock();
177 
178 #if TTK_QT_VERSION_CHECK(5,0,0)
179  m_defaultHandler(type, context, message);
180 #else
181  m_defaultHandler(type, message.toUtf8().constData());
182 #endif
183 
184  if(m_file.isOpen())
185  {
186  const QString &date = QDate::currentDate().toString(TTK_DATE_FORMAT);
187  const bool moreLarge = m_file.size() >= LOG_MAXSIZE;
188  const bool nextDate = date.compare(m_dateTime, Qt::CaseInsensitive) != 0;
189 
190  if(moreLarge || nextDate)
191  {
192  m_file.close();
193  open();
194  }
195  }
196 
197  save(message);
198  m_mutex.unlock();
199 }
200 
201 
202 void TTK::initiailizeLog(const QString &module)
203 {
204  TTKSingleton<TTKLogOutput>::instance()->initialize(module);
205 }
206 
208 {
210 }
211 
213 {
215 }
#define qInstallMessageHandler
void save(const QString &message)
TTK_MODULE_EXPORT void initiailizeLog(const QString &module)
TTK_MODULE_EXPORT void installLogHandler()
#define LOG_MAXSIZE
Definition: ttklogoutput.cpp:7
QtMsgHandler QtMessageHandler
TTK_MODULE_EXPORT void removeLogHandler()
#define TTK_DECLARE_SINGLETON_CLASS(Class)
// Singleton Macro // //
Definition: ttksingleton.h:86
static T * instance()
Definition: ttksingleton.h:64
void write(QtMsgType type, const QMessageLogContext &context, const QString &message)
#define TTK_DATE_FORMAT
Definition: ttkglobal.h:309
void initialize(const QString &module)
#define TTK_WLINEFEED
Definition: ttkglobal.h:272
QtMessageHandler m_defaultHandler
static void loggerHandler(QtMsgType type, const char *message)
QString m_dateTime
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:76
QString m_module
#define LOG_DIR_PATH