19 #include <itkMultiThreader.h>
20 #include <itksys/SystemTools.hxx>
46 bool WasCalled() {
return m_Called; }
57 class mitkTestLoggingThread :
public itk::Object
70 m_MultiThreader = MultiThreader;
71 LoggingRunning =
true;
82 while (LoggingRunning)
84 MITK_INFO <<
"Test info stream in thread" << ThreadID <<
"\n even with newlines";
85 MITK_WARN <<
"Test warning stream in thread " << ThreadID <<
". "
86 <<
"Even with a very long text, even without meaning or implied meaning or content, just a long "
87 "sentence to see whether something has problems with long sentences or output in files or into "
88 "windows or commandlines or whatever.";
89 MITK_DEBUG <<
"Test debugging stream in thread " << ThreadID;
90 MITK_ERROR <<
"Test error stream in thread " << ThreadID;
91 MITK_FATAL <<
"Test fatal stream in thread " << ThreadID;
93 NumberOfMessages += 5;
97 static ITK_THREAD_RETURN_TYPE ThreadStartTracking(
void *pInfoStruct)
100 struct itk::MultiThreader::ThreadInfoStruct *pInfo = (
struct itk::MultiThreader::ThreadInfoStruct *)pInfoStruct;
101 if (pInfo ==
nullptr)
103 return ITK_THREAD_RETURN_VALUE;
105 if (pInfo->UserData ==
nullptr)
107 return ITK_THREAD_RETURN_VALUE;
109 mitkTestLoggingThread *thisthread = (mitkTestLoggingThread *)pInfo->UserData;
111 if (thisthread !=
nullptr)
112 thisthread->LogMessages();
114 return ITK_THREAD_RETURN_VALUE;
120 LoggingRunning =
true;
121 this->ThreadID = m_MultiThreader->SpawnThread(this->ThreadStartTracking,
this);
125 void Stop() { LoggingRunning =
false; }
132 class mitkLogTestClass
135 static void TestSimpleLog()
137 bool testSucceded =
true;
149 testSucceded =
false;
154 static void TestObjectInfoLogging()
156 bool testSucceded =
true;
162 std::string testString =
"testString";
163 std::stringstream testStringStream;
164 testStringStream <<
"test"
168 testMitkPoint.Fill(2);
179 testSucceded =
false;
186 bool testSucceded =
true;
193 itksys::SystemTools::RemoveFile(filename.c_str());
197 unsigned int numberOfThreads = 20;
198 unsigned int threadRuntimeInMilliseconds = 2000;
200 std::vector<unsigned int> threadIDs;
201 std::vector<mitkTestLoggingThread::Pointer> threads;
204 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
208 threads.push_back(newThread);
209 std::cout <<
"Created " << threadIdx <<
". thread." << std::endl;
212 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
215 std::cout <<
"Start " << threadIdx <<
". thread." << std::endl;
216 threadIDs.push_back(threads[threadIdx]->Start());
217 std::cout << threadIdx <<
". thread has ID " << threadIDs[threadIdx] << std::endl;
221 itksys::SystemTools::Delay(threadRuntimeInMilliseconds);
223 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
226 std::cout <<
"Stop " << threadIdx <<
". thread." << std::endl;
227 threads[threadIdx]->Stop();
230 for (
unsigned int threadIdx = 0; threadIdx < numberOfThreads; ++threadIdx)
233 multiThreader->TerminateThread(threadIDs[threadIdx]);
234 std::cout <<
"Terminated " << threadIdx <<
". thread (" << threads[threadIdx]->NumberOfMessages <<
" messages)."
238 catch (std::exception &e)
240 MITK_ERROR <<
"exception during 'TestThreadSaveLog': " << e.what();
241 testSucceded =
false;
245 MITK_ERROR <<
"unknown exception during 'TestThreadSaveLog'";
246 testSucceded =
false;
253 static void TestLoggingToFile()
262 static void TestAddAndRemoveBackends()
273 static void TestDefaultBackend()
279 static void TestEnableDisableBackends()
281 TestBackendCout myCoutBackend = TestBackendCout();
285 MITK_INFO <<
"There should be no output!";
286 bool success = !myCoutBackend.WasCalled();
289 MITK_INFO <<
"Now there should be an output.";
290 success &= myCoutBackend.WasCalled();
302 MITK_TEST_OUTPUT(<<
"TESTING ALL LOGGING OUTPUTS, ERROR MESSAGES ARE ALSO TESTED AND NOT MEANING AN ERROR OCCURED!")
304 mitkLogTestClass::TestSimpleLog();
305 mitkLogTestClass::TestObjectInfoLogging();
307 mitkLogTestClass::TestLoggingToFile();
308 mitkLogTestClass::TestAddAndRemoveBackends();
311 mitkLogTestClass::TestEnableDisableBackends();
itk::SmartPointer< Self > Pointer
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...
static const std::string filename
#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 *[])
virtual 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()
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.