ctkErrorLogModel.cpp 13 KB

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