ctkErrorLogModel.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkErrorLogModel_h
  15. #define __ctkErrorLogModel_h
  16. // Qt includes
  17. #include <QPointer>
  18. #include <QSortFilterProxyModel>
  19. // CTK includes
  20. #include "ctkCoreExport.h"
  21. //------------------------------------------------------------------------------
  22. class CTK_CORE_EXPORT ctkErrorLogLevel : public QObject
  23. {
  24. Q_OBJECT
  25. Q_FLAGS(LogLevel)
  26. public:
  27. ctkErrorLogLevel();
  28. enum LogLevel
  29. {
  30. None = 0x0,
  31. Unknown = 0x1,
  32. Status = 0x2,
  33. Trace = 0x4,
  34. Debug = 0x8,
  35. Info = 0x10,
  36. Warning = 0x20,
  37. Error = 0x40,
  38. Critical = 0x80,
  39. Fatal = 0x100
  40. };
  41. Q_DECLARE_FLAGS(LogLevels, LogLevel)
  42. QString operator ()(LogLevel logLevel);
  43. QString logLevelAsString(ctkErrorLogLevel::LogLevel logLevel)const;
  44. };
  45. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkErrorLogLevel::LogLevels)
  46. //------------------------------------------------------------------------------
  47. class ctkErrorLogTerminalOutputPrivate;
  48. //------------------------------------------------------------------------------
  49. class CTK_CORE_EXPORT ctkErrorLogTerminalOutput
  50. {
  51. public:
  52. ctkErrorLogTerminalOutput();
  53. virtual ~ctkErrorLogTerminalOutput();
  54. bool enabled()const;
  55. void setEnabled(bool value);
  56. int fileDescriptor()const;
  57. void setFileDescriptor(int fd);
  58. void output(const QString& text);
  59. protected:
  60. QScopedPointer<ctkErrorLogTerminalOutputPrivate> d_ptr;
  61. private:
  62. Q_DECLARE_PRIVATE(ctkErrorLogTerminalOutput);
  63. Q_DISABLE_COPY(ctkErrorLogTerminalOutput);
  64. };
  65. //------------------------------------------------------------------------------
  66. class ctkErrorLogAbstractMessageHandler;
  67. class ctkErrorLogModelPrivate;
  68. //------------------------------------------------------------------------------
  69. class CTK_CORE_EXPORT ctkErrorLogModel : public QSortFilterProxyModel
  70. {
  71. Q_OBJECT
  72. Q_FLAGS(TerminalOutput)
  73. Q_PROPERTY(bool logEntryGrouping READ logEntryGrouping WRITE setLogEntryGrouping)
  74. Q_PROPERTY(TerminalOutput terminalOutputs READ terminalOutputs WRITE setTerminalOutputs)
  75. Q_PROPERTY(bool asynchronousLogging READ asynchronousLogging WRITE setAsynchronousLogging)
  76. public:
  77. typedef QSortFilterProxyModel Superclass;
  78. typedef ctkErrorLogModel Self;
  79. explicit ctkErrorLogModel(QObject* parentObject = 0);
  80. virtual ~ctkErrorLogModel();
  81. enum ColumnsIds
  82. {
  83. TimeColumn = 0,
  84. ThreadIdColumn,
  85. LogLevelColumn,
  86. OriginColumn,
  87. DescriptionColumn
  88. };
  89. enum ItemDataRole{
  90. DescriptionTextRole = Qt::UserRole + 1
  91. };
  92. /// Register a message handler.
  93. bool registerMsgHandler(ctkErrorLogAbstractMessageHandler * msgHandler);
  94. QStringList msgHandlerNames()const;
  95. /// Return True if the handler identified by \a handlerName is enabled
  96. bool msgHandlerEnabled(const QString& handlerName) const;
  97. /// Enable a specific handler given its name
  98. void setMsgHandlerEnabled(const QString& handlerName, bool enabled);
  99. /// Return names of the enabled message handlers
  100. QStringList msgHandlerEnabled()const;
  101. /// Enable handler identified by their names
  102. void setMsgHandlerEnabled(const QStringList& handlerNames);
  103. void enableAllMsgHandler();
  104. void disableAllMsgHandler();
  105. void setAllMsgHandlerEnabled(bool enabled);
  106. enum TerminalOutput
  107. {
  108. None = 0x0,
  109. StandardError = 0x1,
  110. StandardOutput = 0x2,
  111. All = StandardError | StandardOutput
  112. };
  113. Q_DECLARE_FLAGS(TerminalOutputs, TerminalOutput)
  114. /// Return if messages are both printed into the terminal and added to ctkErrorLogModel.
  115. /// \note If TerminalOutput::None is returned, message will only be added to the model.
  116. TerminalOutputs terminalOutputs()const;
  117. /// Set terminal output mode
  118. /// \sa terminalOutputs()
  119. /// \sa TerminalOutput
  120. void setTerminalOutputs(const TerminalOutputs& terminalOutput);
  121. /// Remove all message from model
  122. void clear();
  123. ctkErrorLogLevel::LogLevels logLevelFilter()const;
  124. void filterEntry(const ctkErrorLogLevel::LogLevels& logLevel = ctkErrorLogLevel::Unknown, bool disableFilter = false);
  125. bool logEntryGrouping()const;
  126. void setLogEntryGrouping(bool value);
  127. bool asynchronousLogging()const;
  128. void setAsynchronousLogging(bool value);
  129. public Q_SLOTS:
  130. void addEntry(const QDateTime& currentDateTime, const QString& threadId,
  131. ctkErrorLogLevel::LogLevel logLevel, const QString& origin, const QString& text);
  132. Q_SIGNALS:
  133. void logLevelFilterChanged();
  134. protected:
  135. QScopedPointer<ctkErrorLogModelPrivate> d_ptr;
  136. private:
  137. Q_DECLARE_PRIVATE(ctkErrorLogModel);
  138. Q_DISABLE_COPY(ctkErrorLogModel);
  139. };
  140. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkErrorLogModel::TerminalOutputs)
  141. //------------------------------------------------------------------------------
  142. class ctkErrorLogAbstractMessageHandlerPrivate;
  143. //------------------------------------------------------------------------------
  144. class CTK_CORE_EXPORT ctkErrorLogAbstractMessageHandler : public QObject
  145. {
  146. Q_OBJECT
  147. public:
  148. typedef QObject Superclass;
  149. /// Disabled by default.
  150. ctkErrorLogAbstractMessageHandler();
  151. virtual ~ctkErrorLogAbstractMessageHandler();
  152. virtual QString handlerName()const = 0;
  153. QString handlerPrettyName()const;
  154. bool enabled()const;
  155. void setEnabled(bool value);
  156. void handleMessage(const QString& threadId, ctkErrorLogLevel::LogLevel logLevel,
  157. const QString& origin, const QString& text);
  158. ctkErrorLogTerminalOutput* terminalOutput(ctkErrorLogModel::TerminalOutput terminalOutputType)const;
  159. void setTerminalOutput(ctkErrorLogModel::TerminalOutput terminalOutputType,
  160. ctkErrorLogTerminalOutput * terminalOutput);
  161. Q_SIGNALS:
  162. void messageHandled(const QDateTime& currentDateTime, const QString& threadId,
  163. ctkErrorLogLevel::LogLevel logLevel, const QString& origin,
  164. const QString& text);
  165. protected:
  166. void setHandlerPrettyName(const QString& newHandlerPrettyName);
  167. virtual void setEnabledInternal(bool value) = 0;
  168. protected:
  169. QScopedPointer<ctkErrorLogAbstractMessageHandlerPrivate> d_ptr;
  170. private:
  171. Q_DECLARE_PRIVATE(ctkErrorLogAbstractMessageHandler);
  172. Q_DISABLE_COPY(ctkErrorLogAbstractMessageHandler);
  173. };
  174. #endif