ctkErrorLogWidgetTest1.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <QDebug>
  17. #include <QMainWindow>
  18. #include <QStandardItemModel>
  19. #include <QStatusBar>
  20. #include <QTime>
  21. #include <QTimer>
  22. // CTK includes
  23. #include <ctkErrorLogQtMessageHandler.h>
  24. #include <ctkErrorLogStatusMessageHandler.h>
  25. #include <ctkErrorLogStreamMessageHandler.h>
  26. #include <ctkErrorLogWidget.h>
  27. // STD includes
  28. #include <cstdlib>
  29. #include <iostream>
  30. // Helper functions
  31. #include "Testing/Cpp/ctkErrorLogModelTestHelper.cpp"
  32. //-----------------------------------------------------------------------------
  33. int ctkErrorLogWidgetTest1(int argc, char * argv [])
  34. {
  35. QApplication app(argc, argv);
  36. QMainWindow mainWindow;
  37. mainWindow.show();
  38. QString errorMsg;
  39. ctkErrorLogModel model;
  40. ctkErrorLogWidget widget;
  41. // --------------------------------------------------------------------------
  42. // Monitor application StatusBar messages
  43. {
  44. model.registerMsgHandler(new ctkErrorLogStatusMessageHandler(&mainWindow));
  45. model.setMsgHandlerEnabled(ctkErrorLogStatusMessageHandler::HandlerName, true);
  46. QString expectedStatusText1 = QLatin1String("This is a status message");
  47. mainWindow.statusBar()->showMessage(expectedStatusText1);
  48. // Give enough time to the ErrorLogModel to consider the queued messages.
  49. processEvents(1000);
  50. QStringList expectedStatusMessages;
  51. expectedStatusMessages << expectedStatusText1;
  52. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedStatusMessages.count());
  53. if (!errorMsg.isEmpty())
  54. {
  55. model.disableAllMsgHandler();
  56. printErrorMessage(errorMsg);
  57. printTextMessages(model);
  58. return EXIT_FAILURE;
  59. }
  60. errorMsg = checkTextMessages(__LINE__, model, expectedStatusMessages);
  61. if (!errorMsg.isEmpty())
  62. {
  63. model.disableAllMsgHandler();
  64. printErrorMessage(errorMsg);
  65. printTextMessages(model);
  66. return EXIT_FAILURE;
  67. }
  68. // Clear
  69. model.clear();
  70. // Disable Qt messages monitoring
  71. model.setMsgHandlerEnabled(ctkErrorLogStatusMessageHandler::HandlerName, false);
  72. mainWindow.statusBar()->showMessage(
  73. QLatin1String("This is status message shouldn't appear in ErrorLogModel"));
  74. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
  75. if (!errorMsg.isEmpty())
  76. {
  77. model.disableAllMsgHandler();
  78. printErrorMessage(errorMsg);
  79. printTextMessages(model);
  80. return EXIT_FAILURE;
  81. }
  82. }
  83. // --------------------------------------------------------------------------
  84. // Monitor Stream messages
  85. {
  86. model.registerMsgHandler(new ctkErrorLogStreamMessageHandler);
  87. model.setMsgHandlerEnabled(ctkErrorLogStreamMessageHandler::HandlerName, true);
  88. std::cout << "This is a Cout message" << std::endl;
  89. std::cerr << "This is a Cerr message" << std::endl;
  90. }
  91. // --------------------------------------------------------------------------
  92. // Monitor Qt messages
  93. {
  94. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  95. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  96. qDebug() << "This is a Qt Debug message";
  97. qWarning() << "This is a Qt Warning message";
  98. qCritical() << "This is a Qt Critical message";
  99. }
  100. QTime start = QTime::currentTime();
  101. for (int i = 0; i < 500; ++i)
  102. {
  103. qDebug() << "This is a Qt Debug message - id:0 - group:" << i;
  104. std::cout << "This is a Cout message - id:1 - group:" << i << std::endl;
  105. qWarning() << "This is a Qt Warning message - id:2 - group:" << i;
  106. std::cerr << "This is a Cerr message - id:3 - group:" << i << std::endl;
  107. qCritical() << "This is a Qt Critical message - id:4 - group:" << i;
  108. qDebug() << "This is a Qt Debug message - id:5 - group:" << i;
  109. std::cout << "This is a Cout message - id:6 - group:" << i << std::endl;
  110. qWarning() << "This is a Qt Warning message - id:7 - group:" << i;
  111. std::cerr << "This is a Cerr message - id:8 - group:" << i << std::endl;
  112. qCritical() << "This is a Qt Critical message - id:9 - group:" << i;
  113. }
  114. fprintf(stdout, "Msg handling time: %d\n", start.msecsTo(QTime::currentTime()));
  115. fflush(stdout);
  116. model.setLogEntryGrouping(true);
  117. start = QTime::currentTime();
  118. for (int i = 0; i < 1000; ++i)
  119. {
  120. std::cout << "This is a Cout message - id:1 - group:" << i << std::endl;
  121. std::cout << "This is a Cout message - id:2 - group:" << i << std::endl;
  122. std::cout << "This is a Cout message - id:3 - group:" << i << std::endl;
  123. std::cout << "This is a Cout message - id:4 - group:" << i << std::endl;
  124. }
  125. fprintf(stdout, "Msg handling time: %d\n", start.msecsTo(QTime::currentTime()));
  126. fflush(stdout);
  127. widget.setErrorLogModel(&model);
  128. widget.setErrorLogModel(0);
  129. widget.setErrorLogModel(&model);
  130. widget.setErrorLogModel(&model);
  131. widget.show();
  132. if (argc < 2 || QString(argv[1]) != "-I" )
  133. {
  134. QTimer::singleShot(1000, &app, SLOT(quit()));
  135. }
  136. return app.exec();
  137. }