Overte C++ Documentation
LogHandler.h
1 //
2 // LogHandler.cpp
3 // libraries/shared/src
4 //
5 // Created by Stephen Birarda on 2014-10-28.
6 // Migrated from Logging.cpp created on 6/11/13
7 // Copyright 2014 High Fidelity, Inc.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_LogHandler_h
14 #define hifi_LogHandler_h
15 
16 #include <QObject>
17 #include <QString>
18 #include <QRegExp>
19 #include <QRecursiveMutex>
20 #include <vector>
21 #include <memory>
22 
23 const int VERBOSE_LOG_INTERVAL_SECONDS = 5;
24 
25 enum LogMsgType {
26  LogInfo = QtInfoMsg,
27  LogDebug = QtDebugMsg,
28  LogWarning = QtWarningMsg,
29  LogCritical = QtCriticalMsg,
30  LogFatal = QtFatalMsg,
31  LogSuppressed = 100
32 };
33 
35 
40 class LogHandler : public QObject {
41  Q_OBJECT
42 public:
48  static LogHandler& getInstance();
49 
60  bool parseOptions(const QString& options, const QString &paramName);
61 
70  void setTargetName(const QString& targetName);
71 
79  void setShouldOutputProcessID(bool shouldOutputProcessID);
80 
86  void setShouldOutputThreadID(bool shouldOutputThreadID);
87 
93  void setShouldDisplayMilliseconds(bool shouldDisplayMilliseconds);
94 
100  void setShouldUseJournald(bool shouldUseJournald);
101 
110  bool isJournaldAvailable() const;
111 
122  QString printMessage(LogMsgType type, const QMessageLogContext& context, const QString &message);
123 
133  static void verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString &message);
134 
135  int newRepeatedMessageID();
136  void printRepeatedMessage(int messageID, LogMsgType type, const QMessageLogContext& context, const QString &message);
137 
138  void setupRepeatedMessageFlusher();
139 
160  static void breakOnMessage(const char *str);
161 private:
162  LogHandler();
163  ~LogHandler() = default;
164 
165  void flushRepeatedMessages();
166 
167  QString _targetName;
168  bool _shouldOutputProcessID { false };
169  bool _shouldOutputThreadID { false };
170  bool _shouldDisplayMilliseconds { false };
171  bool _useColor { false };
172  bool _keepRepeats { false };
173  bool _useJournald { false };
174 
175  QString _previousMessage;
176  int _repeatCount { 0 };
177 
178 
179  int _currentMessageID { 0 };
180  struct RepeatedMessageRecord {
181  int repeatCount;
182  QString repeatString;
183  };
184  std::vector<RepeatedMessageRecord> _repeatedMessageRecords;
185 
186  QStringList _breakMessages;
187  static QRecursiveMutex _mutex;
188 };
189 
190 #define HIFI_FCDEBUG(category, message) \
191  do { \
192  if (category.isDebugEnabled()) { \
193  static int repeatedMessageID_ = LogHandler::getInstance().newRepeatedMessageID(); \
194  QString logString_; \
195  QDebug debugStringReceiver_(&logString_); \
196  debugStringReceiver_ << message; \
197  LogHandler::getInstance().printRepeatedMessage(repeatedMessageID_, LogDebug, QMessageLogContext(__FILE__, \
198  __LINE__, __func__, category().categoryName()), logString_); \
199  } \
200  } while (false)
201 
202 #define HIFI_FDEBUG(message) HIFI_FCDEBUG((*QLoggingCategory::defaultCategory()), message)
203 
204 #define HIFI_FCDEBUG_ID(category, messageID, message) \
205  do { \
206  if (category.isDebugEnabled()) { \
207  QString logString_; \
208  QDebug debugStringReceiver_(&logString_); \
209  debugStringReceiver_ << message; \
210  LogHandler::getInstance().printRepeatedMessage(messageID, LogDebug, QMessageLogContext(__FILE__, \
211  __LINE__, __func__, category().categoryName()), logString_); \
212  } \
213  } while (false)
214 
215 #define HIFI_FDEBUG_ID(messageID, message) HIFI_FCDEBUG_ID((*QLoggingCategory::defaultCategory()), messageID, message)
216 
217 #endif // hifi_LogHandler_h
Handles custom message handling and sending of stats/logs to Logstash instance.
Definition: LogHandler.h:40
void setShouldDisplayMilliseconds(bool shouldDisplayMilliseconds)
Set whether to display timestamps with milliseconds.
Definition: LogHandler.cpp:171
QString printMessage(LogMsgType type, const QMessageLogContext &context, const QString &message)
Process a log message.
Definition: LogHandler.cpp:211
static LogHandler & getInstance()
Returns the one instance of the LogHandler object.
Definition: LogHandler.cpp:42
bool isJournaldAvailable() const
Whether Journald is available on this version/system.
Definition: LogHandler.cpp:187
bool parseOptions(const QString &options, const QString &paramName)
Parse logging options.
Definition: LogHandler.cpp:124
void setShouldOutputProcessID(bool shouldOutputProcessID)
Set whether to output the process ID.
Definition: LogHandler.cpp:161
void setShouldUseJournald(bool shouldUseJournald)
Set whether to use Journald, if it's available.
Definition: LogHandler.cpp:176
static void breakOnMessage(const char *str)
Break when a message that contains the specified string is logged.
Definition: LogHandler.cpp:386
static void verboseMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
A qtMessageHandler that can be hooked up to a target that links to Qt.
Definition: LogHandler.cpp:346
void setTargetName(const QString &targetName)
Set the name of the component that's producing log output.
Definition: LogHandler.cpp:156
void setShouldOutputThreadID(bool shouldOutputThreadID)
Set whether to output the thread ID.
Definition: LogHandler.cpp:166