mdc.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /******************************************************************************
  2. *
  3. * package: Log4Qt
  4. * file: mdc.h
  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. #ifndef LOG4QT_MDC_H
  25. #define LOG4QT_MDC_H
  26. /******************************************************************************
  27. * Dependencies
  28. ******************************************************************************/
  29. #include <QtCore/QString>
  30. #include <QtCore/QHash>
  31. #include <QtCore/QThreadStorage>
  32. #include "log4qt/log4qt.h"
  33. /******************************************************************************
  34. * Declarations
  35. ******************************************************************************/
  36. namespace Log4Qt
  37. {
  38. /*!
  39. * \brief The class MDC implements a mapped diagnostic context.
  40. *
  41. * \note All the functions declared in this class are thread-safe.
  42. */
  43. class MDC
  44. {
  45. private:
  46. MDC();
  47. MDC(const MDC &rOther); // Not implemented
  48. // virtual ~MDC(); // Use compiler default
  49. MDC &operator=(const MDC &rOther); // Not implemented
  50. public:
  51. static QString get(const QString &rKey);
  52. static QHash<QString, QString> context();
  53. /*!
  54. * Returns the MDC instance.
  55. */
  56. static MDC *instance();
  57. static void put(const QString &rKey, const QString &rValue);
  58. static void remove(const QString &rKey);
  59. private:
  60. static QHash<QString, QString> *localData();
  61. private:
  62. QThreadStorage< QHash<QString, QString> * > mHash;
  63. };
  64. /**************************************************************************
  65. * Operators, Helper
  66. **************************************************************************/
  67. #ifndef QT_NO_DEBUG_STREAM
  68. /*!
  69. * \relates MDC
  70. *
  71. * Writes all object member variables to the given debug stream \a rDebug
  72. * and returns the stream.
  73. *
  74. * <tt>
  75. * %MDC(thread:"main" context:QHash(("login", "Peter")("database", "UAT")) )
  76. * </tt>
  77. * \sa QDebug
  78. */
  79. QDebug operator<<(QDebug debug,
  80. const MDC &rMDC);
  81. #endif // QT_NO_DEBUG_STREAM
  82. /**************************************************************************
  83. * Inline
  84. **************************************************************************/
  85. inline MDC::MDC() :
  86. mHash()
  87. {}
  88. inline void MDC::put(const QString &rKey, const QString &rValue)
  89. { localData()->insert(rKey, rValue); }
  90. inline void MDC::remove(const QString &rKey)
  91. { localData()->remove(rKey); }
  92. } // namespace Log4Qt
  93. // Q_DECLARE_TYPEINFO(Log4Qt::MDC, Q_COMPLEX_TYPE); // Use default
  94. #endif // LOG4QT_MDC_H