15 #include <itkMultiThreader.h> 16 #include <itksys/SystemTools.hxx> 42 bool WasCalled() {
return m_Called; }
53 class mitkTestLoggingThread :
public itk::Object
62 mitkTestLoggingThread(itk::MultiThreader::Pointer MultiThreader)
66 m_MultiThreader = MultiThreader;
67 LoggingRunning =
true;
74 itk::MultiThreader::Pointer m_MultiThreader;
78 while (LoggingRunning)
80 MITK_INFO <<
"Test info stream in thread" << ThreadID <<
"\n even with newlines";
81 MITK_WARN <<
"Test warning stream in thread " << ThreadID <<
". " 82 <<
"Even with a very long text, even without meaning or implied meaning or content, just a long " 83 "sentence to see whether something has problems with long sentences or output in files or into " 84 "windows or commandlines or whatever.";
85 MITK_DEBUG <<
"Test debugging stream in thread " << ThreadID;
86 MITK_ERROR <<
"Test error stream in thread " << ThreadID;
87 MITK_FATAL <<
"Test fatal stream in thread " << ThreadID;
89 NumberOfMessages += 5;
93 static ITK_THREAD_RETURN_TYPE ThreadStartTracking(
void *pInfoStruct)
96 auto *pInfo = (
struct itk::MultiThreader::ThreadInfoStruct *)pInfoStruct;
99 return ITK_THREAD_RETURN_VALUE;
101 if (pInfo->UserData ==
nullptr)
103 return ITK_THREAD_RETURN_VALUE;
105 auto *thisthread = (mitkTestLoggingThread *)pInfo->UserData;
107 if (thisthread !=
nullptr)
108 thisthread->LogMessages();
110 return ITK_THREAD_RETURN_VALUE;
116 LoggingRunning =
true;
117 this->ThreadID = m_MultiThreader->SpawnThread(this->ThreadStartTracking,
this);
121 void Stop() { LoggingRunning =
false; }
128 class mitkLogTestClass
131 static void TestSimpleLog()
133 bool testSucceded =
true;
145 testSucceded =
false;
150 static void TestObjectInfoLogging()
152 bool testSucceded =
true;
158 std::string testString =
"testString";
159 std::stringstream testStringStream;
160 testStringStream <<
"test" 164 testMitkPoint.Fill(2);
175 testSucceded =
false;
182 bool testSucceded =
true;
189 itksys::SystemTools::RemoveFile(filename.c_str());
193 unsigned int numberOfThreads = 20;
194 unsigned int threadRuntimeInMilliseconds = 2000;
196 std::vector<unsigned int> threadIDs;
197 std::vector<mitkTestLoggingThread::Pointer> threads;
199 itk::MultiThreader::Pointer multiThreader = itk::MultiThreader::New();
200 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
203 mitkTestLoggingThread::Pointer newThread = mitkTestLoggingThread::New(multiThreader);
204 threads.push_back(newThread);
205 std::cout <<
"Created " << threadIdx <<
". thread." << std::endl;
208 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
211 std::cout <<
"Start " << threadIdx <<
". thread." << std::endl;
212 threadIDs.push_back(threads[threadIdx]->Start());
213 std::cout << threadIdx <<
". thread has ID " << threadIDs[threadIdx] << std::endl;
217 itksys::SystemTools::Delay(threadRuntimeInMilliseconds);
219 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
222 std::cout <<
"Stop " << threadIdx <<
". thread." << std::endl;
223 threads[threadIdx]->Stop();
226 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
229 multiThreader->TerminateThread(threadIDs[threadIdx]);
230 std::cout <<
"Terminated " << threadIdx <<
". thread (" << threads[threadIdx]->NumberOfMessages <<
" messages)." 234 catch (std::exception &e)
236 MITK_ERROR <<
"exception during 'TestThreadSaveLog': " << e.what();
237 testSucceded =
false;
241 MITK_ERROR <<
"unknown exception during 'TestThreadSaveLog'";
242 testSucceded =
false;
249 static void TestLoggingToFile()
258 static void TestAddAndRemoveBackends()
269 static void TestDefaultBackend()
275 static void TestEnableDisableBackends()
277 TestBackendCout myCoutBackend = TestBackendCout();
281 MITK_INFO <<
"There should be no output!";
282 bool success = !myCoutBackend.WasCalled();
285 MITK_INFO <<
"Now there should be an output.";
286 success &= myCoutBackend.WasCalled();
298 MITK_TEST_OUTPUT(<<
"TESTING ALL LOGGING OUTPUTS, ERROR MESSAGES ARE ALSO TESTED AND NOT MEANING AN ERROR OCCURED!")
300 mitkLogTestClass::TestSimpleLog();
301 mitkLogTestClass::TestObjectInfoLogging();
303 mitkLogTestClass::TestLoggingToFile();
304 mitkLogTestClass::TestAddAndRemoveBackends();
307 mitkLogTestClass::TestEnableDisableBackends();
void MBILOG_EXPORT UnregisterBackend(BackendBase *backend)
Unregisters a backend.
#define mitkNewMacro1Param(classname, type)
void MBILOG_EXPORT EnableBackends(OutputType type)
void LogMessages(unsigned int threadID, unsigned int numberOfTimes)
#define MITK_TEST_CONDITION_REQUIRED(COND, MSG)
void MBILOG_EXPORT DisableBackends(OutputType type)
Default backend of the mbi logging mechanism. This backend is used if no other backend is registered...
section GeneralTestsDeprecatedOldTestingStyle Deprecated macros All tests with MITK_TEST_BEGIN()
static void TestThreadSaveLog(bool toFile)
Test logging from Qt threads.
An object of this class represents a single logging message (logging event) of the mbi logging mechan...
#define MITK_TEST_OUTPUT(x)
Output some text.
std::string GetOptionDirectory()
Return directory of/for option files.
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
#define mitkClassMacroItkParent(className, SuperClassName)
void MBILOG_EXPORT RegisterBackend(BackendBase *backend)
Registeres a backend to the mbi logging mechanism. If a backend is registered here, all mbilog messages are relayed to this backend through the method ProcessMessage. If no backend is registered the default backend is used.
static void SetLogFile(const char *file)
Sets extra log file path (additionally to the console log)
int mitkLogTest(int, char *[])
void ProcessMessage(const mbilog::LogMessage &l) override
This method is called by the mbi logging mechanism if the object is registered in the mbi logging mec...
static std::string GetLogFile()
static StandardFileLocations * GetInstance()