1 /*=========================================================================
2 
3   Library:   CTK
4 
5   Copyright (c) Kitware Inc.
6 
7   Licensed under the Apache License, Version 2.0 (the "License");
8   you may not use this file except in compliance with the License.
9   You may obtain a copy of the License at
10 
11       http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13   Unless required by applicable law or agreed to in writing, software
14   distributed under the License is distributed on an "AS IS" BASIS,
15   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   See the License for the specific language governing permissions and
17   limitations under the License.
18 
19 =========================================================================*/
20 
21 // Qt includes
22 #include <QCoreApplication>
23 #include <QDebug>
24 
25 // CTK includes
26 #include "ctkErrorLogFDMessageHandler.h"
27 #include "ctkModelTester.h"
28 
29 // STL includesQList
30 #include <cstdlib>
31 #include <iostream>
32 
33 // Helper functions
34 #include "Testing/Cpp/ctkErrorLogModelTestHelper.cpp"
35 
36 namespace
37 {
38 //-----------------------------------------------------------------------------
39 class LogFDMessageThread : public LogMessageThread
40 {
41 public:
LogFDMessageThread(int id,int maxIteration)42   LogFDMessageThread(int id, int maxIteration) : LogMessageThread(id, maxIteration){}
43 
logMessage(const QDateTime & dateTime,int threadId,int counterIdx)44   virtual void logMessage(const QDateTime& dateTime, int threadId, int counterIdx)
45   {
46     QString msg = QString("counterIdx:%1 - %2 - Message from thread: %3\n")
47         .arg(counterIdx).arg(dateTime.toString()).arg(threadId);
48 
49     fprintf(stdout, "%s", qPrintable(msg));
50     fflush(stdout);
51 
52     fprintf(stderr, "%s", qPrintable(msg));
53     fflush(stderr);
54   }
55 };
56 
57 }
58 
59 //-----------------------------------------------------------------------------
ctkErrorLogFDMessageHandlerWithThreadsTest1(int argc,char * argv[])60 int ctkErrorLogFDMessageHandlerWithThreadsTest1(int argc, char * argv [])
61 {
62   QCoreApplication app(argc, argv);
63   Q_UNUSED(app);
64 
65   ctkErrorLogModel model;
66   ctkModelTester modelTester;
67   modelTester.setVerbose(false);
68 
69   try
70     {
71     modelTester.setModel(&model);
72 
73     // --------------------------------------------------------------------------
74     // Monitor FD messages
75 
76     model.registerMsgHandler(new ctkErrorLogFDMessageHandler);
77     model.setMsgHandlerEnabled(ctkErrorLogFDMessageHandler::HandlerName, true);
78 
79     int threadCount = 15;
80     int maxIteration = 5;
81     int messagesPerIteration = 2;
82 
83     startLogMessageThreads<LogFDMessageThread>(threadCount, maxIteration);
84 
85     // Give enough time for the threads to send their messages
86     QTimer::singleShot(1500, qApp, SLOT(quit()));
87     app.exec();
88 
89     int expectedMessageCount = threadCount * maxIteration * messagesPerIteration;
90     QString errorMsg = checkRowCount(__LINE__, model.rowCount(), /* expected = */ expectedMessageCount);
91     if (!errorMsg.isEmpty())
92       {
93       model.disableAllMsgHandler();
94       printErrorMessage(errorMsg);
95       printTextMessages(model);
96       return EXIT_FAILURE;
97       }
98     }
99   catch (const char* error)
100     {
101     model.disableAllMsgHandler();
102     std::cerr << error << std::endl;
103     return EXIT_FAILURE;
104     }
105 
106   return EXIT_SUCCESS;
107 }
108