ctkErrorLogFDMessageHandler.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. _close(this->Pipe[1]);
  97. #else
  98. this->SavedFDNumber = dup(fileno(this->terminalOutputFile()));
  99. dup2(this->Pipe[1], fileno(this->terminalOutputFile()));
  100. close(this->Pipe[1]);
  101. #endif
  102. // Start polling thread
  103. this->start();
  104. }
  105. else
  106. {
  107. // Flush stdout or stderr so that any buffered messages are delivered
  108. fflush(this->terminalOutputFile());
  109. // Flush current stream so that any buffered messages are delivered
  110. this->RedirectionFile.flush();
  111. // Stop polling thread
  112. this->terminate();
  113. this->wait();
  114. // Close files and restore standard output to stdout or stderr - which should be the terminal
  115. #ifdef Q_OS_WIN32
  116. _dup2(this->SavedFDNumber, _fileno(this->terminalOutputFile()));
  117. _close(this->SavedFDNumber);
  118. #else
  119. dup2(this->SavedFDNumber, fileno(this->terminalOutputFile()));
  120. close(this->SavedFDNumber);
  121. #endif
  122. clearerr(this->terminalOutputFile());
  123. fsetpos(this->terminalOutputFile(), &this->SavedFDPos);
  124. #ifdef Q_OS_WIN32
  125. this->SavedFDNumber = _fileno(this->terminalOutputFile());
  126. #else
  127. this->SavedFDNumber = fileno(this->terminalOutputFile());
  128. #endif
  129. }
  130. ctkErrorLogTerminalOutput * terminalOutput =
  131. this->MessageHandler->terminalOutput(this->TerminalOutput);
  132. if(terminalOutput)
  133. {
  134. terminalOutput->setFileDescriptor(this->SavedFDNumber);
  135. }
  136. this->Enabled = value;
  137. }
  138. // --------------------------------------------------------------------------
  139. void ctkFDHandler::run()
  140. {
  141. while(true)
  142. {
  143. char c = '\0';
  144. QString line;
  145. while(c != '\n')
  146. {
  147. #ifdef Q_OS_WIN32
  148. int res = _read(this->Pipe[0], &c, 1); // When used with pipe, read() is blocking
  149. #else
  150. ssize_t res = read(this->Pipe[0], &c, 1); // When used with pipe, read() is blocking
  151. #endif
  152. if (res == -1)
  153. {
  154. break;
  155. }
  156. if (c != '\n')
  157. {
  158. line += c;
  159. }
  160. }
  161. Q_ASSERT(this->MessageHandler);
  162. this->MessageHandler->handleMessage(
  163. ctk::qtHandleToString(QThread::currentThreadId()),
  164. this->LogLevel,
  165. this->MessageHandler->handlerPrettyName(),
  166. line);
  167. }
  168. }
  169. // --------------------------------------------------------------------------
  170. // ctkErrorLogFDMessageHandlerPrivate
  171. // --------------------------------------------------------------------------
  172. class ctkErrorLogFDMessageHandlerPrivate
  173. {
  174. public:
  175. ctkErrorLogFDMessageHandlerPrivate();
  176. ~ctkErrorLogFDMessageHandlerPrivate();
  177. ctkFDHandler * StdOutFDHandler;
  178. ctkFDHandler * StdErrFDHandler;
  179. };
  180. // --------------------------------------------------------------------------
  181. // ctkErrorLogFDMessageHandlerPrivate methods
  182. //------------------------------------------------------------------------------
  183. ctkErrorLogFDMessageHandlerPrivate::ctkErrorLogFDMessageHandlerPrivate()
  184. {
  185. }
  186. //------------------------------------------------------------------------------
  187. ctkErrorLogFDMessageHandlerPrivate::~ctkErrorLogFDMessageHandlerPrivate()
  188. {
  189. delete this->StdOutFDHandler;
  190. delete this->StdErrFDHandler;
  191. }
  192. //------------------------------------------------------------------------------
  193. // ctkErrorLogFDMessageHandler methods
  194. //------------------------------------------------------------------------------
  195. QString ctkErrorLogFDMessageHandler::HandlerName = QLatin1String("FD");
  196. // --------------------------------------------------------------------------
  197. ctkErrorLogFDMessageHandler::ctkErrorLogFDMessageHandler() :
  198. Superclass(), d_ptr(new ctkErrorLogFDMessageHandlerPrivate())
  199. {
  200. Q_D(ctkErrorLogFDMessageHandler);
  201. d->StdOutFDHandler = new ctkFDHandler(this, ctkErrorLogLevel::Info, ctkErrorLogModel::StandardOutput);
  202. d->StdErrFDHandler = new ctkFDHandler(this, ctkErrorLogLevel::Critical, ctkErrorLogModel::StandardError);
  203. }
  204. // --------------------------------------------------------------------------
  205. ctkErrorLogFDMessageHandler::~ctkErrorLogFDMessageHandler()
  206. {
  207. }
  208. // --------------------------------------------------------------------------
  209. QString ctkErrorLogFDMessageHandler::handlerName()const
  210. {
  211. return ctkErrorLogFDMessageHandler::HandlerName;
  212. }
  213. // --------------------------------------------------------------------------
  214. void ctkErrorLogFDMessageHandler::setEnabledInternal(bool value)
  215. {
  216. Q_D(ctkErrorLogFDMessageHandler);
  217. d->StdOutFDHandler->setEnabled(value);
  218. d->StdErrFDHandler->setEnabled(value);
  219. }