ctkErrorLogWidgetTest1.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <QDebug>
  17. #include <QMainWindow>
  18. #include <QStandardItemModel>
  19. #include <QStatusBar>
  20. #include <QTime>
  21. #include <QTimer>
  22. // CTK includes
  23. #include <ctkErrorLogModel.h>
  24. #include <ctkErrorLogQtMessageHandler.h>
  25. #include <ctkErrorLogStatusMessageHandler.h>
  26. #include <ctkErrorLogStreamMessageHandler.h>
  27. #include <ctkErrorLogWidget.h>
  28. // STD includes
  29. #include <cstdlib>
  30. #include <iostream>
  31. namespace
  32. {
  33. //-----------------------------------------------------------------------------
  34. // Utility function
  35. //-----------------------------------------------------------------------------
  36. QString checkRowCount(int line, int currentRowCount, int expectedRowCount)
  37. {
  38. if (currentRowCount != expectedRowCount)
  39. {
  40. QString errorMsg("Line %1 - Expected rowCount: %2 - Current rowCount: %3\n");
  41. return errorMsg.arg(line).arg(expectedRowCount).arg(currentRowCount);
  42. }
  43. return QString();
  44. }
  45. //-----------------------------------------------------------------------------
  46. QString checkTextMessages(int line, const ctkErrorLogModel& model, const QStringList& expectedMessages)
  47. {
  48. for(int i=0; i < expectedMessages.count(); ++i)
  49. {
  50. QModelIndex descriptionIndex = model.index(i, ctkErrorLogModel::DescriptionColumn);
  51. QString currentMessage = descriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  52. if (currentMessage.compare(expectedMessages.value(i)) != 0)
  53. {
  54. QString errorMsg("Line %1 - Problem with row%2 !\n"
  55. "\tExpected message [%3]\n"
  56. "\tCurrent message [%4]\n");
  57. return errorMsg.arg(line).arg(i).arg(expectedMessages.value(i)).arg(currentMessage);
  58. }
  59. }
  60. return QString();
  61. }
  62. //-----------------------------------------------------------------------------
  63. void printTextMessages(const ctkErrorLogModel& model)
  64. {
  65. fprintf(stdout, "%s", "ErrorLogModel rows:\n");
  66. QString text("\trow %1 => %2\n");
  67. for (int i=0; i < model.rowCount(); ++i)
  68. {
  69. QString description =
  70. model.index(0, ctkErrorLogModel::DescriptionColumn).data().toString();
  71. fprintf(stdout, "%s", qPrintable(text.arg(i).arg(description)));
  72. }
  73. fflush(stdout);
  74. }
  75. //-----------------------------------------------------------------------------
  76. void printErrorMessage(const QString& errorMessage)
  77. {
  78. fprintf(stderr, "%s", qPrintable(errorMessage));
  79. fflush(stderr);
  80. }
  81. } // end namespace
  82. //-----------------------------------------------------------------------------
  83. int ctkErrorLogWidgetTest1(int argc, char * argv [])
  84. {
  85. QApplication app(argc, argv);
  86. QMainWindow mainWindow;
  87. mainWindow.show();
  88. QString errorMsg;
  89. ctkErrorLogModel model;
  90. ctkErrorLogWidget widget;
  91. // --------------------------------------------------------------------------
  92. // Monitor application StatusBar messages
  93. {
  94. model.registerMsgHandler(new ctkErrorLogStatusMessageHandler(&mainWindow));
  95. model.setMsgHandlerEnabled(ctkErrorLogStatusMessageHandler::HandlerName, true);
  96. QString expectedStatusText1 = QLatin1String("This is a status message");
  97. mainWindow.statusBar()->showMessage(expectedStatusText1);
  98. QStringList expectedStatusMessages;
  99. expectedStatusMessages << expectedStatusText1;
  100. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedStatusMessages.count());
  101. if (!errorMsg.isEmpty())
  102. {
  103. model.disableAllMsgHandler();
  104. printErrorMessage(errorMsg);
  105. printTextMessages(model);
  106. return EXIT_FAILURE;
  107. }
  108. errorMsg = checkTextMessages(__LINE__, model, expectedStatusMessages);
  109. if (!errorMsg.isEmpty())
  110. {
  111. model.disableAllMsgHandler();
  112. printErrorMessage(errorMsg);
  113. return EXIT_FAILURE;
  114. }
  115. // Clear
  116. model.clear();
  117. // Disable Qt messages monitoring
  118. model.setMsgHandlerEnabled(ctkErrorLogStatusMessageHandler::HandlerName, false);
  119. mainWindow.statusBar()->showMessage(
  120. QLatin1String("This is status message shouldn't appear in ErrorLogModel"));
  121. errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ 0);
  122. if (!errorMsg.isEmpty())
  123. {
  124. model.disableAllMsgHandler();
  125. printErrorMessage(errorMsg);
  126. printTextMessages(model);
  127. return EXIT_FAILURE;
  128. }
  129. }
  130. // --------------------------------------------------------------------------
  131. // Monitor Stream messages
  132. {
  133. model.registerMsgHandler(new ctkErrorLogStreamMessageHandler);
  134. model.setMsgHandlerEnabled(ctkErrorLogStreamMessageHandler::HandlerName, true);
  135. std::cout << "This is a Cout message" << std::endl;
  136. std::cerr << "This is a Cerr message" << std::endl;
  137. }
  138. // --------------------------------------------------------------------------
  139. // Monitor Qt messages
  140. {
  141. model.registerMsgHandler(new ctkErrorLogQtMessageHandler);
  142. model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
  143. qDebug() << "This is a Qt Debug message";
  144. qWarning() << "This is a Qt Warning message";
  145. qCritical() << "This is a Qt Critical message";
  146. }
  147. QTime start = QTime::currentTime();
  148. for (int i = 0; i < 500; ++i)
  149. {
  150. qDebug() << "This is a Qt Debug message - id:0 - group:" << i;
  151. std::cout << "This is a Cout message - id:1 - group:" << i << std::endl;
  152. qWarning() << "This is a Qt Warning message - id:2 - group:" << i;
  153. std::cerr << "This is a Cerr message - id:3 - group:" << i << std::endl;
  154. qCritical() << "This is a Qt Critical message - id:4 - group:" << i;
  155. qDebug() << "This is a Qt Debug message - id:5 - group:" << i;
  156. std::cout << "This is a Cout message - id:6 - group:" << i << std::endl;
  157. qWarning() << "This is a Qt Warning message - id:7 - group:" << i;
  158. std::cerr << "This is a Cerr message - id:8 - group:" << i << std::endl;
  159. qCritical() << "This is a Qt Critical message - id:9 - group:" << i;
  160. }
  161. fprintf(stdout, "Msg handling time: %d\n", start.msecsTo(QTime::currentTime()));
  162. fflush(stdout);
  163. model.setLogEntryGrouping(true);
  164. start = QTime::currentTime();
  165. for (int i = 0; i < 1000; ++i)
  166. {
  167. std::cout << "This is a Cout message - id:1 - group:" << i << std::endl;
  168. std::cout << "This is a Cout message - id:2 - group:" << i << std::endl;
  169. std::cout << "This is a Cout message - id:3 - group:" << i << std::endl;
  170. std::cout << "This is a Cout message - id:4 - group:" << i << std::endl;
  171. }
  172. fprintf(stdout, "Msg handling time: %d\n", start.msecsTo(QTime::currentTime()));
  173. fflush(stdout);
  174. widget.setErrorLogModel(&model);
  175. widget.setErrorLogModel(0);
  176. widget.setErrorLogModel(&model);
  177. widget.setErrorLogModel(&model);
  178. widget.show();
  179. if (argc < 2 || QString(argv[1]) != "-I" )
  180. {
  181. QTimer::singleShot(200, &app, SLOT(quit()));
  182. }
  183. return app.exec();
  184. }