TTKMusicPlayer  4.1.3.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 {
17 public:
21  void install();
25  void uninstall();
26 
30 #if TTK_QT_VERSION_CHECK(5,0,0)
31  static void loggerHandler(QtMsgType type, const QMessageLogContext &context, const QString &message);
32 #else
33  static void loggerHandler(QtMsgType type, const char *message);
34 #endif
35 
36 private:
40  TTKLogOutput();
44  ~TTKLogOutput();
45 
49  void open();
53  void save(const QString &message);
57  void write(QtMsgType type, const QMessageLogContext &context, const QString &message);
58 
59 private:
60  QFile m_file;
61  QString m_dateTime;
62  QMutex m_mutex;
64 
66 };
67 
68 #define LOG_DIR_PATH QApplication::applicationDirPath() + "/log/"
69 
71  : m_file(),
72  m_dateTime(),
73  m_mutex(),
74  m_defaultHandler(nullptr)
75 {
76 
77 }
78 
80 {
81  if(m_file.isOpen())
82  {
83  m_file.close();
84  }
85 }
86 
88 {
89  const QString &path = LOG_DIR_PATH;
90  QDir dir(path);
91  if(!dir.exists())
92  {
93  dir.mkdir(path);
94  }
95 
96  open();
98 }
99 
101 {
102  m_defaultHandler = nullptr;
104 }
105 
106 #if TTK_QT_VERSION_CHECK(5,0,0)
107 void TTKLogOutput::loggerHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
108 {
109  TTKSingleton<TTKLogOutput>::instance()->write(type, context, message);
110 }
111 #else
112 void TTKLogOutput::loggerHandler(QtMsgType type, const char *message)
113 {
114  TTKSingleton<TTKLogOutput>::instance()->write(type, {}, message);
115 }
116 #endif
117 
119 {
120  m_dateTime = QDate::currentDate().toString(TTK_DATE_FORMAT);
121  const QString &fileName = LOG_DIR_PATH + m_dateTime;
122 
123  int index = 1;
124  do
125  {
126  m_file.setFileName(fileName + QString("_%1.log").arg(index++));
127  }
128  while(m_file.size() >= LOG_MAXSIZE);
129 
130  m_file.open(QIODevice::WriteOnly | QIODevice::Append);
131 }
132 
133 void TTKLogOutput::save(const QString &message)
134 {
135  if(m_file.isOpen())
136  {
137  QTextStream out(&m_file);
138  out << message << TTK_WLINEFEED;
139  m_file.flush();
140  }
141 }
142 
143 void TTKLogOutput::write(QtMsgType type, const QMessageLogContext &context, const QString &message)
144 {
145  Q_UNUSED(type);
146  Q_UNUSED(context);
147 
148  m_mutex.lock();
149 
150 #if TTK_QT_VERSION_CHECK(5,0,0)
151  m_defaultHandler(type, context, message);
152 #else
153  m_defaultHandler(type, message.toUtf8().constData());
154 #endif
155 
156  if(m_file.isOpen())
157  {
158  const QString &date = QDate::currentDate().toString(TTK_DATE_FORMAT);
159  const bool moreLarge = m_file.size() >= LOG_MAXSIZE;
160  const bool nextDate = date.compare(m_dateTime, Qt::CaseInsensitive) != 0;
161 
162  if(moreLarge || nextDate)
163  {
164  m_file.close();
165  open();
166  }
167  }
168 
169  save(message);
170  m_mutex.unlock();
171 }
172 
173 
175 {
177 }
178 
180 {
182 }
#define qInstallMessageHandler
void save(const QString &message)
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:235
#define TTK_WLINEFEED
Definition: ttkglobal.h:199
QtMessageHandler m_defaultHandler
static void loggerHandler(QtMsgType type, const char *message)
QString m_dateTime
#define LOG_DIR_PATH