00001 /****************************************************************************** 00002 * 00003 * package: Log4Qt 00004 * file: mdc.h 00005 * created: September 2007 00006 * author: Martin Heinrich 00007 * 00008 * 00009 * Copyright 2007 Martin Heinrich 00010 * 00011 * Licensed under the Apache License, Version 2.0 (the "License"); 00012 * you may not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at 00014 * 00015 * http://www.apache.org/licenses/LICENSE-2.0 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, 00019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 * 00023 ******************************************************************************/ 00024 00025 #ifndef LOG4QT_MDC_H 00026 #define LOG4QT_MDC_H 00027 00028 00029 /****************************************************************************** 00030 * Dependencies 00031 ******************************************************************************/ 00032 00033 #include <QtCore/QString> 00034 #include <QtCore/QHash> 00035 #include <QtCore/QThreadStorage> 00036 #include "log4qt/log4qt.h" 00037 00038 00039 /****************************************************************************** 00040 * Declarations 00041 ******************************************************************************/ 00042 00043 namespace Log4Qt 00044 { 00045 00046 00052 class MDC 00053 { 00054 private: 00055 MDC(); 00056 MDC(const MDC &rOther); // Not implemented 00057 // virtual ~MDC(); // Use compiler default 00058 MDC &operator=(const MDC &rOther); // Not implemented 00059 00060 public: 00061 static QString get(const QString &rKey); 00062 static QHash<QString, QString> context(); 00063 00067 static MDC *instance(); 00068 00069 static void put(const QString &rKey, const QString &rValue); 00070 static void remove(const QString &rKey); 00071 00072 private: 00073 static QHash<QString, QString> *localData(); 00074 00075 private: 00076 QThreadStorage< QHash<QString, QString> * > mHash; 00077 }; 00078 00079 00080 /************************************************************************** 00081 * Operators, Helper 00082 **************************************************************************/ 00083 00084 #ifndef QT_NO_DEBUG_STREAM 00085 00096 QDebug operator<<(QDebug debug, 00097 const MDC &rMDC); 00098 #endif // QT_NO_DEBUG_STREAM 00099 00100 00101 /************************************************************************** 00102 * Inline 00103 **************************************************************************/ 00104 00105 inline MDC::MDC() : 00106 mHash() 00107 {} 00108 00109 inline void MDC::put(const QString &rKey, const QString &rValue) 00110 { localData()->insert(rKey, rValue); } 00111 00112 inline void MDC::remove(const QString &rKey) 00113 { localData()->remove(rKey); } 00114 00115 00116 } // namespace Log4Qt 00117 00118 00119 // Q_DECLARE_TYPEINFO(Log4Qt::MDC, Q_COMPLEX_TYPE); // Use default 00120 00121 00122 #endif // LOG4QT_MDC_H