AusweisApp2
LogHandler.h
gehe zur Dokumentation dieser Datei
1 /*
2  * \brief Logging handler of QtMessageHandler
3  *
4  * \copyright Copyright (c) 2014-2021 Governikus GmbH & Co. KG, Germany
5  */
6 
7 #pragma once
8 
9 #include "Env.h"
10 
11 #include <QContiguousCache>
12 #include <QDateTime>
13 #include <QDebug>
14 #include <QFileInfoList>
15 #include <QLoggingCategory>
16 #include <QMessageLogContext>
17 #include <QMutex>
18 #include <QPointer>
19 #include <QStringList>
20 #include <QTemporaryFile>
21 
22 #define spawnMessageLogger(category)\
23  QMessageLogger(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC, category().categoryName())
24 
25 class test_LogHandler;
26 class test_LogModel;
27 
28 namespace governikus
29 {
30 
32  : public QObject
33 {
34  Q_OBJECT
35 
36  friend class LogHandler;
37 
38  private:
39  LogEventHandler() = default;
40  ~LogEventHandler() override = default;
41 
42  Q_SIGNALS:
47  void fireLog(const QString& pMsg);
48  void fireRawLog(const QString& pMsg, const QString& pCategoryName);
49 };
50 
52 {
53  Q_GADGET
54 
55  friend class Env;
56  friend class ::test_LogHandler;
57  friend class ::test_LogModel;
58 
59  struct LogWindowEntry
60  {
61  qint64 mPosition;
62  qint64 mLength;
63  };
64 
65  private:
66  static QString getLogFileTemplate();
67 
68  QPointer<LogEventHandler> mEventHandler;
69  const bool mEnvPattern;
70  const int mFunctionFilenameSize;
71  qint64 mBacklogPosition;
72  bool mCriticalLog;
73  QContiguousCache<LogWindowEntry> mCriticalLogWindow;
74  QStringList mCriticalLogIgnore;
75  const QString mMessagePattern, mDefaultMessagePattern;
76  QPointer<QTemporaryFile> mLogFile;
77  QtMessageHandler mHandler;
78  bool mUseHandler;
79  const QByteArray mFilePrefix;
80  QMutex mMutex;
81 
82  inline void copyMessageLogContext(const QMessageLogContext& pSource,
83  QMessageLogContext& pDestination,
84  const QByteArray& pFilename = QByteArray(),
85  const QByteArray& pFunction = QByteArray(),
86  const QByteArray& pCategory = QByteArray()) const;
87  inline void logToFile(const QString& pOutput);
88  inline QByteArray formatFunction(const char* const pFunction, const QByteArray& pFilename, int pLine) const;
89  inline QByteArray formatFilename(const char* const pFilename) const;
90  [[nodiscard]] inline QByteArray formatCategory(const QByteArray& pCategory) const;
91 
92  [[nodiscard]] QString getPaddedLogMsg(const QMessageLogContext& pContext, const QString& pMsg) const;
93  void handleMessage(QtMsgType pType, const QMessageLogContext& pContext, const QString& pMsg);
94  void handleLogWindow(QtMsgType pType, const char* pCategory, const QString& pMsg);
95  void removeOldLogfiles();
96  QByteArray readLogFile(qint64 pStart, qint64 pLength = -1);
97 
98  static void messageHandler(QtMsgType pType, const QMessageLogContext& pContext, const QString& pMsg);
99  friend QDebug operator<<(QDebug, const LogHandler&);
100 
101  protected:
103  virtual ~LogHandler();
105 
106 #ifndef QT_NO_DEBUG
107 
108  public:
109 #endif
110  void reset();
111  [[nodiscard]] bool isInstalled() const;
112 
113  public:
114  void init();
115  [[nodiscard]] const LogEventHandler* getEventHandler() const;
116 
117  bool setAutoRemove(bool pRemove);
118  bool copy(const QString& pDest);
119  [[nodiscard]] bool copyOther(const QString& pSource, const QString& pDest) const;
120  void resetBacklog();
121  QByteArray getBacklog(bool pAll = false);
122  QByteArray getCriticalLogWindow();
123  [[nodiscard]] bool hasCriticalLog() const;
124  [[nodiscard]] int getCriticalLogCapacity() const;
125  void setCriticalLogCapacity(int pSize);
126 
127  static QDateTime getFileDate(const QFileInfo& pInfo);
128  [[nodiscard]] QDateTime getCurrentLogfileDate() const;
129  [[nodiscard]] QFileInfoList getOtherLogfiles() const;
130  bool removeOtherLogfiles();
131  void setLogfile(bool pEnable);
132  [[nodiscard]] bool useLogfile() const;
133  void setUseHandler(bool pEnable);
134  [[nodiscard]] bool useHandler() const;
135 };
136 
137 inline QDebug operator<<(QDebug pDbg, const governikus::LogHandler& pHandler)
138 {
139  Q_ASSERT(pHandler.mLogFile);
140 
141  QDebugStateSaver saver(pDbg);
142  pDbg.nospace() << pHandler.mLogFile->fileName();
143  return pDbg.space();
144 }
145 
146 
147 } // namespace governikus
governikus::LogHandler::getFileDate
static QDateTime getFileDate(const QFileInfo &pInfo)
Definition: LogHandler.cpp:218
governikus::LogHandler::copy
bool copy(const QString &pDest)
Definition: LogHandler.cpp:410
governikus::LogHandler::setAutoRemove
bool setAutoRemove(bool pRemove)
Definition: LogHandler.cpp:132
governikus::operator<<
QDebug operator<<(QDebug pDbg, const CardInfo &pCardInfo)
Definition: CardInfo.cpp:281
governikus::LogHandler::getInstance
static LogHandler & getInstance()
governikus::LogHandler::setCriticalLogCapacity
void setCriticalLogCapacity(int pSize)
Definition: LogHandler.cpp:211
defineSingleton
defineSingleton(LogHandler) LogHandler
Definition: LogHandler.cpp:16
governikus::LogHandler::getEventHandler
const LogEventHandler * getEventHandler() const
Definition: LogHandler.cpp:120
LogHandler.h
governikus::LogHandler::getBacklog
QByteArray getBacklog(bool pAll=false)
Definition: LogHandler.cpp:177
governikus::LogHandler::useLogfile
bool useLogfile() const
Definition: LogHandler.cpp:519
governikus::LogHandler::setLogfile
void setLogfile(bool pEnable)
Definition: LogHandler.cpp:491
governikus::LogHandler::isInstalled
bool isInstalled() const
Definition: LogHandler.cpp:126
governikus::LogHandler::getCriticalLogWindow
QByteArray getCriticalLogWindow()
Definition: LogHandler.cpp:184
governikus::Env
Definition: Env.h:44
governikus::LogHandler::resetBacklog
void resetBacklog()
Definition: LogHandler.cpp:235
governikus::LogEventHandler::fireRawLog
void fireRawLog(const QString &pMsg, const QString &pCategoryName)
governikus::LogHandler::copyOther
bool copyOther(const QString &pSource, const QString &pDest) const
Definition: LogHandler.cpp:423
governikus::LogHandler::getCurrentLogfileDate
QDateTime getCurrentLogfileDate() const
Definition: LogHandler.cpp:229
governikus::LogHandler::hasCriticalLog
bool hasCriticalLog() const
Definition: LogHandler.cpp:199
governikus::LogEventHandler::fireLog
void fireLog(const QString &pMsg)
Every log will be fired by this signal. Be aware that you NEVER use a qDebug() or something like that...
governikus
Implementation of ActivationContext for Intent based activation on Android systems.
Definition: ActivationContext.h:15
governikus::LogHandler::useHandler
bool useHandler() const
Definition: LogHandler.cpp:531
governikus::LogEventHandler
Definition: LogHandler.h:33
governikus::LogHandler::getCriticalLogCapacity
int getCriticalLogCapacity() const
Definition: LogHandler.cpp:205
Env.h
governikus::LogHandler::LogHandler
LogHandler()
governikus::LogHandler::init
void init()
Definition: LogHandler.cpp:73
governikus::LogHandler::operator<<
friend QDebug operator<<(QDebug, const LogHandler &)
Definition: LogHandler.h:137
governikus::LogHandler
Definition: LogHandler.h:52
governikus::LogHandler::setUseHandler
void setUseHandler(bool pEnable)
Definition: LogHandler.cpp:525
governikus::LogHandler::reset
void reset()
Definition: LogHandler.cpp:62
governikus::LogHandler::getOtherLogfiles
QFileInfoList getOtherLogfiles() const
Definition: LogHandler.cpp:445
governikus::LogHandler::removeOtherLogfiles
bool removeOtherLogfiles()
Definition: LogHandler.cpp:478
governikus::LogHandler::~LogHandler
virtual ~LogHandler()
Definition: LogHandler.cpp:49
SingletonHelper.h
LOGCAT
#define LOGCAT(name)