ctkErrorLogTerminalOutput.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "ctkErrorLogTerminalOutput.h"
  15. // Qt includes
  16. #include <QMutex>
  17. #include <QString>
  18. // STD includes
  19. #include <cstdio> // For _fileno or fileno
  20. #ifdef _MSC_VER
  21. # include <io.h> // For _write()
  22. #else
  23. # include <unistd.h>
  24. #endif
  25. // --------------------------------------------------------------------------
  26. // ctkErrorLogTerminalOutputPrivate
  27. // --------------------------------------------------------------------------
  28. class ctkErrorLogTerminalOutputPrivate
  29. {
  30. public:
  31. ctkErrorLogTerminalOutputPrivate();
  32. ~ctkErrorLogTerminalOutputPrivate();
  33. bool Enabled;
  34. mutable QMutex EnableMutex;
  35. int FD;
  36. mutable QMutex OutputMutex;
  37. };
  38. // --------------------------------------------------------------------------
  39. ctkErrorLogTerminalOutputPrivate::ctkErrorLogTerminalOutputPrivate()
  40. : Enabled(false)
  41. {
  42. #ifdef Q_OS_WIN32
  43. this->FD = _fileno(stdout);
  44. #else
  45. this->FD = fileno(stdout);
  46. #endif
  47. }
  48. // --------------------------------------------------------------------------
  49. ctkErrorLogTerminalOutputPrivate::~ctkErrorLogTerminalOutputPrivate()
  50. {
  51. }
  52. // --------------------------------------------------------------------------
  53. // ctkErrorLogTerminalOutput methods
  54. // --------------------------------------------------------------------------
  55. ctkErrorLogTerminalOutput::ctkErrorLogTerminalOutput()
  56. : d_ptr(new ctkErrorLogTerminalOutputPrivate)
  57. {
  58. }
  59. // --------------------------------------------------------------------------
  60. ctkErrorLogTerminalOutput::~ctkErrorLogTerminalOutput()
  61. {
  62. }
  63. // --------------------------------------------------------------------------
  64. bool ctkErrorLogTerminalOutput::enabled()const
  65. {
  66. Q_D(const ctkErrorLogTerminalOutput);
  67. QMutexLocker locker(&d->EnableMutex);
  68. return d->Enabled;
  69. }
  70. // --------------------------------------------------------------------------
  71. void ctkErrorLogTerminalOutput::setEnabled(bool value)
  72. {
  73. Q_D(ctkErrorLogTerminalOutput);
  74. QMutexLocker locker(&d->EnableMutex);
  75. d->Enabled = value;
  76. }
  77. // --------------------------------------------------------------------------
  78. int ctkErrorLogTerminalOutput::fileDescriptor()const
  79. {
  80. Q_D(const ctkErrorLogTerminalOutput);
  81. QMutexLocker locker(&d->OutputMutex);
  82. return d->FD;
  83. }
  84. // --------------------------------------------------------------------------
  85. void ctkErrorLogTerminalOutput::setFileDescriptor(int fd)
  86. {
  87. Q_D(ctkErrorLogTerminalOutput);
  88. QMutexLocker locker(&d->OutputMutex);
  89. d->FD = fd;
  90. }
  91. // --------------------------------------------------------------------------
  92. void ctkErrorLogTerminalOutput::output(const QString& text)
  93. {
  94. Q_D(ctkErrorLogTerminalOutput);
  95. {
  96. QMutexLocker locker(&d->EnableMutex);
  97. if (!d->Enabled)
  98. {
  99. return;
  100. }
  101. }
  102. {
  103. QMutexLocker locker(&d->OutputMutex);
  104. QString textWithNewLine = text + "\n";
  105. #ifdef _MSC_VER
  106. int res = _write(d->FD, qPrintable(textWithNewLine), textWithNewLine.size());
  107. #else
  108. ssize_t res = write(d->FD, qPrintable(textWithNewLine), textWithNewLine.size());
  109. #endif
  110. if (res == -1)
  111. {
  112. return;
  113. }
  114. }
  115. }