ctkErrorLogModelTestHelper.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <QTimer>
  21. #include <QThread>
  22. // CTK includes
  23. #include "ctkErrorLogModel.h"
  24. namespace
  25. {
  26. //-----------------------------------------------------------------------------
  27. // Utility function
  28. //-----------------------------------------------------------------------------
  29. QString checkRowCount(int line, int currentRowCount, int expectedRowCount)
  30. {
  31. if (currentRowCount != expectedRowCount)
  32. {
  33. QString errorMsg("Line %1 - Expected rowCount: %2 - Current rowCount: %3\n");
  34. return errorMsg.arg(line).arg(expectedRowCount).arg(currentRowCount);
  35. }
  36. return QString();
  37. }
  38. //-----------------------------------------------------------------------------
  39. QString checkTextMessages(int line, const QStringList& currentMessages, const QStringList& expectedMessages)
  40. {
  41. for(int i = 0; i < expectedMessages.count(); ++i)
  42. {
  43. if (!expectedMessages.contains(currentMessages.at(i)))
  44. {
  45. QString errorMsg("Line %1 - Problem with logged messages !\n"
  46. "\tMessage [%2] hasn't been logged !\n");
  47. return errorMsg.arg(line).arg(expectedMessages.value(i));
  48. }
  49. }
  50. return QString();
  51. }
  52. //-----------------------------------------------------------------------------
  53. QString checkTextMessages(int line, const ctkErrorLogModel& model, const QStringList& expectedMessages)
  54. {
  55. QStringList currentMessages;
  56. for(int i = 0; i < expectedMessages.count(); ++i)
  57. {
  58. QModelIndex descriptionIndex = model.index(i, ctkErrorLogModel::DescriptionColumn);
  59. currentMessages << descriptionIndex.data(ctkErrorLogModel::DescriptionTextRole).toString();
  60. }
  61. return checkTextMessages(line, currentMessages, expectedMessages);
  62. }
  63. //-----------------------------------------------------------------------------
  64. void printTextMessages(const ctkErrorLogModel& model)
  65. {
  66. fprintf(stdout, "%s", "ErrorLogModel rows:\n");
  67. QString text("\trow %1 => [%2]\n");
  68. for (int i=0; i < model.rowCount(); ++i)
  69. {
  70. QString description =
  71. model.index(i, ctkErrorLogModel::DescriptionColumn).data().toString();
  72. fprintf(stdout, "%s", qPrintable(text.arg(i).arg(description)));
  73. }
  74. fflush(stdout);
  75. }
  76. //-----------------------------------------------------------------------------
  77. void printErrorMessage(const QString& errorMessage)
  78. {
  79. fprintf(stderr, "%s", qPrintable(errorMessage));
  80. fflush(stderr);
  81. }
  82. //-----------------------------------------------------------------------------
  83. QString checkInteger(int line, const char* valueName, int current, int expected)
  84. {
  85. if (current != expected)
  86. {
  87. QString errorMsg("Line %1 - Expected %2: %3 - Current %4: %5\n");
  88. return errorMsg.arg(line).arg(valueName).
  89. arg(expected).arg(valueName).arg(current);
  90. }
  91. return QString();
  92. }
  93. //-----------------------------------------------------------------------------
  94. QString checkBoolean(int line, const char* valueName, bool current, bool 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(static_cast<int>(expected)).arg(valueName).arg(static_cast<int>(current));
  101. }
  102. return QString();
  103. }
  104. //-----------------------------------------------------------------------------
  105. void processEvents(int durationInMSecs)
  106. {
  107. QTimer timer;
  108. timer.setSingleShot(true);
  109. timer.start(durationInMSecs);
  110. while(timer.isActive())
  111. {
  112. QCoreApplication::processEvents();
  113. }
  114. }
  115. //-----------------------------------------------------------------------------
  116. QMutex AppendToFileMutex;
  117. //-----------------------------------------------------------------------------
  118. void appendToFile(const QString& fileName, const QString& text)
  119. {
  120. QMutexLocker locker(&AppendToFileMutex);
  121. QFile f(fileName);
  122. f.open(QFile::Append);
  123. QTextStream s(&f);
  124. s << QDateTime::currentDateTime().toString() << " - " << text << "\n";
  125. f.close();
  126. }
  127. //-----------------------------------------------------------------------------
  128. class LogMessageThread : public QThread
  129. {
  130. public:
  131. LogMessageThread(int id, int maxIteration) :
  132. Id(id), MaxIteration(maxIteration), Counter(0){}
  133. protected:
  134. void run();
  135. virtual void logMessage(const QDateTime& dateTime, int threadId, int counterIdx) = 0;
  136. private:
  137. int Id;
  138. int MaxIteration;
  139. int Counter;
  140. };
  141. //-----------------------------------------------------------------------------
  142. void LogMessageThread::run()
  143. {
  144. while(this->Counter < this->MaxIteration)
  145. {
  146. this->logMessage(QDateTime::currentDateTime(), this->Id, this->Counter);
  147. ++this->Counter;
  148. }
  149. }
  150. ////-----------------------------------------------------------------------------
  151. //template<class LogMessageThreadType>
  152. //class SynchronousLogMessageStarterThread : public QThread
  153. //{
  154. //public:
  155. // SynchronousLogMessageStarterThread(ctkErrorLogModel * errorLogModel, int threadCount, int maxIteration) :
  156. // ErrorLogModel(errorLogModel), ThreadCount(threadCount), MaxIteration(maxIteration)
  157. // {
  158. // this->ErrorLogModel->setAsynchronousLogging(false);
  159. // }
  160. //protected:
  161. // void run();
  162. //private:
  163. // QList<QSharedPointer<LogMessageThread> > ThreadList;
  164. // ctkErrorLogModel * ErrorLogModel;
  165. // int ThreadCount;
  166. // int MaxIteration;
  167. //};
  168. ////-----------------------------------------------------------------------------
  169. //template<class LogMessageThreadType>
  170. //void SynchronousLogMessageStarterThread<LogMessageThreadType>::run()
  171. //{
  172. // for(int i = 0; i < this->ThreadCount; ++i)
  173. // {
  174. // this->ThreadList << QSharedPointer<LogMessageThread>(new LogMessageThreadType(i, this->MaxIteration));
  175. // this->ThreadList.back()->start();
  176. // }
  177. // foreach(const QSharedPointer<LogMessageThread>& thread, this->ThreadList)
  178. // {
  179. // thread->wait();
  180. // }
  181. // int expectedMessageCount = this->ThreadCount * this->MaxIteration * 2;
  182. // QString errorMsg = checkRowCount(__LINE__, this->ErrorLogModel->rowCount(),
  183. // /* expected = */ expectedMessageCount);
  184. // if (!errorMsg.isEmpty())
  185. // {
  186. // this->ErrorLogModel->disableAllMsgHandler();
  187. // printErrorMessage(errorMsg);
  188. // printTextMessages(*this->ErrorLogModel);
  189. // QCoreApplication::exit(EXIT_FAILURE);
  190. // }
  191. //}
  192. //-----------------------------------------------------------------------------
  193. QList<QSharedPointer<LogMessageThread> > ThreadList;
  194. //-----------------------------------------------------------------------------
  195. template<class LogMessageThreadType>
  196. void startLogMessageThreads(int threadCount, int maxIteration)
  197. {
  198. for(int i = 0; i < threadCount; ++i)
  199. {
  200. ThreadList << QSharedPointer<LogMessageThread>(new LogMessageThreadType(i, maxIteration));
  201. ThreadList.back()->start();
  202. }
  203. }
  204. } // end namespace