mdc.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: mdc.cpp
  5. * created: September 2007
  6. * author: Martin Heinrich
  7. *
  8. *
  9. * changes Feb 2009, Martin Heinrich
  10. * - Fixed 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/mdc.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. /**************************************************************************
  47. * Class implementation: MDC
  48. **************************************************************************/
  49. QString MDC::get(const QString &rKey)
  50. {
  51. if (!instance()->mHash.hasLocalData())
  52. return QString();
  53. return instance()->mHash.localData()->value(rKey);
  54. }
  55. QHash<QString, QString> MDC::context()
  56. {
  57. if (!instance()->mHash.hasLocalData())
  58. return QHash<QString, QString>();
  59. return *instance()->mHash.localData();
  60. }
  61. LOG4QT_IMPLEMENT_INSTANCE(MDC)
  62. QHash<QString, QString> *MDC::localData()
  63. {
  64. if (!instance()->mHash.hasLocalData())
  65. instance()->mHash.setLocalData(new QHash<QString, QString>);
  66. return instance()->mHash.localData();
  67. }
  68. /**************************************************************************
  69. * Implementation: Operators, Helper
  70. **************************************************************************/
  71. #ifndef QT_NO_DEBUG_STREAM
  72. QDebug operator<<(QDebug debug, const MDC &rMDC)
  73. {
  74. Q_UNUSED(rMDC); // To avoid warning C4100 on VS 2008
  75. debug.nospace() << "MDC("
  76. << "thread:" << QThread::currentThread()->objectName() << " "
  77. << "context:" << rMDC.context()
  78. << ")";
  79. return debug.space();
  80. }
  81. #endif // QT_NO_DEBUG_STREAM
  82. } // namespace Log4Qt