ctkErrorLogAbstractMessageHandler.cpp 5.1 KB

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