ctkErrorLogFDMessageHandler.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <QDebug>
  16. #include <QDir>
  17. #include <QFile>
  18. // CTK includes
  19. #include "ctkErrorLogFDMessageHandler.h"
  20. #include "ctkErrorLogFDMessageHandler_p.h"
  21. // STD includes
  22. #include <cstdio>
  23. #ifdef Q_OS_WIN32
  24. # include <io.h>
  25. #else
  26. # include <unistd.h>
  27. #endif
  28. // --------------------------------------------------------------------------
  29. // ctkFDHandler methods
  30. // --------------------------------------------------------------------------
  31. ctkFDHandler::ctkFDHandler(ctkErrorLogFDMessageHandler* messageHandler,
  32. ctkErrorLogModel::LogLevel logLevel,
  33. int fileDescriptorNumber)
  34. {
  35. this->MessageHandler = messageHandler;
  36. this->LogLevel = logLevel;
  37. this->FDNumber = fileDescriptorNumber;
  38. this->SavedFDNumber = 0;
  39. this->FD = 0;
  40. this->Enabled = false;
  41. const QString outputFileTemplateName = QDir::tempPath()
  42. + QDir::separator () + "ctkFDHandler-%1.XXXXXX.txt";
  43. this->OutputFile.setFileTemplate(outputFileTemplateName.arg(this->FDNumber));
  44. connect(&this->OutputFileWatcher, SIGNAL(fileChanged(QString)),
  45. SLOT(outputFileChanged(QString)));
  46. }
  47. // --------------------------------------------------------------------------
  48. ctkErrorLogFDMessageHandler::~ctkErrorLogFDMessageHandler()
  49. {
  50. }
  51. // --------------------------------------------------------------------------
  52. FILE* ctkFDHandler::fileDescriptorFromNumber(int fdNumber)
  53. {
  54. Q_ASSERT(fdNumber == 1 /* stdout*/ || fdNumber == 2 /*stderr*/);
  55. if (fdNumber == 1)
  56. {
  57. return stdout;
  58. }
  59. else if (fdNumber == 2)
  60. {
  61. return stderr;
  62. }
  63. return 0;
  64. }
  65. // --------------------------------------------------------------------------
  66. void ctkFDHandler::setEnabled(bool value)
  67. {
  68. if (this->Enabled == value)
  69. {
  70. return;
  71. }
  72. if (value)
  73. {
  74. // Flush (stdout|stderr) so that any buffered messages are delivered
  75. fflush(Self::fileDescriptorFromNumber(this->FDNumber));
  76. // Save position of current standard output
  77. fgetpos(Self::fileDescriptorFromNumber(this->FDNumber), &this->SavedFDPos);
  78. #ifdef Q_OS_WIN32
  79. this->SavedFDNumber = _dup(_fileno(Self::fileDescriptorFromNumber(this->FDNumber)));
  80. #else
  81. this->SavedFDNumber = dup(fileno(Self::fileDescriptorFromNumber(this->FDNumber)));
  82. #endif
  83. // Open and close the OutputFile so that the unique filename is created
  84. if (!this->OutputFile.exists())
  85. {
  86. this->OutputFile.open();
  87. this->OutputFile.close();
  88. }
  89. //qDebug() << "ctkFDHandler - OutputFile" << this->OutputFile.fileName();
  90. if ((this->FD = freopen(this->OutputFile.fileName().toLatin1(),
  91. "w",
  92. Self::fileDescriptorFromNumber(this->FDNumber))) == 0)
  93. {
  94. // this->SavedFDNumber = 0;
  95. }
  96. // Observe the OutputFile for changes
  97. this->OutputFileWatcher.addPath(this->OutputFile.fileName());
  98. }
  99. else
  100. {
  101. // Flush stdout or stderr so that any buffered messages are delivered
  102. fflush(Self::fileDescriptorFromNumber(this->FDNumber));
  103. // Flush current stream so that any buffered messages are delivered
  104. fflush(this->FD);
  105. // Un-observe OutputFile
  106. this->OutputFileWatcher.removePath(this->OutputFile.fileName());
  107. // Close file and restore standard output to stdout or stderr - which should be the terminal
  108. #ifdef Q_OS_WIN32
  109. _dup2(this->SavedFDNumber, _fileno(Self::fileDescriptorFromNumber(this->FDNumber)));
  110. _close(this->SavedFDNumber);
  111. #else
  112. dup2(this->SavedFDNumber, fileno(Self::fileDescriptorFromNumber(this->FDNumber)));
  113. close(this->SavedFDNumber);
  114. #endif
  115. clearerr(Self::fileDescriptorFromNumber(this->FDNumber));
  116. fsetpos(Self::fileDescriptorFromNumber(this->FDNumber), &this->SavedFDPos);
  117. }
  118. this->Enabled = value;
  119. }
  120. // --------------------------------------------------------------------------
  121. void ctkFDHandler::outputFileChanged(const QString & path)
  122. {
  123. QFile file(path);
  124. if (!file.open(QFile::ReadOnly))
  125. {
  126. qCritical() << "ctkFDHandler - Failed to open file" << path;
  127. return;
  128. }
  129. QTextStream stream(&file);
  130. while (!stream.atEnd())
  131. {
  132. Q_ASSERT(this->MessageHandler->errorLogModel());
  133. this->MessageHandler->errorLogModel()->addEntry(
  134. this->LogLevel, this->MessageHandler->handlerPrettyName(), qPrintable(stream.readLine()));
  135. }
  136. }
  137. // --------------------------------------------------------------------------
  138. // ctkErrorLogFDMessageHandlerPrivate
  139. // --------------------------------------------------------------------------
  140. class ctkErrorLogFDMessageHandlerPrivate
  141. {
  142. public:
  143. ctkErrorLogFDMessageHandlerPrivate();
  144. ~ctkErrorLogFDMessageHandlerPrivate();
  145. ctkFDHandler * StdOutFDHandler;
  146. ctkFDHandler * StdErrFDHandler;
  147. };
  148. // --------------------------------------------------------------------------
  149. // ctkErrorLogFDMessageHandlerPrivate methods
  150. //------------------------------------------------------------------------------
  151. ctkErrorLogFDMessageHandlerPrivate::ctkErrorLogFDMessageHandlerPrivate()
  152. {
  153. }
  154. //------------------------------------------------------------------------------
  155. ctkErrorLogFDMessageHandlerPrivate::~ctkErrorLogFDMessageHandlerPrivate()
  156. {
  157. delete this->StdOutFDHandler;
  158. delete this->StdErrFDHandler;
  159. }
  160. //------------------------------------------------------------------------------
  161. // ctkErrorLogFDMessageHandler methods
  162. //------------------------------------------------------------------------------
  163. QString ctkErrorLogFDMessageHandler::HandlerName = QLatin1String("FD");
  164. // --------------------------------------------------------------------------
  165. ctkErrorLogFDMessageHandler::ctkErrorLogFDMessageHandler() :
  166. Superclass(), d_ptr(new ctkErrorLogFDMessageHandlerPrivate())
  167. {
  168. Q_D(ctkErrorLogFDMessageHandler);
  169. d->StdOutFDHandler = new ctkFDHandler(this, ctkErrorLogModel::Info, 1 /* stdout */);
  170. d->StdErrFDHandler = new ctkFDHandler(this, ctkErrorLogModel::Critical, 2 /* stderr */);
  171. }
  172. // --------------------------------------------------------------------------
  173. QString ctkErrorLogFDMessageHandler::handlerName()const
  174. {
  175. return ctkErrorLogFDMessageHandler::HandlerName;
  176. }
  177. // --------------------------------------------------------------------------
  178. void ctkErrorLogFDMessageHandler::setEnabledInternal(bool value)
  179. {
  180. Q_D(ctkErrorLogFDMessageHandler);
  181. d->StdOutFDHandler->setEnabled(value);
  182. d->StdErrFDHandler->setEnabled(value);
  183. }