ctkErrorLogModel.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.commontk.org/LICENSE
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QDateTime>
  17. #include <QDebug>
  18. #include <QMainWindow>
  19. #include <QMetaEnum>
  20. #include <QMetaType>
  21. #include <QPointer>
  22. #include <QStandardItem>
  23. #include <QStatusBar>
  24. // CTK includes
  25. #include "ctkErrorLogModel.h"
  26. #include <ctkPimpl.h>
  27. // --------------------------------------------------------------------------
  28. // ctkErrorLogAbstractMessageHandler methods
  29. // --------------------------------------------------------------------------
  30. ctkErrorLogModel* ctkErrorLogAbstractMessageHandler::errorLogModel()const
  31. {
  32. return this->ErrorLogModel.data();
  33. }
  34. // --------------------------------------------------------------------------
  35. void ctkErrorLogAbstractMessageHandler::setErrorLogModel(ctkErrorLogModel * newErrorLogModel)
  36. {
  37. this->ErrorLogModel = QPointer<ctkErrorLogModel>(newErrorLogModel);
  38. }
  39. // --------------------------------------------------------------------------
  40. QString ctkErrorLogAbstractMessageHandler::handlerPrettyName()const
  41. {
  42. if (this->HandlerPrettyName.isEmpty())
  43. {
  44. return this->handlerName();
  45. }
  46. else
  47. {
  48. return this->HandlerPrettyName;
  49. }
  50. }
  51. // --------------------------------------------------------------------------
  52. void ctkErrorLogAbstractMessageHandler::setHandlerPrettyName(const QString& newHandlerPrettyName)
  53. {
  54. this->HandlerPrettyName = newHandlerPrettyName;
  55. }
  56. // --------------------------------------------------------------------------
  57. bool ctkErrorLogAbstractMessageHandler::enabled()const
  58. {
  59. return this->Enabled;
  60. }
  61. // --------------------------------------------------------------------------
  62. void ctkErrorLogAbstractMessageHandler::setEnabled(bool value)
  63. {
  64. if (value == this->Enabled)
  65. {
  66. return;
  67. }
  68. this->setEnabledInternal(value);
  69. this->Enabled = value;
  70. }
  71. // --------------------------------------------------------------------------
  72. // ctkErrorLogModelPrivate
  73. // --------------------------------------------------------------------------
  74. class ctkErrorLogModelPrivate
  75. {
  76. Q_DECLARE_PUBLIC(ctkErrorLogModel);
  77. protected:
  78. ctkErrorLogModel* const q_ptr;
  79. public:
  80. ctkErrorLogModelPrivate(ctkErrorLogModel& object);
  81. ~ctkErrorLogModelPrivate();
  82. void init();
  83. QStandardItemModel StandardItemModel;
  84. QHash<QString, ctkErrorLogAbstractMessageHandler*> RegisteredHandlers;
  85. ctkErrorLogModel::LogLevels CurrentLogLevelFilter;
  86. bool LogEntryGrouping;
  87. };
  88. // --------------------------------------------------------------------------
  89. // ctkErrorLogModelPrivate methods
  90. // --------------------------------------------------------------------------
  91. ctkErrorLogModelPrivate::ctkErrorLogModelPrivate(ctkErrorLogModel& object)
  92. : q_ptr(&object)
  93. {
  94. }
  95. // --------------------------------------------------------------------------
  96. ctkErrorLogModelPrivate::~ctkErrorLogModelPrivate()
  97. {
  98. this->LogEntryGrouping = false;
  99. foreach(const QString& handlerName, this->RegisteredHandlers.keys())
  100. {
  101. ctkErrorLogAbstractMessageHandler * msgHandler =
  102. this->RegisteredHandlers.value(handlerName);
  103. Q_ASSERT(msgHandler);
  104. msgHandler->setEnabled(false);
  105. delete msgHandler;
  106. }
  107. }
  108. // --------------------------------------------------------------------------
  109. void ctkErrorLogModelPrivate::init()
  110. {
  111. Q_Q(ctkErrorLogModel);
  112. q->setDynamicSortFilter(true);
  113. //
  114. // WARNING - Using a QSortFilterProxyModel slows down the insertion of rows by a factor 10
  115. //
  116. q->setSourceModel(&this->StandardItemModel);
  117. q->setFilterKeyColumn(ctkErrorLogModel::LogLevelColumn);
  118. }
  119. // --------------------------------------------------------------------------
  120. // ctkErrorLogModel methods
  121. //------------------------------------------------------------------------------
  122. ctkErrorLogModel::ctkErrorLogModel(QObject * parentObject)
  123. : Superclass(parentObject)
  124. , d_ptr(new ctkErrorLogModelPrivate(*this))
  125. {
  126. Q_D(ctkErrorLogModel);
  127. d->init();
  128. }
  129. //------------------------------------------------------------------------------
  130. ctkErrorLogModel::~ctkErrorLogModel()
  131. {
  132. }
  133. //------------------------------------------------------------------------------
  134. bool ctkErrorLogModel::registerMsgHandler(ctkErrorLogAbstractMessageHandler * msgHandler)
  135. {
  136. Q_D(ctkErrorLogModel);
  137. if (!msgHandler)
  138. {
  139. return false;
  140. }
  141. if (d->RegisteredHandlers.keys().contains(msgHandler->handlerName()))
  142. {
  143. return false;
  144. }
  145. msgHandler->setErrorLogModel(this);
  146. d->RegisteredHandlers.insert(msgHandler->handlerName(), msgHandler);
  147. return true;
  148. }
  149. //------------------------------------------------------------------------------
  150. QStringList ctkErrorLogModel::msgHandlerNames()const
  151. {
  152. Q_D(const ctkErrorLogModel);
  153. return d->RegisteredHandlers.keys();
  154. }
  155. //------------------------------------------------------------------------------
  156. bool ctkErrorLogModel::msgHandlerEnabled(const QString& handlerName) const
  157. {
  158. Q_D(const ctkErrorLogModel);
  159. if (!d->RegisteredHandlers.keys().contains(handlerName))
  160. {
  161. return false;
  162. }
  163. return d->RegisteredHandlers.value(handlerName)->enabled();
  164. }
  165. //------------------------------------------------------------------------------
  166. void ctkErrorLogModel::setMsgHandlerEnabled(const QString& handlerName, bool enabled)
  167. {
  168. Q_D(ctkErrorLogModel);
  169. if (!d->RegisteredHandlers.keys().contains(handlerName))
  170. {
  171. // qCritical() << "Failed to enable/disable message handler " << handlerName
  172. // << "- Handler not registered !";
  173. return;
  174. }
  175. d->RegisteredHandlers.value(handlerName)->setEnabled(enabled);
  176. }
  177. //------------------------------------------------------------------------------
  178. void ctkErrorLogModel::disableAllMsgHandler()
  179. {
  180. Q_D(ctkErrorLogModel);
  181. foreach(const QString& msgHandlerName, d->RegisteredHandlers.keys())
  182. {
  183. this->setMsgHandlerEnabled(msgHandlerName, false);
  184. }
  185. }
  186. //------------------------------------------------------------------------------
  187. QString ctkErrorLogModel::logLevelAsString(LogLevel logLevel)const
  188. {
  189. QMetaEnum logLevelEnum = this->metaObject()->enumerator(0);
  190. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  191. return QLatin1String(logLevelEnum.valueToKey(logLevel));
  192. }
  193. //------------------------------------------------------------------------------
  194. void ctkErrorLogModel::addEntry(ctkErrorLogModel::LogLevel logLevel,
  195. const QString& origin, const char* text)
  196. {
  197. Q_D(ctkErrorLogModel);
  198. QString timeFormat("dd.MM.yyyy hh:mm:ss");
  199. QDateTime currentDateTime = QDateTime::currentDateTime();
  200. bool groupEntry = false;
  201. if (d->LogEntryGrouping)
  202. {
  203. // Retrieve logLevel associated with last row
  204. QModelIndex lastRowLogLevelIndex =
  205. d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::LogLevelColumn);
  206. bool logLevelMatched = this->logLevelAsString(logLevel) == lastRowLogLevelIndex.data().toString();
  207. // Retrieve origin associated with last row
  208. QModelIndex lastRowOriginIndex =
  209. d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::OriginColumn);
  210. bool originMatched = origin == lastRowOriginIndex.data().toString();
  211. // Retrieve time associated with last row
  212. QModelIndex lastRowTimeIndex =
  213. d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::TimeColumn);
  214. QDateTime lastRowDateTime = QDateTime::fromString(lastRowTimeIndex.data().toString(), timeFormat);
  215. int groupingIntervalInMsecs = 1000;
  216. bool withinGroupingInterval = lastRowDateTime.time().msecsTo(currentDateTime.time()) <= groupingIntervalInMsecs;
  217. groupEntry = logLevelMatched && originMatched && withinGroupingInterval;
  218. }
  219. if (!groupEntry)
  220. {
  221. QList<QStandardItem*> itemList;
  222. // Time item
  223. QStandardItem * timeItem = new QStandardItem(currentDateTime.toString(timeFormat));
  224. timeItem->setEditable(false);
  225. itemList << timeItem;
  226. // LogLevel item
  227. QStandardItem * logLevelItem = new QStandardItem(this->logLevelAsString(logLevel));
  228. logLevelItem->setEditable(false);
  229. itemList << logLevelItem;
  230. // Origin item
  231. QStandardItem * originItem = new QStandardItem(origin);
  232. originItem->setEditable(false);
  233. itemList << originItem;
  234. // Description item
  235. QStandardItem * descriptionItem = new QStandardItem();
  236. QString descriptionText(text);
  237. descriptionItem->setData(descriptionText.left(160).append((descriptionText.size() > 160) ? "..." : ""), Qt::DisplayRole);
  238. descriptionItem->setData(descriptionText, ctkErrorLogModel::DescriptionTextRole);
  239. descriptionItem->setEditable(false);
  240. itemList << descriptionItem;
  241. d->StandardItemModel.invisibleRootItem()->appendRow(itemList);
  242. }
  243. else
  244. {
  245. // Retrieve description associated with last row
  246. QModelIndex lastRowDescriptionIndex =
  247. d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::DescriptionColumn);
  248. QStringList updatedDescription;
  249. updatedDescription << lastRowDescriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  250. updatedDescription << QLatin1String(text);
  251. d->StandardItemModel.setData(lastRowDescriptionIndex, updatedDescription.join("\n"),
  252. ctkErrorLogModel::DescriptionTextRole);
  253. // Append '...' to displayText if needed
  254. QString displayText = lastRowDescriptionIndex.data().toString();
  255. if (!displayText.endsWith("..."))
  256. {
  257. d->StandardItemModel.setData(lastRowDescriptionIndex, displayText.append("..."), Qt::DisplayRole);
  258. }
  259. }
  260. }
  261. //------------------------------------------------------------------------------
  262. void ctkErrorLogModel::clear()
  263. {
  264. Q_D(ctkErrorLogModel);
  265. d->StandardItemModel.invisibleRootItem()->removeRows(0, d->StandardItemModel.rowCount());
  266. }
  267. //------------------------------------------------------------------------------
  268. void ctkErrorLogModel::filterEntry(const LogLevels& logLevel, bool disableFilter)
  269. {
  270. Q_D(ctkErrorLogModel);
  271. QStringList patterns;
  272. if (!this->filterRegExp().pattern().isEmpty())
  273. {
  274. patterns << this->filterRegExp().pattern().split("|");
  275. }
  276. patterns.removeAll(this->logLevelAsString(Self::None));
  277. // foreach(QString s, patterns)
  278. // {
  279. // std::cout << "pattern:" << qPrintable(s) << std::endl;
  280. // }
  281. QMetaEnum logLevelEnum = this->metaObject()->enumerator(0);
  282. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  283. // Loop over enum values and append associated name to 'patterns' if
  284. // it has been specified within 'logLevel'
  285. for (int i = 1; i < logLevelEnum.keyCount(); ++i)
  286. {
  287. int aLogLevel = logLevelEnum.value(i);
  288. if (logLevel & aLogLevel)
  289. {
  290. QString logLevelAsString = this->logLevelAsString(static_cast<Self::LogLevel>(aLogLevel));
  291. if (!disableFilter)
  292. {
  293. patterns << logLevelAsString;
  294. d->CurrentLogLevelFilter |= static_cast<Self::LogLevels>(aLogLevel);
  295. }
  296. else
  297. {
  298. patterns.removeAll(logLevelAsString);
  299. d->CurrentLogLevelFilter ^= static_cast<Self::LogLevels>(aLogLevel);
  300. }
  301. }
  302. }
  303. if (patterns.isEmpty())
  304. {
  305. // If there are no patterns, let's filter with the None level so that
  306. // all entries are filtered out.
  307. patterns << this->logLevelAsString(Self::None);
  308. }
  309. bool filterChanged = true;
  310. QStringList currentPatterns = this->filterRegExp().pattern().split("|");
  311. if (currentPatterns.count() == patterns.count())
  312. {
  313. foreach(const QString& p, patterns)
  314. {
  315. currentPatterns.removeAll(p);
  316. }
  317. filterChanged = currentPatterns.count() > 0;
  318. }
  319. this->setFilterRegExp(patterns.join("|"));
  320. if (filterChanged)
  321. {
  322. emit this->logLevelFilterChanged();
  323. }
  324. }
  325. //------------------------------------------------------------------------------
  326. ctkErrorLogModel::LogLevels ctkErrorLogModel::logLevelFilter()const
  327. {
  328. QMetaEnum logLevelEnum = this->metaObject()->enumerator(0);
  329. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  330. Self::LogLevels filter = Self::Unknown;
  331. foreach(const QString& filterAsString, this->filterRegExp().pattern().split("|"))
  332. {
  333. filter |= static_cast<Self::LogLevels>(logLevelEnum.keyToValue(filterAsString.toLatin1()));
  334. }
  335. return filter;
  336. }
  337. //------------------------------------------------------------------------------
  338. bool ctkErrorLogModel::logEntryGrouping()const
  339. {
  340. Q_D(const ctkErrorLogModel);
  341. return d->LogEntryGrouping;
  342. }
  343. //------------------------------------------------------------------------------
  344. void ctkErrorLogModel::setLogEntryGrouping(bool value)
  345. {
  346. Q_D(ctkErrorLogModel);
  347. d->LogEntryGrouping = value;
  348. }