PageRenderTime 33ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Testing/Code/Common/itkLoggerThreadWrapperTest.cxx

https://github.com/luisibanez/ITK
C++ | 163 lines | 106 code | 21 blank | 36 comment | 1 complexity | 0f4940086a504bfc36c0e2f5c95ca559 MD5 | raw file
  1. /*=========================================================================
  2. *
  3. * Copyright Insight Software Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0.txt
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. *=========================================================================*/
  18. #if defined(_MSC_VER)
  19. //Warning about: identifier was truncated to '255' characters in the debug information (MVC6.0 Debug)
  20. #pragma warning( disable : 4786 )
  21. #endif
  22. #if ! defined(_MSC_VER) //NOTE: This class does not work under MSVS6
  23. #include "itkLoggerThreadWrapper.h"
  24. #include <iostream>
  25. #include <fstream>
  26. #include "itkStdStreamLogOutput.h"
  27. #include "itkLoggerBase.h"
  28. /** \class SimpleLogger
  29. * \brief Class SimpleLogger is meant to demonstrate how to change the formatting of the LoggerBase mechanism
  30. * and how to define a threaded logger that works with this.
  31. *
  32. * \author Hans J. Johnson, The University of Iowa
  33. * \ingroup OSSystemObjects LoggingObjects
  34. */
  35. class SimpleLogger : public itk::LoggerBase
  36. {
  37. public:
  38. typedef SimpleLogger Self;
  39. typedef itk::LoggerBase Superclass;
  40. typedef itk::SmartPointer<Self> Pointer;
  41. typedef itk::SmartPointer<const Self> ConstPointer;
  42. /** Run-time type information (and related methods). */
  43. itkTypeMacro( SimpleLogger, Object );
  44. /** New macro for creation of through a Smart Pointer */
  45. itkNewMacro( Self );
  46. virtual std::string BuildFormattedEntry(PriorityLevelType level, std::string const & content)
  47. {
  48. return std::string("<H1>") + content + std::string("</H1>");
  49. }
  50. protected:
  51. /** Constructor */
  52. SimpleLogger() {};
  53. /** Destructor */
  54. virtual ~SimpleLogger() {};
  55. }; // class Logger
  56. class LogTester
  57. {
  58. public:
  59. itk::LoggerBase* GetLogger() { return m_Logger; }
  60. void SetLogger(itk::LoggerBase* logger) { m_Logger = logger; }
  61. void log() {
  62. itkLogMacro( DEBUG, "DEBUG message by itkLogMacro\n" );
  63. itkLogMacro( INFO, "INFO message by itkLogMacro\n" );
  64. itkLogMacro( WARNING, "WARNING message by itkLogMacro\n" );
  65. itkLogMacro( CRITICAL, "CRITICAL message by itkLogMacro\n" );
  66. itkLogMacro( FATAL, "FATAL message by itkLogMacro\n" );
  67. itkLogMacro( MUSTFLUSH, "MUSTFLUSH message by itkLogMacro\n" );
  68. }
  69. static void logStatic(LogTester* tester)
  70. {
  71. itkLogMacroStatic( tester, DEBUG, "DEBUG message by itkLogMacroStatic\n" );
  72. itkLogMacroStatic( tester, INFO, "INFO message by itkLogMacroStatic\n" );
  73. itkLogMacroStatic( tester, WARNING, "WARNING message by itkLogMacroStatic\n" );
  74. itkLogMacroStatic( tester, CRITICAL, "CRITICAL message by itkLogMacroStatic\n" );
  75. itkLogMacroStatic( tester, FATAL, "FATAL message by itkLogMacroStatic\n" );
  76. itkLogMacroStatic( tester, MUSTFLUSH, "MUSTFLUSH message by itkLogMacroStatic\n" );
  77. }
  78. private:
  79. itk::LoggerBase* m_Logger;
  80. };
  81. #endif// ! defined(_MSC_VER) //NOTE: This class does not work under MSVS6
  82. int itkLoggerThreadWrapperTest( int argc, char * argv[] )
  83. {
  84. #if ! defined(_MSC_VER) //NOTE: This class does not work under MSVS6
  85. try
  86. {
  87. if (argc < 2)
  88. {
  89. std::cout << "Usage: " << argv[0] << " logFilename" << std::endl;
  90. return EXIT_FAILURE;
  91. }
  92. // Create an ITK StdStreamLogOutputs
  93. itk::StdStreamLogOutput::Pointer coutput = itk::StdStreamLogOutput::New();
  94. itk::StdStreamLogOutput::Pointer foutput = itk::StdStreamLogOutput::New();
  95. coutput->SetStream(std::cout);
  96. std::ofstream fout(argv[1]);
  97. foutput->SetStream(fout);
  98. // Create an ITK ThreadLogger
  99. itk::LoggerThreadWrapper<SimpleLogger>::Pointer logger = itk::LoggerThreadWrapper<SimpleLogger>::New();
  100. std::cout << "Testing itk::ThreadLogger" << std::endl;
  101. // Setting the logger
  102. logger->SetName("org.itk.threadLogger");
  103. logger->SetPriorityLevel(itk::LoggerBase::INFO);
  104. logger->SetLevelForFlushing(itk::LoggerBase::CRITICAL);
  105. std::cout << " Adding console and file stream LogOutputs" << std::endl;
  106. logger->AddLogOutput(coutput);
  107. logger->AddLogOutput(foutput);
  108. // Printing the logger's member variables
  109. std::cout << " Name: " << logger->GetName() << std::endl;
  110. std::cout << " Priority Level: " << logger->GetPriorityLevel() << std::endl;
  111. std::cout << " Level For Flushing: " << logger->GetLevelForFlushing() << std::endl;
  112. // Logging by the itkLogMacro from a class with itk::ThreadLogger
  113. LogTester tester;
  114. tester.SetLogger(logger);
  115. tester.log();
  116. // Logging by the itkLogMacroStatic from a class with itk::ThreadLogger
  117. LogTester::logStatic(&tester);
  118. std::cout << " The printed order of 'Messages ##' below might not be predictable because of multi-threaded logging" << std::endl;
  119. std::cout << " But the logged messages will be in order." << std::endl;
  120. std::cout << " Each line is an atom for synchronization." << std::endl;
  121. // Writing by the logger
  122. logger->Write(itk::LoggerBase::DEBUG, "This is the DEBUG message.\n");
  123. std::cout << " Message #1" << std::endl;
  124. logger->Write(itk::LoggerBase::INFO, "This is the INFO message.\n");
  125. logger->Write(itk::LoggerBase::WARNING, "This is the WARNING message.\n");
  126. std::cout << " Message #2" << std::endl;
  127. logger->Write(itk::LoggerBase::CRITICAL, "This is the CRITICAL message.\n");
  128. logger->Write(itk::LoggerBase::FATAL, "This is the FATAL message.\n");
  129. logger->Write(itk::LoggerBase::MUSTFLUSH, "This is the MUSTFLUSH message.\n");
  130. std::cout << " Message #3" << std::endl;
  131. logger->Flush();
  132. std::cout << " Flushing by the ThreadLogger is synchronized." << std::endl;
  133. }
  134. catch(...)
  135. {
  136. std::cerr << "Exception catched !!" << std::endl;
  137. return EXIT_FAILURE;
  138. }
  139. std::cout << "[PASSED]" << std::endl;
  140. #endif //! defined(_MSC_VER) //NOTE: This class does not work under MSVS6
  141. return EXIT_SUCCESS;
  142. }