ctkErrorLogFDMessageHandler.cpp 6.8 KB

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