ctkErrorLogAbstractMessageHandler.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "ctkErrorLogAbstractMessageHandler.h"
  15. // Qt includes
  16. #include <QHash>
  17. #include <QDateTime>
  18. // CTK includes
  19. #include "ctkErrorLogContext.h"
  20. // --------------------------------------------------------------------------
  21. // ctkErrorLogAbstractMessageHandlerPrivate
  22. // --------------------------------------------------------------------------
  23. class ctkErrorLogAbstractMessageHandlerPrivate
  24. {
  25. public:
  26. ctkErrorLogAbstractMessageHandlerPrivate();
  27. ~ctkErrorLogAbstractMessageHandlerPrivate();
  28. bool Enabled;
  29. QString HandlerPrettyName;
  30. // Use "int" instead of "ctkErrorLogModel::TerminalOutput" to avoid compilation warning ...
  31. // qhash.h:879: warning: passing 'ctkErrorLogModel::TerminalOutput' chooses 'int' over 'uint' [-Wsign-promo]
  32. QHash<int, ctkErrorLogTerminalOutput*> TerminalOutputs;
  33. };
  34. // --------------------------------------------------------------------------
  35. ctkErrorLogAbstractMessageHandlerPrivate::
  36. ctkErrorLogAbstractMessageHandlerPrivate()
  37. : Enabled(false)
  38. {
  39. }
  40. // --------------------------------------------------------------------------
  41. ctkErrorLogAbstractMessageHandlerPrivate::~ctkErrorLogAbstractMessageHandlerPrivate()
  42. {
  43. }
  44. // --------------------------------------------------------------------------
  45. // ctkErrorLogAbstractMessageHandlerPrivate methods
  46. // --------------------------------------------------------------------------
  47. ctkErrorLogAbstractMessageHandler::ctkErrorLogAbstractMessageHandler()
  48. : Superclass(), d_ptr(new ctkErrorLogAbstractMessageHandlerPrivate)
  49. {
  50. }
  51. // --------------------------------------------------------------------------
  52. ctkErrorLogAbstractMessageHandler::~ctkErrorLogAbstractMessageHandler()
  53. {
  54. }
  55. // --------------------------------------------------------------------------
  56. QString ctkErrorLogAbstractMessageHandler::handlerPrettyName()const
  57. {
  58. Q_D(const ctkErrorLogAbstractMessageHandler);
  59. if (d->HandlerPrettyName.isEmpty())
  60. {
  61. return this->handlerName();
  62. }
  63. else
  64. {
  65. return d->HandlerPrettyName;
  66. }
  67. }
  68. // --------------------------------------------------------------------------
  69. void ctkErrorLogAbstractMessageHandler::setHandlerPrettyName(const QString& newHandlerPrettyName)
  70. {
  71. Q_D(ctkErrorLogAbstractMessageHandler);
  72. d->HandlerPrettyName = newHandlerPrettyName;
  73. }
  74. // --------------------------------------------------------------------------
  75. bool ctkErrorLogAbstractMessageHandler::enabled()const
  76. {
  77. Q_D(const ctkErrorLogAbstractMessageHandler);
  78. return d->Enabled;
  79. }
  80. // --------------------------------------------------------------------------
  81. void ctkErrorLogAbstractMessageHandler::setEnabled(bool value)
  82. {
  83. Q_D(ctkErrorLogAbstractMessageHandler);
  84. if (value == d->Enabled)
  85. {
  86. return;
  87. }
  88. this->setEnabledInternal(value);
  89. d->Enabled = value;
  90. }
  91. // --------------------------------------------------------------------------
  92. void ctkErrorLogAbstractMessageHandler::handleMessage(const QString& threadId,
  93. ctkErrorLogLevel::LogLevel logLevel,
  94. const QString& origin,
  95. const ctkErrorLogContext& logContext,
  96. const QString &text)
  97. {
  98. Q_D(ctkErrorLogAbstractMessageHandler);
  99. if (logLevel <= ctkErrorLogLevel::Info)
  100. {
  101. if(d->TerminalOutputs.contains(ctkErrorLogTerminalOutput::StandardOutput))
  102. {
  103. d->TerminalOutputs.value(ctkErrorLogTerminalOutput::StandardOutput)->output(text);
  104. }
  105. }
  106. else
  107. {
  108. if(d->TerminalOutputs.contains(ctkErrorLogTerminalOutput::StandardError))
  109. {
  110. d->TerminalOutputs.value(ctkErrorLogTerminalOutput::StandardError)->output(text);
  111. }
  112. }
  113. emit this->messageHandled(QDateTime::currentDateTime(), threadId, logLevel, origin, logContext, text);
  114. }
  115. // --------------------------------------------------------------------------
  116. ctkErrorLogTerminalOutput* ctkErrorLogAbstractMessageHandler::terminalOutput(
  117. ctkErrorLogTerminalOutput::TerminalOutput terminalOutputType)const
  118. {
  119. Q_D(const ctkErrorLogAbstractMessageHandler);
  120. if(d->TerminalOutputs.contains(terminalOutputType))
  121. {
  122. return d->TerminalOutputs.value(terminalOutputType);
  123. }
  124. return 0;
  125. }
  126. // --------------------------------------------------------------------------
  127. void ctkErrorLogAbstractMessageHandler::setTerminalOutput(
  128. ctkErrorLogTerminalOutput::TerminalOutput terminalOutputType, ctkErrorLogTerminalOutput* terminalOutput)
  129. {
  130. Q_D(ctkErrorLogAbstractMessageHandler);
  131. d->TerminalOutputs.insert(terminalOutputType, terminalOutput);
  132. }