summaryrefslogtreecommitdiff
path: root/src/main/include/log4cxx/mdc.h
blob: f9cd8753b13764d773beb9d08a994cb41fa2d95c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _LOG4CXX_MDC_H
#define _LOG4CXX_MDC_H

#if defined(_MSC_VER)
#pragma warning (push)
#pragma warning ( disable: 4231 4251 4275 4786 )
#endif

#include <log4cxx/log4cxx.h>
#include <log4cxx/logstring.h>
#include <map>

namespace log4cxx
{
        /**
        The MDC class is similar to the {@link log4cxx::NDC NDC} class except that it is
        based on a map instead of a stack. It provides <em>mapped
        diagnostic contexts</em>. A <em>Mapped Diagnostic Context</em>, or
        MDC in short, is an instrument for distinguishing interleaved log
        output from different sources. Log output is typically interleaved
        when a server handles multiple clients near-simultaneously.

        <p><b><em>The MDC is managed on a per thread basis</em></b>. A
        child thread automatically inherits a <em>copy</em> of the mapped
        diagnostic context of its parent.

        */
        class LOG4CXX_EXPORT MDC
        {
        public:
                /** String to string stl map.
                */
                typedef std::map<LogString, LogString> Map;

                /**
                 *  Places a key/value pair in the MDC for the current thread
                 *    which will be removed during the corresponding destructor.  Both
                 *    construction and destruction are expected to be on the same thread.
                 *    @param key key
                 *    @param value value.
                 */
                MDC(const std::string& key, const std::string& value);
                ~MDC();

                /**
                * Put a context value (the <code>o</code> parameter) as identified
                * with the <code>key</code> parameter into the current thread's
                * context map.
                *
                * <p>If the current thread does not have a context map it is
                * created as a side effect.
                 *    @param key key
                 *    @param value value.
                */
                static void put(const std::string& key, const std::string& value);
                /**
                * Put a context value (the <code>o</code> parameter) as identified
                * with the <code>key</code> parameter into the current thread's
                * context map.
                *
                * <p>If the current thread does not have a context map it is
                * created as a side effect.
                * */
                static void putLS(const LogString& key, const LogString& value);

                /**
                * Get the context identified by the <code>key</code> parameter.
                *
                *  <p>This method has no side effects.
                *  @param key key.
                *  @return value for key, empty if not set.
                * */
                static std::string get(const std::string& key);
                /**
                 *  Gets the context identified by the <code>key</code> parameter.
                 *  @param key context key.
                 *  @param dest destination to which value is appended.
                 *  @return true if key has associated value.
                 */
                static bool get(const LogString& key, LogString& dest);

                /**
                * Remove the the context identified by the <code>key</code>
                * parameter.
                *  @param key key.
                * @return value if key had been set, empty if not. 
                */
                static std::string remove(const std::string& key);
#if LOG4CXX_WCHAR_T_API
                /**
                 *  Places a key/value pair in the MDC for the current thread
                 *    which will be removed during the corresponding destructor.  Both
                 *    construction and destruction are expected to be on the same thread.
                 *    @param key key
                 *    @param value value.
                 */
                MDC(const std::wstring& key, const std::wstring& value);
                /**
                * Put a context value (the <code>o</code> parameter) as identified
                * with the <code>key</code> parameter into the current thread's
                * context map.
                *
                * <p>If the current thread does not have a context map it is
                * created as a side effect.
                 *    @param key key
                 *    @param value value.
                */
                static void put(const std::wstring& key, const std::wstring& value);
                /**
                * Get the context identified by the <code>key</code> parameter.
                *
                *  <p>This method has no side effects.
                *  @param key key.
                *  @return value for key, empty if not set.
                * */
                static std::wstring get(const std::wstring& key);
                /**
                * Remove the the context identified by the <code>key</code>
                * parameter.
                *  @param key key.
                * @return value if key had been set, empty if not. 
                */
                static std::wstring remove(const std::wstring& key);
#endif
#if LOG4CXX_UNICHAR_API
                /**
                 *  Places a key/value pair in the MDC for the current thread
                 *    which will be removed during the corresponding destructor.  Both
                 *    construction and destruction are expected to be on the same thread.
                 *    @param key key
                 *    @param value value.
                 */
                MDC(const std::basic_string<UniChar>& key, const std::basic_string<UniChar>& value);
                /**
                * Put a context value (the <code>o</code> parameter) as identified
                * with the <code>key</code> parameter into the current thread's
                * context map.
                *
                * <p>If the current thread does not have a context map it is
                * created as a side effect.
                 *    @param key key
                 *    @param value value.
                */
                static void put(const std::basic_string<UniChar>& key, const std::basic_string<UniChar>& value);
                /**
                * Get the context identified by the <code>key</code> parameter.
                *
                *  <p>This method has no side effects.
                *  @param key key.
                *  @return value for key, empty if not set.
                * */
                static std::basic_string<UniChar> get(const std::basic_string<UniChar>& key);
                /**
                * Remove the the context identified by the <code>key</code>
                * parameter.
                *  @param key key.
                * @return value if key had been set, empty if not. 
                */
                static std::basic_string<UniChar> remove(const std::basic_string<UniChar>& key);
#endif
#if LOG4CXX_CFSTRING_API
                /**
                 *  Places a key/value pair in the MDC for the current thread
                 *    which will be removed during the corresponding destructor.  Both
                 *    construction and destruction are expected to be on the same thread.
                 *    @param key key
                 *    @param value value.
                 */
                MDC(const CFStringRef& key, const CFStringRef& value);
                /**
                * Put a context value (the <code>o</code> parameter) as identified
                * with the <code>key</code> parameter into the current thread's
                * context map.
                *
                * <p>If the current thread does not have a context map it is
                * created as a side effect.
                 *    @param key key
                 *    @param value value.
                */
                static void put(const CFStringRef& key, const CFStringRef& value);
                /**
                * Get the context identified by the <code>key</code> parameter.
                *
                *  <p>This method has no side effects.
                *  @param key key.
                *  @return value for key, empty if not set.
                * */
                static CFStringRef get(const CFStringRef& key);
                /**
                * Remove the the context identified by the <code>key</code>
                * parameter.
                *  @param key key.
                * @return value if key had been set, empty if not. 
                */
                static CFStringRef remove(const CFStringRef& key);
#endif
                /**
                * Remove the the context identified by the <code>key</code>
                * parameter.
                *  @param key key.
                * @param prevValue buffer to which previous value is appended.
                * @return true if key existed in MDC. 
                */
                static bool remove(const LogString& key, LogString& prevValue);

                /**
                * Clear all entries in the MDC.
                */
                static void clear();

        private:
                MDC(const MDC&);
                MDC& operator=(const MDC&);
                LogString key;                
        }; // class MDC;
}  // namespace log4cxx

#if defined(_MSC_VER)
#pragma warning (pop)
#endif


#endif // _LOG4CXX_MDC_H