ctkErrorLogQtMessageHandlerWithThreadsTest1.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <QCoreApplication>
  16. #include <QDebug>
  17. // CTK includes
  18. #include "ctkErrorLogQtMessageHandler.h"
  19. #include "ctkModelTester.h"
  20. // STL includesQList
  21. #include <cstdlib>
  22. #include <iostream>
  23. // Helper functions
  24. #include "Testing/Cpp/ctkErrorLogModelTestHelper.cpp"
  25. namespace
  26. {
  27. //-----------------------------------------------------------------------------
  28. class LogQtMessageThread : public LogMessageThread
  29. {
  30. public:
  31. LogQtMessageThread(int id, int maxIteration) : LogMessageThread(id, maxIteration){}
  32. virtual void logMessage(const QDateTime& dateTime, int threadId, int counterIdx)
  33. {
  34. QString msg = QString("counterIdx:%1 - %2 - Message from thread: %3\n")
  35. .arg(counterIdx).arg(dateTime.toString()).arg(threadId);
  36. qDebug().nospace() << qPrintable(msg);
  37. qWarning().nospace() << qPrintable(msg);
  38. qCritical().nospace() << qPrintable(msg);
  39. }
  40. };
  41. }
  42. //-----------------------------------------------------------------------------
  43. int ctkErrorLogQtMessageHandlerWithThreadsTest1(int argc, char * argv [])
  44. {
  45. QCoreApplication app(argc, argv);
  46. Q_UNUSED(app);
  47. ctkErrorLogModel model;
  48. ctkModelTester modelTester;
  49. modelTester.setVerbose(false);
  50. try
  51. {
  52. modelTester.setModel(&model);
  53. // --------------------------------------------------------------------------
  54. // Monitor Qt messages
  55. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  56. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  57. int threadCount = 10;
  58. int maxIteration = 5;
  59. int messagesPerIteration = 3;
  60. startLogMessageThreads<LogQtMessageThread>(threadCount, maxIteration);
  61. // Give enough time for the threads to send their messages
  62. QTimer::singleShot(1500, qApp, SLOT(quit()));
  63. app.exec();
  64. int expectedMessageCount = threadCount * maxIteration * messagesPerIteration;
  65. QString errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedMessageCount);
  66. if (!errorMsg.isEmpty())
  67. {
  68. model.disableAllMsgHandler();
  69. printErrorMessage(errorMsg);
  70. printTextMessages(model);
  71. return EXIT_FAILURE;
  72. }
  73. }
  74. catch (const char* error)
  75. {
  76. model.disableAllMsgHandler();
  77. std::cerr << error << std::endl;
  78. return EXIT_FAILURE;
  79. }
  80. return EXIT_SUCCESS;
  81. }