ctkErrorLogFDMessageHandler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // CTK includes
  17. #include "ctkErrorLogFDMessageHandler.h"
  18. #include "ctkErrorLogFDMessageHandler_p.h"
  19. #include "ctkUtils.h"
  20. // STD includes
  21. #include <cstdio>
  22. #ifdef Q_OS_WIN32
  23. # include <fcntl.h> // For _O_TEXT
  24. # include <io.h> // For _pipe, _dup and _dup2
  25. #else
  26. # include <unistd.h> // For pipe, dup and dup2
  27. #endif
  28. // --------------------------------------------------------------------------
  29. // ctkFDHandler methods
  30. // See http://stackoverflow.com/questions/5419356/redirect-stdout-stderr-to-a-string
  31. // and http://stackoverflow.com/questions/955962/how-to-buffer-stdout-in-memory-and-write-it-from-a-dedicated-thread
  32. // --------------------------------------------------------------------------
  33. ctkFDHandler::ctkFDHandler(ctkErrorLogFDMessageHandler* messageHandler,
  34. ctkErrorLogLevel::LogLevel logLevel,
  35. ctkErrorLogModel::TerminalOutput terminalOutput)
  36. {
  37. this->MessageHandler = messageHandler;
  38. this->LogLevel = logLevel;
  39. this->TerminalOutput = terminalOutput;
  40. this->SavedFDNumber = 0;
  41. this->Enabled = false;
  42. this->Initialized = false;
  43. this->init();
  44. }
  45. // --------------------------------------------------------------------------
  46. ctkFDHandler::~ctkFDHandler()
  47. {
  48. if (this->Initialized)
  49. {
  50. this->RedirectionFile.close();
  51. delete this->RedirectionStream;
  52. }
  53. }
  54. // --------------------------------------------------------------------------
  55. void ctkFDHandler::init()
  56. {
  57. #ifdef Q_OS_WIN32
  58. int status = _pipe(this->Pipe, 65536, _O_TEXT);
  59. #else
  60. int status = pipe(this->Pipe);
  61. #endif
  62. if (status != 0)
  63. {
  64. qCritical().nospace() << "ctkFDHandler - Failed to create pipe !";
  65. return;
  66. }
  67. this->RedirectionFile.open(this->Pipe[0], QIODevice::ReadOnly);
  68. this->RedirectionStream = new QTextStream(&this->RedirectionFile);
  69. this->Initialized = true;
  70. }
  71. // --------------------------------------------------------------------------
  72. FILE* ctkFDHandler::terminalOutputFile()
  73. {
  74. return this->TerminalOutput == ctkErrorLogModel::StandardOutput ? stdout : stderr;
  75. }
  76. // --------------------------------------------------------------------------
  77. void ctkFDHandler::setEnabled(bool value)
  78. {
  79. if (!this->Initialized)
  80. {
  81. return;
  82. }
  83. if (this->Enabled == value)
  84. {
  85. return;
  86. }
  87. if (value)
  88. {
  89. // Flush (stdout|stderr) so that any buffered messages are delivered
  90. fflush(this->terminalOutputFile());
  91. // Save position of current standard output
  92. fgetpos(this->terminalOutputFile(), &this->SavedFDPos);
  93. #ifdef Q_OS_WIN32
  94. this->SavedFDNumber = _dup(_fileno(this->terminalOutputFile()));
  95. _dup2(this->Pipe[1], _fileno(this->terminalOutputFile()));
  96. #else
  97. this->SavedFDNumber = dup(fileno(this->terminalOutputFile()));
  98. dup2(this->Pipe[1], fileno(this->terminalOutputFile()));
  99. #endif
  100. close(this->Pipe[1]);
  101. // Start polling thread
  102. this->start();
  103. }
  104. else
  105. {
  106. // Flush stdout or stderr so that any buffered messages are delivered
  107. fflush(this->terminalOutputFile());
  108. // Flush current stream so that any buffered messages are delivered
  109. this->RedirectionFile.flush();
  110. // Stop polling thread
  111. this->terminate();
  112. this->wait();
  113. // Close files and restore standard output to stdout or stderr - which should be the terminal
  114. #ifdef Q_OS_WIN32
  115. _dup2(this->SavedFDNumber, _fileno(this->terminalOutputFile()));
  116. _close(this->SavedFDNumber);
  117. #else
  118. dup2(this->SavedFDNumber, fileno(this->terminalOutputFile()));
  119. close(this->SavedFDNumber);
  120. #endif
  121. clearerr(this->terminalOutputFile());
  122. fsetpos(this->terminalOutputFile(), &this->SavedFDPos);
  123. this->SavedFDNumber = fileno(this->terminalOutputFile());
  124. }
  125. ctkErrorLogTerminalOutput * terminalOutput =
  126. this->MessageHandler->terminalOutput(this->TerminalOutput);
  127. if(terminalOutput)
  128. {
  129. terminalOutput->setFileDescriptor(this->SavedFDNumber);
  130. }
  131. this->Enabled = value;
  132. }
  133. // --------------------------------------------------------------------------
  134. void ctkFDHandler::run()
  135. {
  136. while(true)
  137. {
  138. char c = '\0';
  139. QString line;
  140. while(c != '\n')
  141. {
  142. read(this->Pipe[0], &c, 1); // When used with pipe, read() is blocking
  143. if (c != '\n')
  144. {
  145. line += c;
  146. }
  147. }
  148. Q_ASSERT(this->MessageHandler);
  149. this->MessageHandler->handleMessage(
  150. ctk::qtHandleToString(QThread::currentThreadId()),
  151. this->LogLevel,
  152. this->MessageHandler->handlerPrettyName(),
  153. line);
  154. }
  155. }
  156. // --------------------------------------------------------------------------
  157. // ctkErrorLogFDMessageHandlerPrivate
  158. // --------------------------------------------------------------------------
  159. class ctkErrorLogFDMessageHandlerPrivate
  160. {
  161. public:
  162. ctkErrorLogFDMessageHandlerPrivate();
  163. ~ctkErrorLogFDMessageHandlerPrivate();
  164. ctkFDHandler * StdOutFDHandler;
  165. ctkFDHandler * StdErrFDHandler;
  166. };
  167. // --------------------------------------------------------------------------
  168. // ctkErrorLogFDMessageHandlerPrivate methods
  169. //------------------------------------------------------------------------------
  170. ctkErrorLogFDMessageHandlerPrivate::ctkErrorLogFDMessageHandlerPrivate()
  171. {
  172. }
  173. //------------------------------------------------------------------------------
  174. ctkErrorLogFDMessageHandlerPrivate::~ctkErrorLogFDMessageHandlerPrivate()
  175. {
  176. delete this->StdOutFDHandler;
  177. delete this->StdErrFDHandler;
  178. }
  179. //------------------------------------------------------------------------------
  180. // ctkErrorLogFDMessageHandler methods
  181. //------------------------------------------------------------------------------
  182. QString ctkErrorLogFDMessageHandler::HandlerName = QLatin1String("FD");
  183. // --------------------------------------------------------------------------
  184. ctkErrorLogFDMessageHandler::ctkErrorLogFDMessageHandler() :
  185. Superclass(), d_ptr(new ctkErrorLogFDMessageHandlerPrivate())
  186. {
  187. Q_D(ctkErrorLogFDMessageHandler);
  188. d->StdOutFDHandler = new ctkFDHandler(this, ctkErrorLogLevel::Info, ctkErrorLogModel::StandardOutput);
  189. d->StdErrFDHandler = new ctkFDHandler(this, ctkErrorLogLevel::Critical, ctkErrorLogModel::StandardError);
  190. }
  191. // --------------------------------------------------------------------------
  192. ctkErrorLogFDMessageHandler::~ctkErrorLogFDMessageHandler()
  193. {
  194. }
  195. // --------------------------------------------------------------------------
  196. QString ctkErrorLogFDMessageHandler::handlerName()const
  197. {
  198. return ctkErrorLogFDMessageHandler::HandlerName;
  199. }
  200. // --------------------------------------------------------------------------
  201. void ctkErrorLogFDMessageHandler::setEnabledInternal(bool value)
  202. {
  203. Q_D(ctkErrorLogFDMessageHandler);
  204. d->StdOutFDHandler->setEnabled(value);
  205. d->StdErrFDHandler->setEnabled(value);
  206. }