| 1 | /*
|
|---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
|---|
| 3 | * contributor license agreements. See the NOTICE file distributed with
|
|---|
| 4 | * this work for additional information regarding copyright ownership.
|
|---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
|---|
| 6 | * (the "License"); you may not use this file except in compliance with
|
|---|
| 7 | * the License. You may obtain a copy of the License at
|
|---|
| 8 | *
|
|---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0
|
|---|
| 10 | *
|
|---|
| 11 | * Unless required by applicable law or agreed to in writing, software
|
|---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
|---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|---|
| 14 | * See the License for the specific language governing permissions and
|
|---|
| 15 | * limitations under the License.
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | #ifndef _LOG4CXX_MDC_H
|
|---|
| 19 | #define _LOG4CXX_MDC_H
|
|---|
| 20 |
|
|---|
| 21 | #if defined(_MSC_VER)
|
|---|
| 22 | #pragma warning (push)
|
|---|
| 23 | #pragma warning ( disable: 4231 4251 4275 4786 )
|
|---|
| 24 | #endif
|
|---|
| 25 |
|
|---|
| 26 | #include <log4cxx/log4cxx.h>
|
|---|
| 27 | #include <log4cxx/logstring.h>
|
|---|
| 28 | #include <map>
|
|---|
| 29 |
|
|---|
| 30 | namespace log4cxx
|
|---|
| 31 | {
|
|---|
| 32 | /**
|
|---|
| 33 | The MDC class is similar to the {@link log4cxx::NDC NDC} class except that it is
|
|---|
| 34 | based on a map instead of a stack. It provides <em>mapped
|
|---|
| 35 | diagnostic contexts</em>. A <em>Mapped Diagnostic Context</em>, or
|
|---|
| 36 | MDC in short, is an instrument for distinguishing interleaved log
|
|---|
| 37 | output from different sources. Log output is typically interleaved
|
|---|
| 38 | when a server handles multiple clients near-simultaneously.
|
|---|
| 39 |
|
|---|
| 40 | <p><b><em>The MDC is managed on a per thread basis</em></b>. A
|
|---|
| 41 | child thread automatically inherits a <em>copy</em> of the mapped
|
|---|
| 42 | diagnostic context of its parent.
|
|---|
| 43 |
|
|---|
| 44 | */
|
|---|
| 45 | class LOG4CXX_EXPORT MDC
|
|---|
| 46 | {
|
|---|
| 47 | public:
|
|---|
| 48 | /** String to string stl map.
|
|---|
| 49 | */
|
|---|
| 50 | typedef std::map<LogString, LogString> Map;
|
|---|
| 51 |
|
|---|
| 52 | /**
|
|---|
| 53 | * Places a key/value pair in the MDC for the current thread
|
|---|
| 54 | * which will be removed during the corresponding destructor. Both
|
|---|
| 55 | * construction and destruction are expected to be on the same thread.
|
|---|
| 56 | * @param key key
|
|---|
| 57 | * @param value value.
|
|---|
| 58 | */
|
|---|
| 59 | MDC(const std::string& key, const std::string& value);
|
|---|
| 60 | ~MDC();
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Put a context value (the <code>o</code> parameter) as identified
|
|---|
| 64 | * with the <code>key</code> parameter into the current thread's
|
|---|
| 65 | * context map.
|
|---|
| 66 | *
|
|---|
| 67 | * <p>If the current thread does not have a context map it is
|
|---|
| 68 | * created as a side effect.
|
|---|
| 69 | * @param key key
|
|---|
| 70 | * @param value value.
|
|---|
| 71 | */
|
|---|
| 72 | static void put(const std::string& key, const std::string& value);
|
|---|
| 73 | /**
|
|---|
| 74 | * Put a context value (the <code>o</code> parameter) as identified
|
|---|
| 75 | * with the <code>key</code> parameter into the current thread's
|
|---|
| 76 | * context map.
|
|---|
| 77 | *
|
|---|
| 78 | * <p>If the current thread does not have a context map it is
|
|---|
| 79 | * created as a side effect.
|
|---|
| 80 | * */
|
|---|
| 81 | static void putLS(const LogString& key, const LogString& value);
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Get the context identified by the <code>key</code> parameter.
|
|---|
| 85 | *
|
|---|
| 86 | * <p>This method has no side effects.
|
|---|
| 87 | * @param key key.
|
|---|
| 88 | * @return value for key, empty if not set.
|
|---|
| 89 | * */
|
|---|
| 90 | static std::string get(const std::string& key);
|
|---|
| 91 | /**
|
|---|
| 92 | * Gets the context identified by the <code>key</code> parameter.
|
|---|
| 93 | * @param key context key.
|
|---|
| 94 | * @param dest destination to which value is appended.
|
|---|
| 95 | * @return true if key has associated value.
|
|---|
| 96 | */
|
|---|
| 97 | static bool get(const LogString& key, LogString& dest);
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Remove the the context identified by the <code>key</code>
|
|---|
| 101 | * parameter.
|
|---|
| 102 | * @param key key.
|
|---|
| 103 | * @return value if key had been set, empty if not.
|
|---|
| 104 | */
|
|---|
| 105 | static std::string remove(const std::string& key);
|
|---|
| 106 | #if LOG4CXX_WCHAR_T_API
|
|---|
| 107 | /**
|
|---|
| 108 | * Places a key/value pair in the MDC for the current thread
|
|---|
| 109 | * which will be removed during the corresponding destructor. Both
|
|---|
| 110 | * construction and destruction are expected to be on the same thread.
|
|---|
| 111 | * @param key key
|
|---|
| 112 | * @param value value.
|
|---|
| 113 | */
|
|---|
| 114 | MDC(const std::wstring& key, const std::wstring& value);
|
|---|
| 115 | /**
|
|---|
| 116 | * Put a context value (the <code>o</code> parameter) as identified
|
|---|
| 117 | * with the <code>key</code> parameter into the current thread's
|
|---|
| 118 | * context map.
|
|---|
| 119 | *
|
|---|
| 120 | * <p>If the current thread does not have a context map it is
|
|---|
| 121 | * created as a side effect.
|
|---|
| 122 | * @param key key
|
|---|
| 123 | * @param value value.
|
|---|
| 124 | */
|
|---|
| 125 | static void put(const std::wstring& key, const std::wstring& value);
|
|---|
| 126 | /**
|
|---|
| 127 | * Get the context identified by the <code>key</code> parameter.
|
|---|
| 128 | *
|
|---|
| 129 | * <p>This method has no side effects.
|
|---|
| 130 | * @param key key.
|
|---|
| 131 | * @return value for key, empty if not set.
|
|---|
| 132 | * */
|
|---|
| 133 | static std::wstring get(const std::wstring& key);
|
|---|
| 134 | /**
|
|---|
| 135 | * Remove the the context identified by the <code>key</code>
|
|---|
| 136 | * parameter.
|
|---|
| 137 | * @param key key.
|
|---|
| 138 | * @return value if key had been set, empty if not.
|
|---|
| 139 | */
|
|---|
| 140 | static std::wstring remove(const std::wstring& key);
|
|---|
| 141 | #endif
|
|---|
| 142 | #if LOG4CXX_UNICHAR_API
|
|---|
| 143 | /**
|
|---|
| 144 | * Places a key/value pair in the MDC for the current thread
|
|---|
| 145 | * which will be removed during the corresponding destructor. Both
|
|---|
| 146 | * construction and destruction are expected to be on the same thread.
|
|---|
| 147 | * @param key key
|
|---|
| 148 | * @param value value.
|
|---|
| 149 | */
|
|---|
| 150 | MDC(const std::basic_string<UniChar>& key, const std::basic_string<UniChar>& value);
|
|---|
| 151 | /**
|
|---|
| 152 | * Put a context value (the <code>o</code> parameter) as identified
|
|---|
| 153 | * with the <code>key</code> parameter into the current thread's
|
|---|
| 154 | * context map.
|
|---|
| 155 | *
|
|---|
| 156 | * <p>If the current thread does not have a context map it is
|
|---|
| 157 | * created as a side effect.
|
|---|
| 158 | * @param key key
|
|---|
| 159 | * @param value value.
|
|---|
| 160 | */
|
|---|
| 161 | static void put(const std::basic_string<UniChar>& key, const std::basic_string<UniChar>& value);
|
|---|
| 162 | /**
|
|---|
| 163 | * Get the context identified by the <code>key</code> parameter.
|
|---|
| 164 | *
|
|---|
| 165 | * <p>This method has no side effects.
|
|---|
| 166 | * @param key key.
|
|---|
| 167 | * @return value for key, empty if not set.
|
|---|
| 168 | * */
|
|---|
| 169 | static std::basic_string<UniChar> get(const std::basic_string<UniChar>& key);
|
|---|
| 170 | /**
|
|---|
| 171 | * Remove the the context identified by the <code>key</code>
|
|---|
| 172 | * parameter.
|
|---|
| 173 | * @param key key.
|
|---|
| 174 | * @return value if key had been set, empty if not.
|
|---|
| 175 | */
|
|---|
| 176 | static std::basic_string<UniChar> remove(const std::basic_string<UniChar>& key);
|
|---|
| 177 | #endif
|
|---|
| 178 | #if LOG4CXX_CFSTRING_API
|
|---|
| 179 | /**
|
|---|
| 180 | * Places a key/value pair in the MDC for the current thread
|
|---|
| 181 | * which will be removed during the corresponding destructor. Both
|
|---|
| 182 | * construction and destruction are expected to be on the same thread.
|
|---|
| 183 | * @param key key
|
|---|
| 184 | * @param value value.
|
|---|
| 185 | */
|
|---|
| 186 | MDC(const CFStringRef& key, const CFStringRef& value);
|
|---|
| 187 | /**
|
|---|
| 188 | * Put a context value (the <code>o</code> parameter) as identified
|
|---|
| 189 | * with the <code>key</code> parameter into the current thread's
|
|---|
| 190 | * context map.
|
|---|
| 191 | *
|
|---|
| 192 | * <p>If the current thread does not have a context map it is
|
|---|
| 193 | * created as a side effect.
|
|---|
| 194 | * @param key key
|
|---|
| 195 | * @param value value.
|
|---|
| 196 | */
|
|---|
| 197 | static void put(const CFStringRef& key, const CFStringRef& value);
|
|---|
| 198 | /**
|
|---|
| 199 | * Get the context identified by the <code>key</code> parameter.
|
|---|
| 200 | *
|
|---|
| 201 | * <p>This method has no side effects.
|
|---|
| 202 | * @param key key.
|
|---|
| 203 | * @return value for key, empty if not set.
|
|---|
| 204 | * */
|
|---|
| 205 | static CFStringRef get(const CFStringRef& key);
|
|---|
| 206 | /**
|
|---|
| 207 | * Remove the the context identified by the <code>key</code>
|
|---|
| 208 | * parameter.
|
|---|
| 209 | * @param key key.
|
|---|
| 210 | * @return value if key had been set, empty if not.
|
|---|
| 211 | */
|
|---|
| 212 | static CFStringRef remove(const CFStringRef& key);
|
|---|
| 213 | #endif
|
|---|
| 214 | /**
|
|---|
| 215 | * Remove the the context identified by the <code>key</code>
|
|---|
| 216 | * parameter.
|
|---|
| 217 | * @param key key.
|
|---|
| 218 | * @param prevValue buffer to which previous value is appended.
|
|---|
| 219 | * @return true if key existed in MDC.
|
|---|
| 220 | */
|
|---|
| 221 | static bool remove(const LogString& key, LogString& prevValue);
|
|---|
| 222 |
|
|---|
| 223 | /**
|
|---|
| 224 | * Clear all entries in the MDC.
|
|---|
| 225 | */
|
|---|
| 226 | static void clear();
|
|---|
| 227 |
|
|---|
| 228 | private:
|
|---|
| 229 | MDC(const MDC&);
|
|---|
| 230 | MDC& operator=(const MDC&);
|
|---|
| 231 | LogString key;
|
|---|
| 232 | }; // class MDC;
|
|---|
| 233 | } // namespace log4cxx
|
|---|
| 234 |
|
|---|
| 235 | #if defined(_MSC_VER)
|
|---|
| 236 | #pragma warning (pop)
|
|---|
| 237 | #endif
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 | #endif // _LOG4CXX_MDC_H
|
|---|