Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkCommon.h"
18 #include "mitkTestingMacros.h"
19 #include <itksys/SystemTools.hxx>
20 #include <mitkLog.h>
22 
23 #include <string>
24 
25 #include <QtConcurrentRun>
26 
27 void LogMessages(unsigned int threadID, unsigned int numberOfTimes)
28 {
29  unsigned int times = 0;
30 
31  while (times < numberOfTimes)
32  {
33  MITK_INFO << "Test info stream in thread" << threadID << "\n even with newlines";
34  MITK_WARN << "Test warning stream in thread " << threadID << ". "
35  << "Even with a very long text, even without meaning or implied meaning or content, just a long sentence "
36  "to see whether something has problems with long sentences or output in files or into windows or "
37  "commandlines or whatever.";
38  MITK_DEBUG << "Test debugging stream in thread " << threadID;
39  MITK_ERROR << "Test error stream in thread " << threadID;
40  MITK_FATAL << "Test fatal stream in thread " << threadID;
41 
42  times += 5;
43  }
44 }
45 
49 static void TestThreadSaveLog(bool toFile)
50 {
51  bool testSucceded = true;
52 
53  try
54  {
55  if (toFile)
56  {
57  std::string filename = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + "/qtestthreadlog.log";
58  itksys::SystemTools::RemoveFile(filename.c_str()); // remove old file, we do not want to append to large files
59  mitk::LoggingBackend::SetLogFile(filename.c_str());
60  }
61 
62  unsigned int numberOfThreads = 10;
63  unsigned int threadRuntimeInMilliseconds = 4000;
64  QVector<QFuture<void>> threads;
65 
66  // Spawn some threads
67  for (unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
68  {
69  threads.push_back(QtConcurrent::run(LogMessages, threadIdx, 100));
70  std::cout << "Created " << threadIdx << ". thread." << std::endl;
71  }
72 
73  // wait for some time (milliseconds)
74  itksys::SystemTools::Delay(threadRuntimeInMilliseconds);
75 
76  // Wait for all to finish
77  for (unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
78  {
79  threads[threadIdx].waitForFinished();
80  std::cout << threadIdx << ". thread has finished" << std::endl;
81  }
82  }
83  catch (std::exception e)
84  {
85  MITK_ERROR << "exception during 'TestThreadSaveLog': " << e.what();
86  testSucceded = false;
87  }
88  catch (...)
89  {
90  MITK_ERROR << "unknown exception during 'TestThreadSaveLog'";
91  testSucceded = false;
92  }
93 
94  // if no error occured until now, everything is ok
95  MITK_TEST_CONDITION_REQUIRED(testSucceded, "Test logging in different threads.");
96 }
97 
98 int QmitkThreadedLogTest(int /* argc */, char * /*argv*/ [])
99 {
100  // always start with this!
101  MITK_TEST_BEGIN("QmitkThreadedLogTest")
102 
103  MITK_TEST_OUTPUT(<< "TESTING ALL LOGGING OUTPUTS, ERROR MESSAGES ARE ALSO TESTED AND NOT MEANING AN ERROR OCCURED!")
104 
105  TestThreadSaveLog(false); // false = to console
106  TestThreadSaveLog(true); // true = to file
107 
108  MITK_TEST_OUTPUT(<< "Number of threads in QThreadPool: " << QThreadPool::globalInstance()->maxThreadCount())
109 
110  // always end with this!
111  MITK_TEST_END()
112 }
#define MITK_FATAL
Definition: mitkLogMacros.h:25
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
void LogMessages(unsigned int threadID, unsigned int numberOfTimes)
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
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:23
static const std::string filename
static void SetLogFile(const char *file)
Sets extra log file path (additionally to the console log)
Definition: mitkLog.cpp:94
and MITK_TEST_END()
static StandardFileLocations * GetInstance()