debugappender.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: debugappender.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * Copyright 2007 Martin Heinrich
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ******************************************************************************/
  24. /******************************************************************************
  25. * Dependencies
  26. ******************************************************************************/
  27. #include "log4qt/varia/debugappender.h"
  28. #include <QtCore/QDebug>
  29. #include <stdio.h>
  30. #include "log4qt/layout.h"
  31. #include "log4qt/loggingevent.h"
  32. #if defined(Q_WS_WIN) || defined(Q_OS_WIN32)
  33. #include <Windows.h>
  34. #endif
  35. namespace Log4Qt
  36. {
  37. /**************************************************************************
  38. * Declarations
  39. **************************************************************************/
  40. /**************************************************************************
  41. * C helper functions
  42. **************************************************************************/
  43. /**************************************************************************
  44. * Class implementation: DebugAppender
  45. **************************************************************************/
  46. DebugAppender::DebugAppender(Layout *pLayout,
  47. QObject *pParent) :
  48. AppenderSkeleton(pParent)
  49. {
  50. setLayout(pLayout);
  51. }
  52. bool DebugAppender::requiresLayout() const
  53. {
  54. return true;
  55. }
  56. void DebugAppender::append(const LoggingEvent &rEvent)
  57. {
  58. // Q_ASSERT_X(, "DebugAppender::append()", "Lock must be held by caller");
  59. Q_ASSERT_X(layout(), "DebugAppender::append()", "Layout must not be null");
  60. QString message(layout()->format(rEvent));
  61. #if defined(Q_OS_WIN32) || defined(Q_WS_WIN)
  62. #if (QT_VERSION < 0x050000)
  63. QT_WA({
  64. OutputDebugStringW(reinterpret_cast<const WCHAR*>(message.utf16()));
  65. }, {
  66. OutputDebugStringA(message.toLocal8Bit().data());
  67. });
  68. #else
  69. OutputDebugStringW(reinterpret_cast<const WCHAR*>(message.utf16()));
  70. #endif
  71. #else
  72. fprintf(stderr, message.toLocal8Bit().data());
  73. fflush(stderr);
  74. #endif
  75. }
  76. /**************************************************************************
  77. * Implementation: Operators, Helper
  78. **************************************************************************/
  79. #ifndef QT_NO_DEBUG_STREAM
  80. QDebug DebugAppender::debug(QDebug &rDebug) const
  81. {
  82. QString layout_name;
  83. if (layout())
  84. layout_name = layout()->name();
  85. rDebug.nospace() << "DebugAppender("
  86. << "name:" << name() << " "
  87. << "filter:" << firstFilter() << " "
  88. << "isactive:" << isActive() << " "
  89. << "isclosed:" << isClosed() << " "
  90. << "layout:" << layout_name << " "
  91. << "referencecount:" << referenceCount() << " "
  92. << "threshold:" << threshold().toString()
  93. << ")";
  94. return rDebug.space();
  95. }
  96. #endif // QT_NO_DEBUG_STREAM
  97. } // namspace Log4Qt