ctkErrorLogModel.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 ctkErrorLogLevel::logLevelAsString(logLevel);
  47. }
  48. // --------------------------------------------------------------------------
  49. QString ctkErrorLogLevel::logLevelAsString(ctkErrorLogLevel::LogLevel logLevel)
  50. {
  51. QMetaEnum logLevelEnum = ctkErrorLogLevel::staticMetaObject.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->StandardItemModel.setColumnCount(ctkErrorLogModel::MaxColumn);
  179. this->LogEntryGrouping = false;
  180. this->AsynchronousLogging = true;
  181. this->AddingEntry = false;
  182. }
  183. // --------------------------------------------------------------------------
  184. ctkErrorLogModelPrivate::~ctkErrorLogModelPrivate()
  185. {
  186. foreach(const QString& handlerName, this->RegisteredHandlers.keys())
  187. {
  188. ctkErrorLogAbstractMessageHandler * msgHandler =
  189. this->RegisteredHandlers.value(handlerName);
  190. Q_ASSERT(msgHandler);
  191. msgHandler->setEnabled(false);
  192. delete msgHandler;
  193. }
  194. }
  195. // --------------------------------------------------------------------------
  196. void ctkErrorLogModelPrivate::init()
  197. {
  198. Q_Q(ctkErrorLogModel);
  199. q->setDynamicSortFilter(true);
  200. //
  201. // WARNING - Using a QSortFilterProxyModel slows down the insertion of rows by a factor 10
  202. //
  203. q->setSourceModel(&this->StandardItemModel);
  204. q->setFilterKeyColumn(ctkErrorLogModel::LogLevelColumn);
  205. }
  206. // --------------------------------------------------------------------------
  207. void ctkErrorLogModelPrivate::appendToFile(const QString& fileName, const QString& text)
  208. {
  209. QMutexLocker locker(&this->AppendToFileMutex);
  210. QFile f(fileName);
  211. f.open(QFile::Append);
  212. QTextStream s(&f);
  213. s << QDateTime::currentDateTime().toString() << " - " << text << "\n";
  214. f.close();
  215. }
  216. // --------------------------------------------------------------------------
  217. void ctkErrorLogModelPrivate::setMessageHandlerConnection(
  218. ctkErrorLogAbstractMessageHandler * msgHandler, bool asynchronous)
  219. {
  220. Q_Q(ctkErrorLogModel);
  221. msgHandler->disconnect();
  222. QObject::connect(msgHandler,
  223. SIGNAL(messageHandled(QDateTime,QString,ctkErrorLogLevel::LogLevel,QString,QString)),
  224. q, SLOT(addEntry(QDateTime,QString,ctkErrorLogLevel::LogLevel,QString,QString)),
  225. asynchronous ? Qt::QueuedConnection : Qt::BlockingQueuedConnection);
  226. }
  227. // --------------------------------------------------------------------------
  228. // ctkErrorLogModel methods
  229. //------------------------------------------------------------------------------
  230. ctkErrorLogModel::ctkErrorLogModel(QObject * parentObject)
  231. : Superclass(parentObject)
  232. , d_ptr(new ctkErrorLogModelPrivate(*this))
  233. {
  234. Q_D(ctkErrorLogModel);
  235. d->init();
  236. }
  237. //------------------------------------------------------------------------------
  238. ctkErrorLogModel::~ctkErrorLogModel()
  239. {
  240. }
  241. //------------------------------------------------------------------------------
  242. bool ctkErrorLogModel::registerMsgHandler(ctkErrorLogAbstractMessageHandler * msgHandler)
  243. {
  244. Q_D(ctkErrorLogModel);
  245. if (!msgHandler)
  246. {
  247. return false;
  248. }
  249. if (d->RegisteredHandlers.keys().contains(msgHandler->handlerName()))
  250. {
  251. return false;
  252. }
  253. d->setMessageHandlerConnection(msgHandler, d->AsynchronousLogging);
  254. msgHandler->setTerminalOutput(Self::StandardError, &d->StdErrTerminalOutput);
  255. msgHandler->setTerminalOutput(Self::StandardOutput, &d->StdOutTerminalOutput);
  256. d->RegisteredHandlers.insert(msgHandler->handlerName(), msgHandler);
  257. return true;
  258. }
  259. //------------------------------------------------------------------------------
  260. QStringList ctkErrorLogModel::msgHandlerNames()const
  261. {
  262. Q_D(const ctkErrorLogModel);
  263. return d->RegisteredHandlers.keys();
  264. }
  265. //------------------------------------------------------------------------------
  266. bool ctkErrorLogModel::msgHandlerEnabled(const QString& handlerName) const
  267. {
  268. Q_D(const ctkErrorLogModel);
  269. if (!d->RegisteredHandlers.keys().contains(handlerName))
  270. {
  271. return false;
  272. }
  273. return d->RegisteredHandlers.value(handlerName)->enabled();
  274. }
  275. //------------------------------------------------------------------------------
  276. void ctkErrorLogModel::setMsgHandlerEnabled(const QString& handlerName, bool enabled)
  277. {
  278. Q_D(ctkErrorLogModel);
  279. if (!d->RegisteredHandlers.keys().contains(handlerName))
  280. {
  281. // qCritical() << "Failed to enable/disable message handler " << handlerName
  282. // << "- Handler not registered !";
  283. return;
  284. }
  285. d->RegisteredHandlers.value(handlerName)->setEnabled(enabled);
  286. }
  287. //------------------------------------------------------------------------------
  288. QStringList ctkErrorLogModel::msgHandlerEnabled() const
  289. {
  290. Q_D(const ctkErrorLogModel);
  291. QStringList msgHandlers;
  292. foreach(const QString& handlerName, d->RegisteredHandlers.keys())
  293. {
  294. if (d->RegisteredHandlers.value(handlerName)->enabled())
  295. {
  296. msgHandlers << handlerName;
  297. }
  298. }
  299. return msgHandlers;
  300. }
  301. //------------------------------------------------------------------------------
  302. void ctkErrorLogModel::setMsgHandlerEnabled(const QStringList& handlerNames)
  303. {
  304. foreach(const QString& handlerName, handlerNames)
  305. {
  306. this->setMsgHandlerEnabled(handlerName, true);
  307. }
  308. }
  309. //------------------------------------------------------------------------------
  310. void ctkErrorLogModel::enableAllMsgHandler()
  311. {
  312. this->setAllMsgHandlerEnabled(true);
  313. }
  314. //------------------------------------------------------------------------------
  315. void ctkErrorLogModel::disableAllMsgHandler()
  316. {
  317. this->setAllMsgHandlerEnabled(false);
  318. }
  319. //------------------------------------------------------------------------------
  320. void ctkErrorLogModel::setAllMsgHandlerEnabled(bool enabled)
  321. {
  322. Q_D(ctkErrorLogModel);
  323. foreach(const QString& msgHandlerName, d->RegisteredHandlers.keys())
  324. {
  325. this->setMsgHandlerEnabled(msgHandlerName, enabled);
  326. }
  327. }
  328. //------------------------------------------------------------------------------
  329. ctkErrorLogModel::TerminalOutputs ctkErrorLogModel::terminalOutputs()const
  330. {
  331. Q_D(const ctkErrorLogModel);
  332. ctkErrorLogModel::TerminalOutputs currentTerminalOutputs;
  333. currentTerminalOutputs |= d->StdErrTerminalOutput.enabled() ? Self::StandardError : Self::None;
  334. currentTerminalOutputs |= d->StdOutTerminalOutput.enabled() ? Self::StandardOutput : Self::None;
  335. return currentTerminalOutputs;
  336. }
  337. //------------------------------------------------------------------------------
  338. void ctkErrorLogModel::setTerminalOutputs(
  339. const ctkErrorLogModel::TerminalOutputs& terminalOutput)
  340. {
  341. Q_D(ctkErrorLogModel);
  342. d->StdErrTerminalOutput.setEnabled(terminalOutput & ctkErrorLogModel::StandardOutput);
  343. d->StdOutTerminalOutput.setEnabled(terminalOutput & ctkErrorLogModel::StandardError);
  344. }
  345. //------------------------------------------------------------------------------
  346. void ctkErrorLogModel::addEntry(const QDateTime& currentDateTime, const QString& threadId,
  347. ctkErrorLogLevel::LogLevel logLevel,
  348. const QString& origin, const QString& text)
  349. {
  350. Q_D(ctkErrorLogModel);
  351. // d->appendToFile("/tmp/ctkErrorLogModel-appendToFile.txt",
  352. // QString("addEntry: %1").arg(QThread::currentThreadId()));
  353. if (d->AddingEntry)
  354. {
  355. // QString str;
  356. // QTextStream s(&str);
  357. // s << "----------------------------------\n";
  358. // s << "text=>" << text << "\n";
  359. // s << "\tlogLevel:" << qPrintable(d->ErrorLogLevel(logLevel)) << "\n";
  360. // s << "\torigin:" << qPrintable(origin) << "\n";
  361. // d->appendToFile("/tmp/ctkErrorLogModel-AddingEntry-true.txt", str);
  362. return;
  363. }
  364. d->AddingEntry = true;
  365. QString timeFormat("dd.MM.yyyy hh:mm:ss");
  366. bool groupEntry = false;
  367. if (d->LogEntryGrouping)
  368. {
  369. int lastRowIndex = d->StandardItemModel.rowCount() - 1;
  370. QString lastRowThreadId = this->logEntryData(lastRowIndex, Self::ThreadIdColumn).toString();
  371. bool threadIdMatched = threadId == lastRowThreadId;
  372. QString lastRowLogLevel = this->logEntryData(lastRowIndex, Self::LogLevelColumn).toString();
  373. bool logLevelMatched = d->ErrorLogLevel(logLevel) == lastRowLogLevel;
  374. QString lastRowOrigin = this->logEntryData(lastRowIndex, Self::OriginColumn).toString();
  375. bool originMatched = origin == lastRowOrigin;
  376. QDateTime lastRowDateTime =
  377. QDateTime::fromString(this->logEntryData(lastRowIndex, Self::TimeColumn).toString(), timeFormat);
  378. int groupingIntervalInMsecs = 1000;
  379. bool withinGroupingInterval = lastRowDateTime.time().msecsTo(currentDateTime.time()) <= groupingIntervalInMsecs;
  380. groupEntry = threadIdMatched && logLevelMatched && originMatched && withinGroupingInterval;
  381. }
  382. if (!groupEntry)
  383. {
  384. QList<QStandardItem*> itemList;
  385. // Time item
  386. QStandardItem * timeItem = new QStandardItem(currentDateTime.toString(timeFormat));
  387. timeItem->setEditable(false);
  388. itemList << timeItem;
  389. // ThreadId item
  390. QStandardItem * threadIdItem = new QStandardItem(threadId);
  391. threadIdItem->setEditable(false);
  392. itemList << threadIdItem;
  393. // LogLevel item
  394. QStandardItem * logLevelItem = new QStandardItem(d->ErrorLogLevel(logLevel));
  395. logLevelItem->setEditable(false);
  396. itemList << logLevelItem;
  397. // Origin item
  398. QStandardItem * originItem = new QStandardItem(origin);
  399. originItem->setEditable(false);
  400. itemList << originItem;
  401. // Description item
  402. QStandardItem * descriptionItem = new QStandardItem();
  403. QString descriptionText(text);
  404. descriptionItem->setData(descriptionText.left(160).append((descriptionText.size() > 160) ? "..." : ""), Qt::DisplayRole);
  405. descriptionItem->setData(descriptionText, ctkErrorLogModel::DescriptionTextRole);
  406. descriptionItem->setEditable(false);
  407. itemList << descriptionItem;
  408. d->StandardItemModel.invisibleRootItem()->appendRow(itemList);
  409. }
  410. else
  411. {
  412. // Retrieve description associated with last row
  413. QModelIndex lastRowDescriptionIndex =
  414. d->StandardItemModel.index(d->StandardItemModel.rowCount() - 1, ctkErrorLogModel::DescriptionColumn);
  415. QStringList updatedDescription;
  416. updatedDescription << lastRowDescriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  417. updatedDescription << text;
  418. d->StandardItemModel.setData(lastRowDescriptionIndex, updatedDescription.join("\n"),
  419. ctkErrorLogModel::DescriptionTextRole);
  420. // Append '...' to displayText if needed
  421. QString displayText = lastRowDescriptionIndex.data().toString();
  422. if (!displayText.endsWith("..."))
  423. {
  424. d->StandardItemModel.setData(lastRowDescriptionIndex, displayText.append("..."), Qt::DisplayRole);
  425. }
  426. }
  427. d->AddingEntry = false;
  428. emit this->entryAdded(logLevel);
  429. }
  430. //------------------------------------------------------------------------------
  431. void ctkErrorLogModel::clear()
  432. {
  433. Q_D(ctkErrorLogModel);
  434. d->StandardItemModel.invisibleRootItem()->removeRows(0, d->StandardItemModel.rowCount());
  435. }
  436. //------------------------------------------------------------------------------
  437. void ctkErrorLogModel::filterEntry(const ctkErrorLogLevel::LogLevels& logLevel,
  438. bool disableFilter)
  439. {
  440. Q_D(ctkErrorLogModel);
  441. QStringList patterns;
  442. if (!this->filterRegExp().pattern().isEmpty())
  443. {
  444. patterns << this->filterRegExp().pattern().split("|");
  445. }
  446. patterns.removeAll(d->ErrorLogLevel(ctkErrorLogLevel::None));
  447. // foreach(QString s, patterns)
  448. // {
  449. // std::cout << "pattern:" << qPrintable(s) << std::endl;
  450. // }
  451. QMetaEnum logLevelEnum = d->ErrorLogLevel.metaObject()->enumerator(0);
  452. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  453. // Loop over enum values and append associated name to 'patterns' if
  454. // it has been specified within 'logLevel'
  455. for (int i = 1; i < logLevelEnum.keyCount(); ++i)
  456. {
  457. int aLogLevel = logLevelEnum.value(i);
  458. if (logLevel & aLogLevel)
  459. {
  460. QString logLevelAsString = d->ErrorLogLevel(static_cast<ctkErrorLogLevel::LogLevel>(aLogLevel));
  461. if (!disableFilter)
  462. {
  463. patterns << logLevelAsString;
  464. d->CurrentLogLevelFilter |= static_cast<ctkErrorLogLevel::LogLevels>(aLogLevel);
  465. }
  466. else
  467. {
  468. patterns.removeAll(logLevelAsString);
  469. d->CurrentLogLevelFilter ^= static_cast<ctkErrorLogLevel::LogLevels>(aLogLevel);
  470. }
  471. }
  472. }
  473. if (patterns.isEmpty())
  474. {
  475. // If there are no patterns, let's filter with the None level so that
  476. // all entries are filtered out.
  477. patterns << d->ErrorLogLevel(ctkErrorLogLevel::None);
  478. }
  479. bool filterChanged = true;
  480. QStringList currentPatterns = this->filterRegExp().pattern().split("|");
  481. if (currentPatterns.count() == patterns.count())
  482. {
  483. foreach(const QString& p, patterns)
  484. {
  485. currentPatterns.removeAll(p);
  486. }
  487. filterChanged = currentPatterns.count() > 0;
  488. }
  489. this->setFilterRegExp(patterns.join("|"));
  490. if (filterChanged)
  491. {
  492. emit this->logLevelFilterChanged();
  493. }
  494. }
  495. //------------------------------------------------------------------------------
  496. ctkErrorLogLevel::LogLevels ctkErrorLogModel::logLevelFilter()const
  497. {
  498. Q_D(const ctkErrorLogModel);
  499. QMetaEnum logLevelEnum = d->ErrorLogLevel.metaObject()->enumerator(0);
  500. Q_ASSERT(QString("LogLevel").compare(logLevelEnum.name()) == 0);
  501. ctkErrorLogLevel::LogLevels filter = ctkErrorLogLevel::Unknown;
  502. foreach(const QString& filterAsString, this->filterRegExp().pattern().split("|"))
  503. {
  504. filter |= static_cast<ctkErrorLogLevel::LogLevels>(logLevelEnum.keyToValue(filterAsString.toLatin1()));
  505. }
  506. return filter;
  507. }
  508. //------------------------------------------------------------------------------
  509. bool ctkErrorLogModel::logEntryGrouping()const
  510. {
  511. Q_D(const ctkErrorLogModel);
  512. return d->LogEntryGrouping;
  513. }
  514. //------------------------------------------------------------------------------
  515. void ctkErrorLogModel::setLogEntryGrouping(bool value)
  516. {
  517. Q_D(ctkErrorLogModel);
  518. d->LogEntryGrouping = value;
  519. }
  520. //------------------------------------------------------------------------------
  521. bool ctkErrorLogModel::asynchronousLogging()const
  522. {
  523. Q_D(const ctkErrorLogModel);
  524. return d->AsynchronousLogging;
  525. }
  526. //------------------------------------------------------------------------------
  527. void ctkErrorLogModel::setAsynchronousLogging(bool value)
  528. {
  529. Q_D(ctkErrorLogModel);
  530. if (d->AsynchronousLogging == value)
  531. {
  532. return;
  533. }
  534. foreach(const QString& handlerName, d->RegisteredHandlers.keys())
  535. {
  536. d->setMessageHandlerConnection(
  537. d->RegisteredHandlers.value(handlerName), value);
  538. }
  539. d->AsynchronousLogging = value;
  540. }
  541. // --------------------------------------------------------------------------
  542. QVariant ctkErrorLogModel::logEntryData(int row, int column, int role) const
  543. {
  544. Q_D(const ctkErrorLogModel);
  545. if (column < 0 || column > Self::MaxColumn
  546. || row < 0 || row > this->logEntryCount())
  547. {
  548. return QVariant();
  549. }
  550. QModelIndex rowDescriptionIndex = d->StandardItemModel.index(row, column);
  551. return rowDescriptionIndex.data(role);
  552. }
  553. // --------------------------------------------------------------------------
  554. QString ctkErrorLogModel::logEntryDescription(int row) const
  555. {
  556. return this->logEntryData(row, Self::DescriptionColumn, Self::DescriptionTextRole).toString();
  557. }
  558. // --------------------------------------------------------------------------
  559. int ctkErrorLogModel::logEntryCount()const
  560. {
  561. Q_D(const ctkErrorLogModel);
  562. return d->StandardItemModel.rowCount();
  563. }
  564. // --------------------------------------------------------------------------
  565. // ctkErrorLogAbstractMessageHandlerPrivate
  566. // --------------------------------------------------------------------------
  567. class ctkErrorLogAbstractMessageHandlerPrivate
  568. {
  569. public:
  570. ctkErrorLogAbstractMessageHandlerPrivate();
  571. ~ctkErrorLogAbstractMessageHandlerPrivate();
  572. bool Enabled;
  573. QString HandlerPrettyName;
  574. // Use "int" instead of "ctkErrorLogModel::TerminalOutput" to avoid compilation warning ...
  575. // qhash.h:879: warning: passing ‘ctkErrorLogModel::TerminalOutput’ chooses ‘int’ over ‘uint’ [-Wsign-promo]
  576. QHash<int, ctkErrorLogTerminalOutput*> TerminalOutputs;
  577. };
  578. // --------------------------------------------------------------------------
  579. ctkErrorLogAbstractMessageHandlerPrivate::
  580. ctkErrorLogAbstractMessageHandlerPrivate()
  581. : Enabled(false)
  582. {
  583. }
  584. // --------------------------------------------------------------------------
  585. ctkErrorLogAbstractMessageHandlerPrivate::~ctkErrorLogAbstractMessageHandlerPrivate()
  586. {
  587. }
  588. // --------------------------------------------------------------------------
  589. // ctkErrorLogAbstractMessageHandlerPrivate methods
  590. // --------------------------------------------------------------------------
  591. ctkErrorLogAbstractMessageHandler::ctkErrorLogAbstractMessageHandler()
  592. : Superclass(), d_ptr(new ctkErrorLogAbstractMessageHandlerPrivate)
  593. {
  594. }
  595. // --------------------------------------------------------------------------
  596. ctkErrorLogAbstractMessageHandler::~ctkErrorLogAbstractMessageHandler()
  597. {
  598. }
  599. // --------------------------------------------------------------------------
  600. QString ctkErrorLogAbstractMessageHandler::handlerPrettyName()const
  601. {
  602. Q_D(const ctkErrorLogAbstractMessageHandler);
  603. if (d->HandlerPrettyName.isEmpty())
  604. {
  605. return this->handlerName();
  606. }
  607. else
  608. {
  609. return d->HandlerPrettyName;
  610. }
  611. }
  612. // --------------------------------------------------------------------------
  613. void ctkErrorLogAbstractMessageHandler::setHandlerPrettyName(const QString& newHandlerPrettyName)
  614. {
  615. Q_D(ctkErrorLogAbstractMessageHandler);
  616. d->HandlerPrettyName = newHandlerPrettyName;
  617. }
  618. // --------------------------------------------------------------------------
  619. bool ctkErrorLogAbstractMessageHandler::enabled()const
  620. {
  621. Q_D(const ctkErrorLogAbstractMessageHandler);
  622. return d->Enabled;
  623. }
  624. // --------------------------------------------------------------------------
  625. void ctkErrorLogAbstractMessageHandler::setEnabled(bool value)
  626. {
  627. Q_D(ctkErrorLogAbstractMessageHandler);
  628. if (value == d->Enabled)
  629. {
  630. return;
  631. }
  632. this->setEnabledInternal(value);
  633. d->Enabled = value;
  634. }
  635. // --------------------------------------------------------------------------
  636. void ctkErrorLogAbstractMessageHandler::handleMessage(const QString& threadId,
  637. ctkErrorLogLevel::LogLevel logLevel,
  638. const QString& origin, const QString& text)
  639. {
  640. Q_D(ctkErrorLogAbstractMessageHandler);
  641. if (logLevel <= ctkErrorLogLevel::Info)
  642. {
  643. if(d->TerminalOutputs.contains(ctkErrorLogModel::StandardOutput))
  644. {
  645. d->TerminalOutputs.value(ctkErrorLogModel::StandardOutput)->output(text);
  646. }
  647. }
  648. else
  649. {
  650. if(d->TerminalOutputs.contains(ctkErrorLogModel::StandardError))
  651. {
  652. d->TerminalOutputs.value(ctkErrorLogModel::StandardError)->output(text);
  653. }
  654. }
  655. emit this->messageHandled(QDateTime::currentDateTime(), threadId, logLevel, origin, text);
  656. }
  657. // --------------------------------------------------------------------------
  658. ctkErrorLogTerminalOutput* ctkErrorLogAbstractMessageHandler::terminalOutput(
  659. ctkErrorLogModel::TerminalOutput terminalOutputType)const
  660. {
  661. Q_D(const ctkErrorLogAbstractMessageHandler);
  662. if(d->TerminalOutputs.contains(terminalOutputType))
  663. {
  664. return d->TerminalOutputs.value(terminalOutputType);
  665. }
  666. return 0;
  667. }
  668. // --------------------------------------------------------------------------
  669. void ctkErrorLogAbstractMessageHandler::setTerminalOutput(
  670. ctkErrorLogModel::TerminalOutput terminalOutputType, ctkErrorLogTerminalOutput* terminalOutput)
  671. {
  672. Q_D(ctkErrorLogAbstractMessageHandler);
  673. d->TerminalOutputs.insert(terminalOutputType, terminalOutput);
  674. }