Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
QmitkThreadedLogTest.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkCommon.h"
14 #include "mitkTestingMacros.h"
15 #include <itksys/SystemTools.hxx>
16 #include <mitkLog.h>
18 
19 #include <string>
20 
21 #include <QtConcurrentRun>
22 
23 void LogMessages(unsigned int threadID, unsigned int numberOfTimes)
24 {
25  unsigned int times = 0;
26 
27  while (times < numberOfTimes)
28  {
29  MITK_INFO << "Test info stream in thread" << threadID << "\n even with newlines";
30  MITK_WARN << "Test warning stream in thread " << threadID << ". "
31  << "Even with a very long text, even without meaning or implied meaning or content, just a long sentence "
32  "to see whether something has problems with long sentences or output in files or into windows or "
33  "commandlines or whatever.";
34  MITK_DEBUG << "Test debugging stream in thread " << threadID;
35  MITK_ERROR << "Test error stream in thread " << threadID;
36  MITK_FATAL << "Test fatal stream in thread " << threadID;
37 
38  times += 5;
39  }
40 }
41 
45 static void TestThreadSaveLog(bool toFile)
46 {
47  bool testSucceded = true;
48 
49  try
50  {
51  if (toFile)
52  {
53  std::string filename = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + "/qtestthreadlog.log";
54  itksys::SystemTools::RemoveFile(filename.c_str()); // remove old file, we do not want to append to large files
55  mitk::LoggingBackend::SetLogFile(filename.c_str());
56  }
57 
58  unsigned int numberOfThreads = 10;
59  unsigned int threadRuntimeInMilliseconds = 4000;
60  QVector<QFuture<void>> threads;
61 
62  // Spawn some threads
63  for (unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
64  {
65  threads.push_back(QtConcurrent::run(LogMessages, threadIdx, 100));
66  std::cout << "Created " << threadIdx << ". thread." << std::endl;
67  }
68 
69  // wait for some time (milliseconds)
70  itksys::SystemTools::Delay(threadRuntimeInMilliseconds);
71 
72  // Wait for all to finish
73  for (unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
74  {
75  threads[threadIdx].waitForFinished();
76  std::cout << threadIdx << ". thread has finished" << std::endl;
77  }
78  }
79  catch (std::exception e)
80  {
81  MITK_ERROR << "exception during 'TestThreadSaveLog': " << e.what();
82  testSucceded = false;
83  }
84  catch (...)
85  {
86  MITK_ERROR << "unknown exception during 'TestThreadSaveLog'";
87  testSucceded = false;
88  }
89 
90  // if no error occured until now, everything is ok
91  MITK_TEST_CONDITION_REQUIRED(testSucceded, "Test logging in different threads.");
92 }
93 
94 int QmitkThreadedLogTest(int /* argc */, char * /*argv*/ [])
95 {
96  // always start with this!
97  MITK_TEST_BEGIN("QmitkThreadedLogTest")
98 
99  MITK_TEST_OUTPUT(<< "TESTING ALL LOGGING OUTPUTS, ERROR MESSAGES ARE ALSO TESTED AND NOT MEANING AN ERROR OCCURED!")
100 
101  TestThreadSaveLog(false); // false = to console
102  TestThreadSaveLog(true); // true = to file
103 
104  MITK_TEST_OUTPUT(<< "Number of threads in QThreadPool: " << QThreadPool::globalInstance()->maxThreadCount())
105 
106  // always end with this!
107  MITK_TEST_END()
108 }
#define MITK_FATAL
Definition: mitkLogMacros.h:21
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
void LogMessages(unsigned int threadID, unsigned int numberOfTimes)
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
int QmitkThreadedLogTest(int, char *[])
static void TestThreadSaveLog(bool toFile)
Test logging from Qt threads.
#define MITK_TEST_OUTPUT(x)
Output some text.
std::string GetOptionDirectory()
Return directory of/for option files.
#define MITK_WARN
Definition: mitkLogMacros.h:19
static void SetLogFile(const char *file)
Sets extra log file path (additionally to the console log)
Definition: mitkLog.cpp:90
and MITK_TEST_END()
static StandardFileLocations * GetInstance()