ctkErrorLogModelTestHelper.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDateTime>
  16. #include <QFile>
  17. #include <QMutexLocker>
  18. #include <QSharedPointer>
  19. #include <QStringList>
  20. #include <QTextStream>
  21. #include <QTimer>
  22. #include <QThread>
  23. // CTK includes
  24. #include "ctkErrorLogModel.h"
  25. namespace
  26. {
  27. //-----------------------------------------------------------------------------
  28. // Utility function
  29. //-----------------------------------------------------------------------------
  30. QString checkRowCount(int line, int currentRowCount, int expectedRowCount)
  31. {
  32. if (currentRowCount != expectedRowCount)
  33. {
  34. QString errorMsg("Line %1 - Expected rowCount: %2 - Current rowCount: %3\n");
  35. return errorMsg.arg(line).arg(expectedRowCount).arg(currentRowCount);
  36. }
  37. return QString();
  38. }
  39. //-----------------------------------------------------------------------------
  40. QString checkSpyCount(int line, int currentSpyCount, int expectedSpyCount)
  41. {
  42. if (currentSpyCount != expectedSpyCount)
  43. {
  44. QString errorMsg("Line %1 - Expected spyCount: %2 - Current spyCount: %3\n");
  45. return errorMsg.arg(line).arg(expectedSpyCount).arg(currentSpyCount);
  46. }
  47. return QString();
  48. }
  49. //-----------------------------------------------------------------------------
  50. QString checkTextMessages(int line, const QStringList& currentMessages, const QStringList& expectedMessages)
  51. {
  52. for(int i = 0; i < expectedMessages.count(); ++i)
  53. {
  54. if (!expectedMessages.contains(currentMessages.at(i)))
  55. {
  56. QString errorMsg("Line %1 - Problem with logged messages !\n"
  57. "\tMessage [%2] hasn't been logged !\n");
  58. return errorMsg.arg(line).arg(expectedMessages.value(i));
  59. }
  60. }
  61. return QString();
  62. }
  63. //-----------------------------------------------------------------------------
  64. QString checkTextMessages(int line, const ctkErrorLogModel& model, const QStringList& expectedMessages)
  65. {
  66. QStringList currentMessages;
  67. for(int i = 0; i < expectedMessages.count(); ++i)
  68. {
  69. QModelIndex descriptionIndex = model.index(i, ctkErrorLogModel::DescriptionColumn);
  70. currentMessages << descriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  71. }
  72. return checkTextMessages(line, currentMessages, expectedMessages);
  73. }
  74. //-----------------------------------------------------------------------------
  75. void printTextMessages(const ctkErrorLogModel& model)
  76. {
  77. fprintf(stdout, "%s", "ErrorLogModel rows:\n");
  78. QString text("\trow %1 => [%2]\n");
  79. for (int i=0; i < model.rowCount(); ++i)
  80. {
  81. QString description =
  82. model.index(i, ctkErrorLogModel::DescriptionColumn).data().toString();
  83. fprintf(stdout, "%s", qPrintable(text.arg(i).arg(description)));
  84. }
  85. fflush(stdout);
  86. }
  87. //-----------------------------------------------------------------------------
  88. void printErrorMessage(const QString& errorMessage)
  89. {
  90. fprintf(stderr, "%s", qPrintable(errorMessage));
  91. fflush(stderr);
  92. }
  93. //-----------------------------------------------------------------------------
  94. QString checkInteger(int line, const char* valueName, int current, int expected)
  95. {
  96. if (current != expected)
  97. {
  98. QString errorMsg("Line %1 - Expected %2: %3 - Current %4: %5\n");
  99. return errorMsg.arg(line).arg(valueName).
  100. arg(expected).arg(valueName).arg(current);
  101. }
  102. return QString();
  103. }
  104. //-----------------------------------------------------------------------------
  105. QString checkBoolean(int line, const char* valueName, bool current, bool expected)
  106. {
  107. if (current != expected)
  108. {
  109. QString errorMsg("Line %1 - Expected %2: %3 - Current %4: %5\n");
  110. return errorMsg.arg(line).arg(valueName).
  111. arg(static_cast<int>(expected)).arg(valueName).arg(static_cast<int>(current));
  112. }
  113. return QString();
  114. }
  115. //-----------------------------------------------------------------------------
  116. QString checkString(int line, const char* valueName, QString current, QString expected)
  117. {
  118. if (current != expected)
  119. {
  120. QString errorMsg("Line %1 - Expected %2: %3 - Current %4: %5\n");
  121. return errorMsg.arg(line).arg(valueName).
  122. arg(static_cast<QString>(expected)).arg(valueName).arg(static_cast<QString>(current));
  123. }
  124. return QString();
  125. }
  126. //-----------------------------------------------------------------------------
  127. void processEvents(int durationInMSecs)
  128. {
  129. QTimer timer;
  130. timer.setSingleShot(true);
  131. timer.start(durationInMSecs);
  132. while(timer.isActive())
  133. {
  134. QCoreApplication::processEvents();
  135. }
  136. }
  137. //-----------------------------------------------------------------------------
  138. QMutex AppendToFileMutex;
  139. //-----------------------------------------------------------------------------
  140. void appendToFile(const QString& fileName, const QString& text)
  141. {
  142. QMutexLocker locker(&AppendToFileMutex);
  143. QFile f(fileName);
  144. f.open(QFile::Append);
  145. QTextStream s(&f);
  146. s << QDateTime::currentDateTime().toString() << " - " << text << "\n";
  147. f.close();
  148. }
  149. //-----------------------------------------------------------------------------
  150. QStringList readFile(const QString& filePath)
  151. {
  152. QStringList lines;
  153. QFile file(filePath);
  154. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  155. {
  156. return lines;
  157. }
  158. QTextStream in(&file);
  159. while(!in.atEnd())
  160. {
  161. lines << in.readLine();
  162. }
  163. file.close();
  164. return lines;
  165. }
  166. //-----------------------------------------------------------------------------
  167. class LogMessageThread : public QThread
  168. {
  169. public:
  170. LogMessageThread(int id, int maxIteration) :
  171. Id(id), MaxIteration(maxIteration), Counter(0){}
  172. protected:
  173. void run();
  174. virtual void logMessage(const QDateTime& dateTime, int threadId, int counterIdx) = 0;
  175. private:
  176. int Id;
  177. int MaxIteration;
  178. int Counter;
  179. };
  180. //-----------------------------------------------------------------------------
  181. void LogMessageThread::run()
  182. {
  183. while(this->Counter < this->MaxIteration)
  184. {
  185. this->logMessage(QDateTime::currentDateTime(), this->Id, this->Counter);
  186. ++this->Counter;
  187. }
  188. }
  189. ////-----------------------------------------------------------------------------
  190. //template<class LogMessageThreadType>
  191. //class SynchronousLogMessageStarterThread : public QThread
  192. //{
  193. //public:
  194. // SynchronousLogMessageStarterThread(ctkErrorLogModel * errorLogModel, int threadCount, int maxIteration) :
  195. // ErrorLogModel(errorLogModel), ThreadCount(threadCount), MaxIteration(maxIteration)
  196. // {
  197. // this->ErrorLogModel->setAsynchronousLogging(false);
  198. // }
  199. //protected:
  200. // void run();
  201. //private:
  202. // QList<QSharedPointer<LogMessageThread> > ThreadList;
  203. // ctkErrorLogModel * ErrorLogModel;
  204. // int ThreadCount;
  205. // int MaxIteration;
  206. //};
  207. ////-----------------------------------------------------------------------------
  208. //template<class LogMessageThreadType>
  209. //void SynchronousLogMessageStarterThread<LogMessageThreadType>::run()
  210. //{
  211. // for(int i = 0; i < this->ThreadCount; ++i)
  212. // {
  213. // this->ThreadList << QSharedPointer<LogMessageThread>(new LogMessageThreadType(i, this->MaxIteration));
  214. // this->ThreadList.back()->start();
  215. // }
  216. // foreach(const QSharedPointer<LogMessageThread>& thread, this->ThreadList)
  217. // {
  218. // thread->wait();
  219. // }
  220. // int expectedMessageCount = this->ThreadCount * this->MaxIteration * 2;
  221. // QString errorMsg = checkRowCount(__LINE__, this->ErrorLogModel->rowCount(),
  222. // /* expected = */ expectedMessageCount);
  223. // if (!errorMsg.isEmpty())
  224. // {
  225. // this->ErrorLogModel->disableAllMsgHandler();
  226. // printErrorMessage(errorMsg);
  227. // printTextMessages(*this->ErrorLogModel);
  228. // QCoreApplication::exit(EXIT_FAILURE);
  229. // }
  230. //}
  231. //-----------------------------------------------------------------------------
  232. QList<QSharedPointer<LogMessageThread> > ThreadList;
  233. //-----------------------------------------------------------------------------
  234. template<class LogMessageThreadType>
  235. void startLogMessageThreads(int threadCount, int maxIteration)
  236. {
  237. for(int i = 0; i < threadCount; ++i)
  238. {
  239. ThreadList << QSharedPointer<LogMessageThread>(new LogMessageThreadType(i, maxIteration));
  240. ThreadList.back()->start();
  241. }
  242. }
  243. } // end namespace