ctkErrorLogModel.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /// \ingroup Core
  23. class CTK_CORE_EXPORT ctkErrorLogLevel : public QObject
  24. {
  25. Q_OBJECT
  26. Q_FLAGS(LogLevel)
  27. public:
  28. ctkErrorLogLevel();
  29. enum LogLevel
  30. {
  31. None = 0x0,
  32. Unknown = 0x1,
  33. Status = 0x2,
  34. Trace = 0x4,
  35. Debug = 0x8,
  36. Info = 0x10,
  37. Warning = 0x20,
  38. Error = 0x40,
  39. Critical = 0x80,
  40. Fatal = 0x100
  41. };
  42. Q_DECLARE_FLAGS(LogLevels, LogLevel)
  43. QString operator ()(LogLevel logLevel);
  44. static QString logLevelAsString(ctkErrorLogLevel::LogLevel logLevel);
  45. };
  46. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkErrorLogLevel::LogLevels)
  47. //------------------------------------------------------------------------------
  48. class ctkErrorLogTerminalOutputPrivate;
  49. //------------------------------------------------------------------------------
  50. /// \ingroup Core
  51. class CTK_CORE_EXPORT ctkErrorLogTerminalOutput
  52. {
  53. public:
  54. ctkErrorLogTerminalOutput();
  55. virtual ~ctkErrorLogTerminalOutput();
  56. bool enabled()const;
  57. void setEnabled(bool value);
  58. int fileDescriptor()const;
  59. void setFileDescriptor(int fd);
  60. void output(const QString& text);
  61. protected:
  62. QScopedPointer<ctkErrorLogTerminalOutputPrivate> d_ptr;
  63. private:
  64. Q_DECLARE_PRIVATE(ctkErrorLogTerminalOutput);
  65. Q_DISABLE_COPY(ctkErrorLogTerminalOutput);
  66. };
  67. //------------------------------------------------------------------------------
  68. class ctkErrorLogAbstractMessageHandler;
  69. class ctkErrorLogModelPrivate;
  70. //------------------------------------------------------------------------------
  71. /// \ingroup Core
  72. class CTK_CORE_EXPORT ctkErrorLogModel : public QSortFilterProxyModel
  73. {
  74. Q_OBJECT
  75. Q_FLAGS(TerminalOutput)
  76. Q_PROPERTY(bool logEntryGrouping READ logEntryGrouping WRITE setLogEntryGrouping)
  77. Q_PROPERTY(TerminalOutput terminalOutputs READ terminalOutputs WRITE setTerminalOutputs)
  78. Q_PROPERTY(bool asynchronousLogging READ asynchronousLogging WRITE setAsynchronousLogging)
  79. public:
  80. typedef QSortFilterProxyModel Superclass;
  81. typedef ctkErrorLogModel Self;
  82. explicit ctkErrorLogModel(QObject* parentObject = 0);
  83. virtual ~ctkErrorLogModel();
  84. enum ColumnsIds
  85. {
  86. TimeColumn = 0,
  87. ThreadIdColumn,
  88. LogLevelColumn,
  89. OriginColumn,
  90. DescriptionColumn,
  91. MaxColumn = DescriptionColumn
  92. };
  93. enum ItemDataRole{
  94. DescriptionTextRole = Qt::UserRole + 1
  95. };
  96. /// Register a message handler.
  97. bool registerMsgHandler(ctkErrorLogAbstractMessageHandler * msgHandler);
  98. QStringList msgHandlerNames()const;
  99. /// Return True if the handler identified by \a handlerName is enabled
  100. bool msgHandlerEnabled(const QString& handlerName) const;
  101. /// Enable a specific handler given its name
  102. void setMsgHandlerEnabled(const QString& handlerName, bool enabled);
  103. /// Return names of the enabled message handlers
  104. QStringList msgHandlerEnabled()const;
  105. /// Enable handler identified by their names
  106. void setMsgHandlerEnabled(const QStringList& handlerNames);
  107. void enableAllMsgHandler();
  108. void disableAllMsgHandler();
  109. void setAllMsgHandlerEnabled(bool enabled);
  110. enum TerminalOutput
  111. {
  112. None = 0x0,
  113. StandardError = 0x1,
  114. StandardOutput = 0x2,
  115. All = StandardError | StandardOutput
  116. };
  117. Q_DECLARE_FLAGS(TerminalOutputs, TerminalOutput)
  118. /// Return if messages are both printed into the terminal and added to ctkErrorLogModel.
  119. /// \note If TerminalOutput::None is returned, message will only be added to the model.
  120. TerminalOutputs terminalOutputs()const;
  121. /// Set terminal output mode
  122. /// \sa terminalOutputs()
  123. /// \sa TerminalOutput
  124. void setTerminalOutputs(const TerminalOutputs& terminalOutput);
  125. ctkErrorLogLevel::LogLevels logLevelFilter()const;
  126. void filterEntry(const ctkErrorLogLevel::LogLevels& logLevel = ctkErrorLogLevel::Unknown, bool disableFilter = false);
  127. bool logEntryGrouping()const;
  128. void setLogEntryGrouping(bool value);
  129. bool asynchronousLogging()const;
  130. void setAsynchronousLogging(bool value);
  131. /// Return log entry information associated with \a row and \a column.
  132. /// \internal
  133. QVariant logEntryData(int row,
  134. int column = ctkErrorLogModel::DescriptionColumn,
  135. int role = Qt::DisplayRole) const;
  136. /// Return log entry information associated with Description column.
  137. /// \sa ctkErrorLogModel::DescriptionColumn, logEntryData()
  138. Q_INVOKABLE QString logEntryDescription(int row) const;
  139. /// Return current number of log entries.
  140. /// \sa clear()
  141. Q_INVOKABLE int logEntryCount() const;
  142. public Q_SLOTS:
  143. /// Remove all log entries from model
  144. void clear();
  145. /// \sa logEntryGrouping(), asynchronousLogging()
  146. void addEntry(const QDateTime& currentDateTime, const QString& threadId,
  147. ctkErrorLogLevel::LogLevel logLevel, const QString& origin, const QString& text);
  148. Q_SIGNALS:
  149. void logLevelFilterChanged();
  150. /// \sa addEntry()
  151. void entryAdded(ctkErrorLogLevel::LogLevel logLevel);
  152. protected:
  153. QScopedPointer<ctkErrorLogModelPrivate> d_ptr;
  154. private:
  155. Q_DECLARE_PRIVATE(ctkErrorLogModel);
  156. Q_DISABLE_COPY(ctkErrorLogModel);
  157. };
  158. Q_DECLARE_OPERATORS_FOR_FLAGS(ctkErrorLogModel::TerminalOutputs)
  159. //------------------------------------------------------------------------------
  160. class ctkErrorLogAbstractMessageHandlerPrivate;
  161. //------------------------------------------------------------------------------
  162. /// \ingroup Core
  163. class CTK_CORE_EXPORT ctkErrorLogAbstractMessageHandler : public QObject
  164. {
  165. Q_OBJECT
  166. public:
  167. typedef QObject Superclass;
  168. /// Disabled by default.
  169. ctkErrorLogAbstractMessageHandler();
  170. virtual ~ctkErrorLogAbstractMessageHandler();
  171. virtual QString handlerName()const = 0;
  172. QString handlerPrettyName()const;
  173. bool enabled()const;
  174. void setEnabled(bool value);
  175. void handleMessage(const QString& threadId, ctkErrorLogLevel::LogLevel logLevel,
  176. const QString& origin, const QString& text);
  177. ctkErrorLogTerminalOutput* terminalOutput(ctkErrorLogModel::TerminalOutput terminalOutputType)const;
  178. void setTerminalOutput(ctkErrorLogModel::TerminalOutput terminalOutputType,
  179. ctkErrorLogTerminalOutput * terminalOutput);
  180. Q_SIGNALS:
  181. void messageHandled(const QDateTime& currentDateTime, const QString& threadId,
  182. ctkErrorLogLevel::LogLevel logLevel, const QString& origin,
  183. const QString& text);
  184. protected:
  185. void setHandlerPrettyName(const QString& newHandlerPrettyName);
  186. virtual void setEnabledInternal(bool value) = 0;
  187. protected:
  188. QScopedPointer<ctkErrorLogAbstractMessageHandlerPrivate> d_ptr;
  189. private:
  190. Q_DECLARE_PRIVATE(ctkErrorLogAbstractMessageHandler);
  191. Q_DISABLE_COPY(ctkErrorLogAbstractMessageHandler);
  192. };
  193. #endif