ndc.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: ndc.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * changes Feb 2009, Martin Heinrich
  10. * - Fixed VS 2008 unreferenced formal parameter warning by using
  11. * Q_UNUSED in operator<<.
  12. *
  13. *
  14. * Copyright 2007 - 2009 Martin Heinrich
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. *****************************************************************************/
  29. /******************************************************************************
  30. * Dependencies
  31. ******************************************************************************/
  32. #include "log4qt/ndc.h"
  33. #include <QtCore/QDebug>
  34. #include <QtCore/QMutex>
  35. #include <QtCore/QThread>
  36. #include "log4qt/helpers/initialisationhelper.h"
  37. #include "log4qt/logger.h"
  38. namespace Log4Qt
  39. {
  40. /**************************************************************************
  41. * Declarations
  42. **************************************************************************/
  43. /**************************************************************************
  44. * C helper functions
  45. **************************************************************************/
  46. LOG4QT_DECLARE_STATIC_LOGGER(logger, Log4Qt:NDC)
  47. /**************************************************************************
  48. * Class implementation: NDC
  49. **************************************************************************/
  50. void NDC::clear()
  51. {
  52. if (!instance()->mStack.hasLocalData())
  53. return;
  54. instance()->mStack.localData()->clear();
  55. }
  56. int NDC::depth()
  57. {
  58. if (!instance()->mStack.hasLocalData())
  59. return 0;
  60. return instance()->mStack.localData()->count();
  61. }
  62. LOG4QT_IMPLEMENT_INSTANCE(NDC)
  63. QString NDC::pop()
  64. {
  65. if (!instance()->mStack.hasLocalData() || instance()->mStack.localData()->isEmpty())
  66. {
  67. logger()->warn("Requesting pop from empty NDC stack");
  68. return QString();
  69. }
  70. return instance()->mStack.localData()->pop();
  71. }
  72. void NDC::push(const QString &rMessage)
  73. {
  74. if (!instance()->mStack.hasLocalData())
  75. instance()->mStack.setLocalData(new QStack<QString>);
  76. instance()->mStack.localData()->push(rMessage);
  77. }
  78. void NDC::setMaxDepth(int maxDepth)
  79. {
  80. if (!instance()->mStack.hasLocalData() ||
  81. instance()->mStack.localData()->size() <= maxDepth)
  82. return;
  83. instance()->mStack.localData()->resize(maxDepth);
  84. }
  85. QString NDC::peek()
  86. {
  87. if (!instance()->mStack.hasLocalData() ||
  88. instance()->mStack.localData()->isEmpty())
  89. return QString();
  90. return instance()->mStack.localData()->top();
  91. }
  92. /**************************************************************************
  93. * Implementation: Operators, Helper
  94. **************************************************************************/
  95. #ifndef QT_NO_DEBUG_STREAM
  96. QDebug operator<<(QDebug debug,
  97. const NDC &rNDC)
  98. {
  99. Q_UNUSED(rNDC); // To avoid warning C4100 on VS 2008
  100. debug.nospace() << "NDC("
  101. << "thread:" << QThread::currentThread()->objectName() << " "
  102. << "peek:" << rNDC.peek() << " "
  103. << "depth:" << rNDC.depth()
  104. << ")";
  105. return debug.space();
  106. }
  107. #endif // QT_NO_DEBUG_STREAM
  108. } // namespace Log4Qt