ctkVTKErrorLogModelFileLoggingTest1.cpp 3.4 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. #include <QDir>
  18. #include <QTemporaryFile>
  19. // CTK includes
  20. #include "ctkVTKErrorLogMessageHandler.h"
  21. // VTK includes
  22. #include <vtkNew.h>
  23. #include <vtkObject.h>
  24. #include <vtkOutputWindow.h>
  25. // STL includes
  26. #include <cstdlib>
  27. // Helper functions
  28. #include "Testing/Cpp/ctkErrorLogModelTestHelper.cpp"
  29. //-----------------------------------------------------------------------------
  30. int ctkVTKErrorLogModelFileLoggingTest1(int argc, char * argv [])
  31. {
  32. QCoreApplication app(argc, argv);
  33. Q_UNUSED(app);
  34. ctkErrorLogModel model;
  35. model.setFileLoggingEnabled(true);
  36. // Create log file
  37. QTemporaryFile logFile(QDir::tempPath() + "/ctkVTKErrorLogModelFileLoggingTest1.XXXXXX");
  38. logFile.setAutoRemove(false);
  39. logFile.open();
  40. logFile.close();
  41. QString logFilePath = logFile.fileName();
  42. model.setFilePath(logFilePath);
  43. // Monitor VTK messages
  44. model.registerMsgHandler(new ctkVTKErrorLogMessageHandler);
  45. model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, true);
  46. vtkNew<vtkObject> object;
  47. // VTK messages
  48. vtkDebugWithObjectMacro(object.GetPointer(), "This is a VTK debug message");
  49. vtkWarningWithObjectMacro(object.GetPointer(), "This is a VTK warning message");
  50. vtkErrorWithObjectMacro(object.GetPointer(), "This is a VTK error message");
  51. // Give enough time to the ErrorLogModel to consider the queued messages.
  52. processEvents(1000);
  53. model.disableAllMsgHandler();
  54. QStringList logLines = readFile(logFilePath);
  55. QString expectedLogEntryPatternTemplate(
  56. "^\\[%1\\]\\[VTK\\] [0-9\\.\\s\\:]+ \\[vtkObject \\(0x[a-zA-B0-9]+\\)\\] "
  57. "\\(.+ctkVTKErrorLogModelFileLoggingTest1\\.cpp\\:%2\\) \\- %3$");
  58. {
  59. int entryIndex = 0;
  60. QRegExp regexp(expectedLogEntryPatternTemplate.arg("WARNING").arg(66).arg("This is a VTK warning message"));
  61. if (!regexp.exactMatch(logLines.at(entryIndex)))
  62. {
  63. printErrorMessage(
  64. QString("Line %1 - Log entry %2 does NOT math expected regular expression.\n\tLogEntry: %3\n\tRegExp: %4").
  65. arg(__LINE__).arg(entryIndex).arg(logLines.at(entryIndex)).arg(regexp.pattern()));
  66. return EXIT_FAILURE;
  67. }
  68. }
  69. {
  70. int entryIndex = 1;
  71. QRegExp regexp(expectedLogEntryPatternTemplate.arg("ERROR").arg(67).arg("This is a VTK error message"));
  72. if (!regexp.exactMatch(logLines.at(entryIndex)))
  73. {
  74. printErrorMessage(
  75. QString("Line %1 - Log entry %2 does NOT math expected regular expression.\n\tLogEntry: %3\n\tRegExp: %4").
  76. arg(__LINE__).arg(entryIndex).arg(logLines.at(entryIndex)).arg(regexp.pattern()));
  77. return EXIT_FAILURE;
  78. }
  79. }
  80. return EXIT_SUCCESS;
  81. }