ctkErrorLogModel.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. // Qt includes
  15. #include <QApplication>
  16. #include <QDateTime>
  17. #include <QDebug>
  18. #include <QFile>
  19. #include <QMainWindow>
  20. #include <QMetaEnum>
  21. #include <QMetaType>
  22. #include <QMutexLocker>
  23. #include <QPointer>
  24. #include <QStandardItem>
  25. #include <QStatusBar>
  26. // CTK includes
  27. #include "ctkErrorLogModel.h"
  28. #include <ctkPimpl.h>
  29. // STD includes
  30. #include <cstdio> // For _fileno or fileno
  31. #ifdef _MSC_VER
  32. # include <io.h> // For _write()
  33. #else
  34. # include <unistd.h>
  35. #endif
  36. // --------------------------------------------------------------------------
  37. // ctkErrorLogLevel methods
  38. // --------------------------------------------------------------------------
  39. ctkErrorLogLevel::ctkErrorLogLevel()
  40. {
  41. qRegisterMetaType<ctkErrorLogLevel::LogLevel>("ctkErrorLogLevel::LogLevel");
  42. }
  43. // --------------------------------------------------------------------------
  44. QString ctkErrorLogLevel::operator()(ctkErrorLogLevel::LogLevel logLevel)
  45. {
  46. return this->logLevelAsString(logLevel);
  47. }
  48. // --------------------------------------------------------------------------
  49. QString ctkErrorLogLevel::logLevelAsString(ctkErrorLogLevel::LogLevel logLevel)const
  50. {
  51. QMetaEnum logLevelEnum = this->metaObject()->enumerator(0);
  52. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  53. return QLatin1String(logLevelEnum.valueToKey(logLevel));
  54. }
  55. // --------------------------------------------------------------------------
  56. // ctkErrorLogTerminalOutputPrivate
  57. // --------------------------------------------------------------------------
  58. class ctkErrorLogTerminalOutputPrivate
  59. {
  60. public:
  61. ctkErrorLogTerminalOutputPrivate();
  62. ~ctkErrorLogTerminalOutputPrivate();
  63. bool Enabled;
  64. mutable QMutex EnableMutex;
  65. int FD;
  66. mutable QMutex OutputMutex;
  67. };
  68. // --------------------------------------------------------------------------
  69. ctkErrorLogTerminalOutputPrivate::ctkErrorLogTerminalOutputPrivate()
  70. : Enabled(false)
  71. {
  72. #ifdef Q_OS_WIN32
  73. this->FD = _fileno(stdout);
  74. #else
  75. this->FD = fileno(stdout);
  76. #endif
  77. }
  78. // --------------------------------------------------------------------------
  79. ctkErrorLogTerminalOutputPrivate::~ctkErrorLogTerminalOutputPrivate()
  80. {
  81. }
  82. // --------------------------------------------------------------------------
  83. // ctkErrorLogTerminalOutput methods
  84. // --------------------------------------------------------------------------
  85. ctkErrorLogTerminalOutput::ctkErrorLogTerminalOutput()
  86. : d_ptr(new ctkErrorLogTerminalOutputPrivate)
  87. {
  88. }
  89. // --------------------------------------------------------------------------
  90. ctkErrorLogTerminalOutput::~ctkErrorLogTerminalOutput()
  91. {
  92. }
  93. // --------------------------------------------------------------------------
  94. bool ctkErrorLogTerminalOutput::enabled()const
  95. {
  96. Q_D(const ctkErrorLogTerminalOutput);
  97. QMutexLocker locker(&d->EnableMutex);
  98. return d->Enabled;
  99. }
  100. // --------------------------------------------------------------------------
  101. void ctkErrorLogTerminalOutput::setEnabled(bool value)
  102. {
  103. Q_D(ctkErrorLogTerminalOutput);
  104. QMutexLocker locker(&d->EnableMutex);
  105. d->Enabled = value;
  106. }
  107. // --------------------------------------------------------------------------
  108. int ctkErrorLogTerminalOutput::fileDescriptor()const
  109. {
  110. Q_D(const ctkErrorLogTerminalOutput);
  111. QMutexLocker locker(&d->OutputMutex);
  112. return d->FD;
  113. }
  114. // --------------------------------------------------------------------------
  115. void ctkErrorLogTerminalOutput::setFileDescriptor(int fd)
  116. {
  117. Q_D(ctkErrorLogTerminalOutput);
  118. QMutexLocker locker(&d->OutputMutex);
  119. d->FD = fd;
  120. }
  121. // --------------------------------------------------------------------------
  122. void ctkErrorLogTerminalOutput::output(const QString& text)
  123. {
  124. Q_D(ctkErrorLogTerminalOutput);
  125. {
  126. QMutexLocker locker(&d->EnableMutex);
  127. if (!d->Enabled)
  128. {
  129. return;
  130. }
  131. }
  132. {
  133. QMutexLocker locker(&d->OutputMutex);
  134. QString textWithNewLine = text + "\n";
  135. #ifdef _MSC_VER
  136. int res = _write(d->FD, qPrintable(textWithNewLine), textWithNewLine.size());
  137. #else
  138. ssize_t res = write(d->FD, qPrintable(textWithNewLine), textWithNewLine.size());
  139. #endif
  140. if (res == -1)
  141. {
  142. return;
  143. }
  144. }
  145. }
  146. // --------------------------------------------------------------------------
  147. // ctkErrorLogModelPrivate
  148. // --------------------------------------------------------------------------
  149. class ctkErrorLogModelPrivate
  150. {
  151. Q_DECLARE_PUBLIC(ctkErrorLogModel);
  152. protected:
  153. ctkErrorLogModel* const q_ptr;
  154. public:
  155. ctkErrorLogModelPrivate(ctkErrorLogModel& object);
  156. ~ctkErrorLogModelPrivate();
  157. void init();
  158. /// Convenient method that could be used for debugging purposes.
  159. void appendToFile(const QString& fileName, const QString& text);
  160. void setMessageHandlerConnection(ctkErrorLogAbstractMessageHandler * msgHandler, bool asynchronous);
  161. QStandardItemModel StandardItemModel;
  162. QHash<QString, ctkErrorLogAbstractMessageHandler*> RegisteredHandlers;
  163. ctkErrorLogLevel::LogLevels CurrentLogLevelFilter;
  164. bool LogEntryGrouping;
  165. bool AsynchronousLogging;
  166. bool AddingEntry;
  167. ctkErrorLogLevel ErrorLogLevel;
  168. ctkErrorLogTerminalOutput StdErrTerminalOutput;
  169. ctkErrorLogTerminalOutput StdOutTerminalOutput;
  170. QMutex AppendToFileMutex;
  171. };
  172. // --------------------------------------------------------------------------
  173. // ctkErrorLogModelPrivate methods
  174. // --------------------------------------------------------------------------
  175. ctkErrorLogModelPrivate::ctkErrorLogModelPrivate(ctkErrorLogModel& object)
  176. : q_ptr(&object)
  177. {
  178. this->LogEntryGrouping = false;
  179. this->AsynchronousLogging = true;
  180. this->AddingEntry = false;
  181. }
  182. // --------------------------------------------------------------------------
  183. ctkErrorLogModelPrivate::~ctkErrorLogModelPrivate()
  184. {
  185. foreach(const QString& handlerName, this->RegisteredHandlers.keys())
  186. {
  187. ctkErrorLogAbstractMessageHandler * msgHandler =
  188. this->RegisteredHandlers.value(handlerName);
  189. Q_ASSERT(msgHandler);
  190. msgHandler->setEnabled(false);
  191. delete msgHandler;
  192. }
  193. }
  194. // --------------------------------------------------------------------------
  195. void ctkErrorLogModelPrivate::init()
  196. {
  197. Q_Q(ctkErrorLogModel);
  198. q->setDynamicSortFilter(true);
  199. //
  200. // WARNING - Using a QSortFilterProxyModel slows down the insertion of rows by a factor 10
  201. //
  202. q->setSourceModel(&this->StandardItemModel);
  203. q->setFilterKeyColumn(ctkErrorLogModel::LogLevelColumn);
  204. }
  205. // --------------------------------------------------------------------------
  206. void ctkErrorLogModelPrivate::appendToFile(const QString& fileName, const QString& text)
  207. {
  208. QMutexLocker locker(&this->AppendToFileMutex);
  209. QFile f(fileName);
  210. f.open(QFile::Append);
  211. QTextStream s(&f);
  212. s << QDateTime::currentDateTime().toString() << " - " << text << "\n";
  213. f.close();
  214. }
  215. // --------------------------------------------------------------------------
  216. void ctkErrorLogModelPrivate::setMessageHandlerConnection(
  217. ctkErrorLogAbstractMessageHandler * msgHandler, bool asynchronous)
  218. {
  219. Q_Q(ctkErrorLogModel);
  220. msgHandler->disconnect();
  221. QObject::connect(msgHandler,
  222. SIGNAL(messageHandled(QDateTime,QString,ctkErrorLogLevel::LogLevel,QString,QString)),
  223. q, SLOT(addEntry(QDateTime,QString,ctkErrorLogLevel::LogLevel,QString,QString)),
  224. asynchronous ? Qt::QueuedConnection : Qt::BlockingQueuedConnection);
  225. }
  226. // --------------------------------------------------------------------------
  227. // ctkErrorLogModel methods
  228. //------------------------------------------------------------------------------
  229. ctkErrorLogModel::ctkErrorLogModel(QObject * parentObject)
  230. : Superclass(parentObject)
  231. , d_ptr(new ctkErrorLogModelPrivate(*this))
  232. {
  233. Q_D(ctkErrorLogModel);
  234. d->init();
  235. }
  236. //------------------------------------------------------------------------------
  237. ctkErrorLogModel::~ctkErrorLogModel()
  238. {
  239. }
  240. //------------------------------------------------------------------------------
  241. bool ctkErrorLogModel::registerMsgHandler(ctkErrorLogAbstractMessageHandler * msgHandler)
  242. {
  243. Q_D(ctkErrorLogModel);
  244. if (!msgHandler)
  245. {
  246. return false;
  247. }
  248. if (d->RegisteredHandlers.keys().contains(msgHandler->handlerName()))
  249. {
  250. return false;
  251. }
  252. d->setMessageHandlerConnection(msgHandler, d->AsynchronousLogging);
  253. msgHandler->setTerminalOutput(Self::StandardError, &d->StdErrTerminalOutput);
  254. msgHandler->setTerminalOutput(Self::StandardOutput, &d->StdOutTerminalOutput);
  255. d->RegisteredHandlers.insert(msgHandler->handlerName(), msgHandler);
  256. return true;
  257. }
  258. //------------------------------------------------------------------------------
  259. QStringList ctkErrorLogModel::msgHandlerNames()const
  260. {
  261. Q_D(const ctkErrorLogModel);
  262. return d->RegisteredHandlers.keys();
  263. }
  264. //------------------------------------------------------------------------------
  265. bool ctkErrorLogModel::msgHandlerEnabled(const QString& handlerName) const
  266. {
  267. Q_D(const ctkErrorLogModel);
  268. if (!d->RegisteredHandlers.keys().contains(handlerName))
  269. {
  270. return false;
  271. }
  272. return d->RegisteredHandlers.value(handlerName)->enabled();
  273. }
  274. //------------------------------------------------------------------------------
  275. void ctkErrorLogModel::setMsgHandlerEnabled(const QString& handlerName, bool enabled)
  276. {
  277. Q_D(ctkErrorLogModel);
  278. if (!d->RegisteredHandlers.keys().contains(handlerName))
  279. {
  280. // qCritical() << "Failed to enable/disable message handler " << handlerName
  281. // << "- Handler not registered !";
  282. return;
  283. }
  284. d->RegisteredHandlers.value(handlerName)->setEnabled(enabled);
  285. }
  286. //------------------------------------------------------------------------------
  287. QStringList ctkErrorLogModel::msgHandlerEnabled() const
  288. {
  289. Q_D(const ctkErrorLogModel);
  290. QStringList msgHandlers;
  291. foreach(const QString& handlerName, d->RegisteredHandlers.keys())
  292. {
  293. if (d->RegisteredHandlers.value(handlerName)->enabled())
  294. {
  295. msgHandlers << handlerName;
  296. }
  297. }
  298. return msgHandlers;
  299. }
  300. //------------------------------------------------------------------------------
  301. void ctkErrorLogModel::setMsgHandlerEnabled(const QStringList& handlerNames)
  302. {
  303. foreach(const QString& handlerName, handlerNames)
  304. {
  305. this->setMsgHandlerEnabled(handlerName, true);
  306. }
  307. }
  308. //------------------------------------------------------------------------------
  309. void ctkErrorLogModel::enableAllMsgHandler()
  310. {
  311. this->setAllMsgHandlerEnabled(true);
  312. }
  313. //------------------------------------------------------------------------------
  314. void ctkErrorLogModel::disableAllMsgHandler()
  315. {
  316. this->setAllMsgHandlerEnabled(false);
  317. }
  318. //------------------------------------------------------------------------------
  319. void ctkErrorLogModel::setAllMsgHandlerEnabled(bool enabled)
  320. {
  321. Q_D(ctkErrorLogModel);
  322. foreach(const QString& msgHandlerName, d->RegisteredHandlers.keys())
  323. {
  324. this->setMsgHandlerEnabled(msgHandlerName, enabled);
  325. }
  326. }
  327. //------------------------------------------------------------------------------
  328. ctkErrorLogModel::TerminalOutputs ctkErrorLogModel::terminalOutputs()const
  329. {
  330. Q_D(const ctkErrorLogModel);
  331. ctkErrorLogModel::TerminalOutputs currentTerminalOutputs;
  332. currentTerminalOutputs |= d->StdErrTerminalOutput.enabled() ? Self::StandardError : Self::None;
  333. currentTerminalOutputs |= d->StdOutTerminalOutput.enabled() ? Self::StandardOutput : Self::None;
  334. return currentTerminalOutputs;
  335. }
  336. //------------------------------------------------------------------------------
  337. void ctkErrorLogModel::setTerminalOutputs(
  338. const ctkErrorLogModel::TerminalOutputs& terminalOutput)
  339. {
  340. Q_D(ctkErrorLogModel);
  341. d->StdErrTerminalOutput.setEnabled(terminalOutput & ctkErrorLogModel::StandardOutput);
  342. d->StdOutTerminalOutput.setEnabled(terminalOutput & ctkErrorLogModel::StandardError);
  343. }
  344. //------------------------------------------------------------------------------
  345. void ctkErrorLogModel::addEntry(const QDateTime& currentDateTime, const QString& threadId,
  346. ctkErrorLogLevel::LogLevel logLevel,
  347. const QString& origin, const QString& text)
  348. {
  349. Q_D(ctkErrorLogModel);
  350. // d->appendToFile("/tmp/ctkErrorLogModel-appendToFile.txt",
  351. // QString("addEntry: %1").arg(QThread::currentThreadId()));
  352. if (d->AddingEntry)
  353. {
  354. // QString str;
  355. // QTextStream s(&str);
  356. // s << "----------------------------------\n";
  357. // s << "text=>" << text << "\n";
  358. // s << "\tlogLevel:" << qPrintable(d->ErrorLogLevel(logLevel)) << "\n";
  359. // s << "\torigin:" << qPrintable(origin) << "\n";
  360. // d->appendToFile("/tmp/ctkErrorLogModel-AddingEntry-true.txt", str);
  361. return;
  362. }
  363. d->AddingEntry = true;
  364. QString timeFormat("dd.MM.yyyy hh:mm:ss");
  365. bool groupEntry = false;
  366. if (d->LogEntryGrouping)
  367. {
  368. int lastRowIndex = d->StandardItemModel.rowCount() - 1;
  369. QString lastRowThreadId = this->logEntryData(lastRowIndex, Self::ThreadIdColumn).toString();
  370. bool threadIdMatched = threadId == lastRowThreadId;
  371. QString lastRowLogLevel = this->logEntryData(lastRowIndex, Self::LogLevelColumn).toString();
  372. bool logLevelMatched = d->ErrorLogLevel(logLevel) == lastRowLogLevel;
  373. QString lastRowOrigin = this->logEntryData(lastRowIndex, Self::OriginColumn).toString();
  374. bool originMatched = origin == lastRowOrigin;
  375. QDateTime lastRowDateTime =
  376. QDateTime::fromString(this->logEntryData(lastRowIndex, Self::TimeColumn).toString(), timeFormat);
  377. int groupingIntervalInMsecs = 1000;
  378. bool withinGroupingInterval = lastRowDateTime.time().msecsTo(currentDateTime.time()) <= groupingIntervalInMsecs;
  379. groupEntry = threadIdMatched && logLevelMatched && originMatched && withinGroupingInterval;
  380. }
  381. if (!groupEntry)
  382. {
  383. QList<QStandardItem*> itemList;
  384. // Time item
  385. QStandardItem * timeItem = new QStandardItem(currentDateTime.toString(timeFormat));
  386. timeItem->setEditable(false);
  387. itemList << timeItem;
  388. // ThreadId item
  389. QStandardItem * threadIdItem = new QStandardItem(threadId);
  390. threadIdItem->setEditable(false);
  391. itemList << threadIdItem;
  392. // LogLevel item
  393. QStandardItem * logLevelItem = new QStandardItem(d->ErrorLogLevel(logLevel));
  394. logLevelItem->setEditable(false);
  395. itemList << logLevelItem;
  396. // Origin item
  397. QStandardItem * originItem = new QStandardItem(origin);
  398. originItem->setEditable(false);
  399. itemList << originItem;
  400. // Description item
  401. QStandardItem * descriptionItem = new QStandardItem();
  402. QString descriptionText(text);
  403. descriptionItem->setData(descriptionText.left(160).append((descriptionText.size() > 160) ? "..." : ""), Qt::DisplayRole);
  404. descriptionItem->setData(descriptionText, ctkErrorLogModel::DescriptionTextRole);
  405. descriptionItem->setEditable(false);
  406. itemList << descriptionItem;
  407. d->StandardItemModel.invisibleRootItem()->appendRow(itemList);
  408. }
  409. else
  410. {
  411. // Retrieve description associated with last row
  412. QModelIndex lastRowDescriptionIndex =
  413. d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::DescriptionColumn);
  414. QStringList updatedDescription;
  415. updatedDescription << lastRowDescriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  416. updatedDescription << text;
  417. d->StandardItemModel.setData(lastRowDescriptionIndex, updatedDescription.join("\n"),
  418. ctkErrorLogModel::DescriptionTextRole);
  419. // Append '...' to displayText if needed
  420. QString displayText = lastRowDescriptionIndex.data().toString();
  421. if (!displayText.endsWith("..."))
  422. {
  423. d->StandardItemModel.setData(lastRowDescriptionIndex, displayText.append("..."), Qt::DisplayRole);
  424. }
  425. }
  426. d->AddingEntry = false;
  427. emit this->entryAdded(logLevel);
  428. }
  429. //------------------------------------------------------------------------------
  430. void ctkErrorLogModel::clear()
  431. {
  432. Q_D(ctkErrorLogModel);
  433. d->StandardItemModel.invisibleRootItem()->removeRows(0, d->StandardItemModel.rowCount());
  434. }
  435. //------------------------------------------------------------------------------
  436. void ctkErrorLogModel::filterEntry(const ctkErrorLogLevel::LogLevels& logLevel,
  437. bool disableFilter)
  438. {
  439. Q_D(ctkErrorLogModel);
  440. QStringList patterns;
  441. if (!this->filterRegExp().pattern().isEmpty())
  442. {
  443. patterns << this->filterRegExp().pattern().split("|");
  444. }
  445. patterns.removeAll(d->ErrorLogLevel(ctkErrorLogLevel::None));
  446. // foreach(QString s, patterns)
  447. // {
  448. // std::cout << "pattern:" << qPrintable(s) << std::endl;
  449. // }
  450. QMetaEnum logLevelEnum = d->ErrorLogLevel.metaObject()->enumerator(0);
  451. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  452. // Loop over enum values and append associated name to 'patterns' if
  453. // it has been specified within 'logLevel'
  454. for (int i = 1; i < logLevelEnum.keyCount(); ++i)
  455. {
  456. int aLogLevel = logLevelEnum.value(i);
  457. if (logLevel & aLogLevel)
  458. {
  459. QString logLevelAsString = d->ErrorLogLevel(static_cast<ctkErrorLogLevel::LogLevel>(aLogLevel));
  460. if (!disableFilter)
  461. {
  462. patterns << logLevelAsString;
  463. d->CurrentLogLevelFilter |= static_cast<ctkErrorLogLevel::LogLevels>(aLogLevel);
  464. }
  465. else
  466. {
  467. patterns.removeAll(logLevelAsString);
  468. d->CurrentLogLevelFilter ^= static_cast<ctkErrorLogLevel::LogLevels>(aLogLevel);
  469. }
  470. }
  471. }
  472. if (patterns.isEmpty())
  473. {
  474. // If there are no patterns, let's filter with the None level so that
  475. // all entries are filtered out.
  476. patterns << d->ErrorLogLevel(ctkErrorLogLevel::None);
  477. }
  478. bool filterChanged = true;
  479. QStringList currentPatterns = this->filterRegExp().pattern().split("|");
  480. if (currentPatterns.count() == patterns.count())
  481. {
  482. foreach(const QString& p, patterns)
  483. {
  484. currentPatterns.removeAll(p);
  485. }
  486. filterChanged = currentPatterns.count() > 0;
  487. }
  488. this->setFilterRegExp(patterns.join("|"));
  489. if (filterChanged)
  490. {
  491. emit this->logLevelFilterChanged();
  492. }
  493. }
  494. //------------------------------------------------------------------------------
  495. ctkErrorLogLevel::LogLevels ctkErrorLogModel::logLevelFilter()const
  496. {
  497. Q_D(const ctkErrorLogModel);
  498. QMetaEnum logLevelEnum = d->ErrorLogLevel.metaObject()->enumerator(0);
  499. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  500. ctkErrorLogLevel::LogLevels filter = ctkErrorLogLevel::Unknown;
  501. foreach(const QString& filterAsString, this->filterRegExp().pattern().split("|"))
  502. {
  503. filter |= static_cast<ctkErrorLogLevel::LogLevels>(logLevelEnum.keyToValue(filterAsString.toLatin1()));
  504. }
  505. return filter;
  506. }
  507. //------------------------------------------------------------------------------
  508. bool ctkErrorLogModel::logEntryGrouping()const
  509. {
  510. Q_D(const ctkErrorLogModel);
  511. return d->LogEntryGrouping;
  512. }
  513. //------------------------------------------------------------------------------
  514. void ctkErrorLogModel::setLogEntryGrouping(bool value)
  515. {
  516. Q_D(ctkErrorLogModel);
  517. d->LogEntryGrouping = value;
  518. }
  519. //------------------------------------------------------------------------------
  520. bool ctkErrorLogModel::asynchronousLogging()const
  521. {
  522. Q_D(const ctkErrorLogModel);
  523. return d->AsynchronousLogging;
  524. }
  525. //------------------------------------------------------------------------------
  526. void ctkErrorLogModel::setAsynchronousLogging(bool value)
  527. {
  528. Q_D(ctkErrorLogModel);
  529. if (d->AsynchronousLogging == value)
  530. {
  531. return;
  532. }
  533. foreach(const QString& handlerName, d->RegisteredHandlers.keys())
  534. {
  535. d->setMessageHandlerConnection(
  536. d->RegisteredHandlers.value(handlerName), value);
  537. }
  538. d->AsynchronousLogging = value;
  539. }
  540. // --------------------------------------------------------------------------
  541. QVariant ctkErrorLogModel::logEntryData(int row, int column, int role) const
  542. {
  543. Q_D(const ctkErrorLogModel);
  544. if (column < 0 || column > Self::MaxColumn
  545. || row < 0 || row > this->logEntryCount())
  546. {
  547. return QVariant();
  548. }
  549. QModelIndex rowDescriptionIndex = d->StandardItemModel.index(row, column);
  550. return rowDescriptionIndex.data(role);
  551. }
  552. // --------------------------------------------------------------------------
  553. QString ctkErrorLogModel::logEntryDescription(int row) const
  554. {
  555. return this->logEntryData(row, Self::DescriptionColumn, Self::DescriptionTextRole).toString();
  556. }
  557. // --------------------------------------------------------------------------
  558. int ctkErrorLogModel::logEntryCount()const
  559. {
  560. Q_D(const ctkErrorLogModel);
  561. return d->StandardItemModel.rowCount();
  562. }
  563. // --------------------------------------------------------------------------
  564. // ctkErrorLogAbstractMessageHandlerPrivate
  565. // --------------------------------------------------------------------------
  566. class ctkErrorLogAbstractMessageHandlerPrivate
  567. {
  568. public:
  569. ctkErrorLogAbstractMessageHandlerPrivate();
  570. ~ctkErrorLogAbstractMessageHandlerPrivate();
  571. bool Enabled;
  572. QString HandlerPrettyName;
  573. // Use "int" instead of "ctkErrorLogModel::TerminalOutput" to avoid compilation warning ...
  574. // qhash.h:879: warning: passing ‘ctkErrorLogModel::TerminalOutput’ chooses ‘int’ over ‘uint’ [-Wsign-promo]
  575. QHash<int, ctkErrorLogTerminalOutput*> TerminalOutputs;
  576. };
  577. // --------------------------------------------------------------------------
  578. ctkErrorLogAbstractMessageHandlerPrivate::
  579. ctkErrorLogAbstractMessageHandlerPrivate()
  580. : Enabled(false)
  581. {
  582. }
  583. // --------------------------------------------------------------------------
  584. ctkErrorLogAbstractMessageHandlerPrivate::~ctkErrorLogAbstractMessageHandlerPrivate()
  585. {
  586. }
  587. // --------------------------------------------------------------------------
  588. // ctkErrorLogAbstractMessageHandlerPrivate methods
  589. // --------------------------------------------------------------------------
  590. ctkErrorLogAbstractMessageHandler::ctkErrorLogAbstractMessageHandler()
  591. : Superclass(), d_ptr(new ctkErrorLogAbstractMessageHandlerPrivate)
  592. {
  593. }
  594. // --------------------------------------------------------------------------
  595. ctkErrorLogAbstractMessageHandler::~ctkErrorLogAbstractMessageHandler()
  596. {
  597. }
  598. // --------------------------------------------------------------------------
  599. QString ctkErrorLogAbstractMessageHandler::handlerPrettyName()const
  600. {
  601. Q_D(const ctkErrorLogAbstractMessageHandler);
  602. if (d->HandlerPrettyName.isEmpty())
  603. {
  604. return this->handlerName();
  605. }
  606. else
  607. {
  608. return d->HandlerPrettyName;
  609. }
  610. }
  611. // --------------------------------------------------------------------------
  612. void ctkErrorLogAbstractMessageHandler::setHandlerPrettyName(const QString& newHandlerPrettyName)
  613. {
  614. Q_D(ctkErrorLogAbstractMessageHandler);
  615. d->HandlerPrettyName = newHandlerPrettyName;
  616. }
  617. // --------------------------------------------------------------------------
  618. bool ctkErrorLogAbstractMessageHandler::enabled()const
  619. {
  620. Q_D(const ctkErrorLogAbstractMessageHandler);
  621. return d->Enabled;
  622. }
  623. // --------------------------------------------------------------------------
  624. void ctkErrorLogAbstractMessageHandler::setEnabled(bool value)
  625. {
  626. Q_D(ctkErrorLogAbstractMessageHandler);
  627. if (value == d->Enabled)
  628. {
  629. return;
  630. }
  631. this->setEnabledInternal(value);
  632. d->Enabled = value;
  633. }
  634. // --------------------------------------------------------------------------
  635. void ctkErrorLogAbstractMessageHandler::handleMessage(const QString& threadId,
  636. ctkErrorLogLevel::LogLevel logLevel,
  637. const QString& origin, const QString& text)
  638. {
  639. Q_D(ctkErrorLogAbstractMessageHandler);
  640. if (logLevel <= ctkErrorLogLevel::Info)
  641. {
  642. if(d->TerminalOutputs.contains(ctkErrorLogModel::StandardOutput))
  643. {
  644. d->TerminalOutputs.value(ctkErrorLogModel::StandardOutput)->output(text);
  645. }
  646. }
  647. else
  648. {
  649. if(d->TerminalOutputs.contains(ctkErrorLogModel::StandardError))
  650. {
  651. d->TerminalOutputs.value(ctkErrorLogModel::StandardError)->output(text);
  652. }
  653. }
  654. emit this->messageHandled(QDateTime::currentDateTime(), threadId, logLevel, origin, text);
  655. }
  656. // --------------------------------------------------------------------------
  657. ctkErrorLogTerminalOutput* ctkErrorLogAbstractMessageHandler::terminalOutput(
  658. ctkErrorLogModel::TerminalOutput terminalOutputType)const
  659. {
  660. Q_D(const ctkErrorLogAbstractMessageHandler);
  661. if(d->TerminalOutputs.contains(terminalOutputType))
  662. {
  663. return d->TerminalOutputs.value(terminalOutputType);
  664. }
  665. return 0;
  666. }
  667. // --------------------------------------------------------------------------
  668. void ctkErrorLogAbstractMessageHandler::setTerminalOutput(
  669. ctkErrorLogModel::TerminalOutput terminalOutputType, ctkErrorLogTerminalOutput* terminalOutput)
  670. {
  671. Q_D(ctkErrorLogAbstractMessageHandler);
  672. d->TerminalOutputs.insert(terminalOutputType, terminalOutput);
  673. }