/src/log4cplus/src/loggingevent.cxx
Line | Count | Source |
1 | | // Module: Log4CPLUS |
2 | | // File: loggingevent.cxx |
3 | | // Created: 6/2003 |
4 | | // Author: Tad E. Smith |
5 | | // |
6 | | // |
7 | | // Copyright 2003-2017 Tad E. Smith |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | // you may not use this file except in compliance with the License. |
11 | | // You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software |
16 | | // distributed under the License is distributed on an "AS IS" BASIS, |
17 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | // See the License for the specific language governing permissions and |
19 | | // limitations under the License. |
20 | | |
21 | | #include <log4cplus/spi/loggingevent.h> |
22 | | #include <log4cplus/internal/internal.h> |
23 | | #include <algorithm> |
24 | | |
25 | | |
26 | | namespace log4cplus { namespace spi { |
27 | | |
28 | | |
29 | | static const int LOG4CPLUS_DEFAULT_TYPE = 1; |
30 | | |
31 | | |
32 | | /////////////////////////////////////////////////////////////////////////////// |
33 | | // InternalLoggingEvent ctors and dtor |
34 | | /////////////////////////////////////////////////////////////////////////////// |
35 | | |
36 | | InternalLoggingEvent::InternalLoggingEvent(const log4cplus::tstring& logger, |
37 | | LogLevel loglevel, const log4cplus::tstring& message_, const char* filename, |
38 | | int line_, const char * function_) |
39 | 0 | : message(message_) |
40 | 0 | , loggerName(logger) |
41 | 0 | , ll(loglevel) |
42 | 0 | , ndc() |
43 | 0 | , mdc() |
44 | 0 | , thread() |
45 | 0 | , timestamp(log4cplus::helpers::now ()) |
46 | 0 | , file(filename |
47 | 0 | ? LOG4CPLUS_C_STR_TO_TSTRING(filename) |
48 | 0 | : log4cplus::tstring()) |
49 | 0 | , function (function_ |
50 | 0 | ? LOG4CPLUS_C_STR_TO_TSTRING(function_) |
51 | 0 | : log4cplus::tstring()) |
52 | 0 | , line(line_) |
53 | 0 | , threadCached(false) |
54 | 0 | , thread2Cached(false) |
55 | 0 | , ndcCached(false) |
56 | 0 | , mdcCached(false) |
57 | 0 | { |
58 | 0 | } |
59 | | |
60 | | |
61 | | InternalLoggingEvent::InternalLoggingEvent(const log4cplus::tstring& logger, |
62 | | LogLevel loglevel, const log4cplus::tstring& ndc_, |
63 | | MappedDiagnosticContextMap const & mdc_, const log4cplus::tstring& message_, |
64 | | const log4cplus::tstring& thread_, log4cplus::helpers::Time time, |
65 | | const log4cplus::tstring& file_, int line_, |
66 | | const log4cplus::tstring & function_) |
67 | 0 | : message(message_) |
68 | 0 | , loggerName(logger) |
69 | 0 | , ll(loglevel) |
70 | 0 | , ndc(ndc_) |
71 | 0 | , mdc(mdc_) |
72 | 0 | , thread(thread_) |
73 | 0 | , timestamp(time) |
74 | 0 | , file(file_) |
75 | 0 | , function (function_) |
76 | 0 | , line(line_) |
77 | 0 | , threadCached(true) |
78 | 0 | , thread2Cached(true) |
79 | 0 | , ndcCached(true) |
80 | 0 | , mdcCached(true) |
81 | 0 | { |
82 | 0 | } |
83 | | |
84 | | |
85 | | InternalLoggingEvent::InternalLoggingEvent(const log4cplus::tstring& logger, |
86 | | LogLevel loglevel, const log4cplus::tstring& ndc_, |
87 | | MappedDiagnosticContextMap const & mdc_, const log4cplus::tstring& message_, |
88 | | const log4cplus::tstring& thread_, const log4cplus::tstring& thread2_, |
89 | | log4cplus::helpers::Time time, const log4cplus::tstring& file_, int line_, |
90 | | const log4cplus::tstring & function_) |
91 | 0 | : message(message_) |
92 | 0 | , loggerName(logger) |
93 | 0 | , ll(loglevel) |
94 | 0 | , ndc(ndc_) |
95 | 0 | , mdc(mdc_) |
96 | 0 | , thread(thread_) |
97 | 0 | , thread2(thread2_) |
98 | 0 | , timestamp(time) |
99 | 0 | , file(file_) |
100 | 0 | , function (function_) |
101 | 0 | , line(line_) |
102 | 0 | , threadCached(true) |
103 | 0 | , thread2Cached(true) |
104 | 0 | , ndcCached(true) |
105 | 0 | , mdcCached(true) |
106 | 0 | { |
107 | 0 | } |
108 | | |
109 | | |
110 | | InternalLoggingEvent::InternalLoggingEvent () |
111 | 70 | : ll (NOT_SET_LOG_LEVEL) |
112 | 70 | , function () |
113 | 70 | , line (0) |
114 | 70 | , threadCached(false) |
115 | 70 | , thread2Cached(false) |
116 | 70 | , ndcCached(false) |
117 | 70 | , mdcCached(false) |
118 | 70 | { } |
119 | | |
120 | | |
121 | | InternalLoggingEvent::InternalLoggingEvent( |
122 | | const log4cplus::spi::InternalLoggingEvent& rhs) |
123 | 0 | : message(rhs.getMessage()) |
124 | 0 | , loggerName(rhs.getLoggerName()) |
125 | 0 | , ll(rhs.getLogLevel()) |
126 | 0 | , ndc(rhs.getNDC()) |
127 | 0 | , mdc(rhs.getMDCCopy()) |
128 | 0 | , thread(rhs.getThread()) |
129 | 0 | , thread2(rhs.getThread2()) |
130 | 0 | , timestamp(rhs.getTimestamp()) |
131 | 0 | , file(rhs.getFile()) |
132 | 0 | , function(rhs.getFunction()) |
133 | 0 | , line(rhs.getLine()) |
134 | 0 | , threadCached(true) |
135 | 0 | , thread2Cached(true) |
136 | 0 | , ndcCached(true) |
137 | 0 | , mdcCached(true) |
138 | 0 | { |
139 | 0 | } |
140 | | |
141 | | |
142 | | InternalLoggingEvent::~InternalLoggingEvent() |
143 | 0 | { |
144 | 0 | } |
145 | | |
146 | | |
147 | | |
148 | | /////////////////////////////////////////////////////////////////////////////// |
149 | | // InternalLoggingEvent static methods |
150 | | /////////////////////////////////////////////////////////////////////////////// |
151 | | |
152 | | unsigned int |
153 | | InternalLoggingEvent::getDefaultType() |
154 | 0 | { |
155 | 0 | return LOG4CPLUS_DEFAULT_TYPE; |
156 | 0 | } |
157 | | |
158 | | |
159 | | |
160 | | /////////////////////////////////////////////////////////////////////////////// |
161 | | // InternalLoggingEvent implementation |
162 | | /////////////////////////////////////////////////////////////////////////////// |
163 | | |
164 | | void |
165 | | InternalLoggingEvent::setLoggingEvent (const log4cplus::tstring & logger, |
166 | | LogLevel loglevel, const log4cplus::tstring & msg, const char * filename, |
167 | | int fline, const char * function_) |
168 | 561k | { |
169 | | // This could be imlemented using the swap idiom: |
170 | | // |
171 | | // InternalLoggingEvent (logger, loglevel, msg, filename, fline).swap (*this); |
172 | | // |
173 | | // But that defeats the optimization of using thread local instance |
174 | | // of InternalLoggingEvent to avoid memory allocation. |
175 | | |
176 | 561k | loggerName = logger; |
177 | 561k | ll = loglevel; |
178 | 561k | message = msg; |
179 | 561k | timestamp = helpers::now (); |
180 | | |
181 | 561k | if (filename) |
182 | 561k | file = LOG4CPLUS_C_STR_TO_TSTRING (filename); |
183 | 0 | else |
184 | 0 | file.clear (); |
185 | | |
186 | 561k | if (function_) |
187 | 561k | function = LOG4CPLUS_C_STR_TO_TSTRING (function_); |
188 | 0 | else |
189 | 0 | function.clear (); |
190 | | |
191 | 561k | line = fline; |
192 | 561k | threadCached = false; |
193 | 561k | thread2Cached = false; |
194 | 561k | ndcCached = false; |
195 | 561k | mdcCached = false; |
196 | 561k | } |
197 | | |
198 | | |
199 | | void |
200 | | InternalLoggingEvent::setFunction (char const * func) |
201 | 0 | { |
202 | 0 | if (func) |
203 | 0 | function = LOG4CPLUS_C_STR_TO_TSTRING (func); |
204 | 0 | else |
205 | 0 | function.clear (); |
206 | 0 | } |
207 | | |
208 | | |
209 | | void |
210 | | InternalLoggingEvent::setFunction (log4cplus::tstring const & func) |
211 | 0 | { |
212 | 0 | function = func; |
213 | 0 | } |
214 | | |
215 | | |
216 | | const log4cplus::tstring& |
217 | | InternalLoggingEvent::getMessage() const |
218 | 561k | { |
219 | 561k | return message; |
220 | 561k | } |
221 | | |
222 | | |
223 | | unsigned int |
224 | | InternalLoggingEvent::getType() const |
225 | 0 | { |
226 | 0 | return LOG4CPLUS_DEFAULT_TYPE; |
227 | 0 | } |
228 | | |
229 | | |
230 | | |
231 | | std::unique_ptr<InternalLoggingEvent> |
232 | | InternalLoggingEvent::clone() const |
233 | 0 | { |
234 | 0 | std::unique_ptr<InternalLoggingEvent> tmp(new InternalLoggingEvent(*this)); |
235 | 0 | return tmp; |
236 | 0 | } |
237 | | |
238 | | |
239 | | tstring const & |
240 | | InternalLoggingEvent::getMDC (tstring const & key) const |
241 | 0 | { |
242 | 0 | MappedDiagnosticContextMap const & mdc_ = getMDCCopy (); |
243 | 0 | MappedDiagnosticContextMap::const_iterator it = mdc_.find (key); |
244 | 0 | if (it != mdc_.end ()) |
245 | 0 | return it->second; |
246 | 0 | else |
247 | 0 | return internal::empty_str; |
248 | 0 | } |
249 | | |
250 | | |
251 | | |
252 | | InternalLoggingEvent & |
253 | | InternalLoggingEvent::operator = (const InternalLoggingEvent& rhs) |
254 | 0 | { |
255 | 0 | InternalLoggingEvent (rhs).swap (*this); |
256 | 0 | return *this; |
257 | 0 | } |
258 | | |
259 | | |
260 | | void |
261 | | InternalLoggingEvent::gatherThreadSpecificData () const |
262 | 0 | { |
263 | 0 | getNDC (); |
264 | 0 | getMDCCopy (); |
265 | 0 | getThread (); |
266 | 0 | getThread2 (); |
267 | 0 | } |
268 | | |
269 | | |
270 | | void |
271 | | InternalLoggingEvent::swap (InternalLoggingEvent & other) |
272 | 0 | { |
273 | 0 | using std::swap; |
274 | |
|
275 | 0 | swap (message, other.message); |
276 | 0 | swap (loggerName, other.loggerName); |
277 | 0 | swap (ll, other.ll); |
278 | 0 | swap (ndc, other.ndc); |
279 | 0 | swap (mdc, other.mdc); |
280 | 0 | swap (thread, other.thread); |
281 | 0 | swap (thread2, other.thread2); |
282 | 0 | swap (timestamp, other.timestamp); |
283 | 0 | swap (file, other.file); |
284 | 0 | swap (function, other.function); |
285 | 0 | swap (line, other.line); |
286 | 0 | swap (threadCached, other.threadCached); |
287 | 0 | swap (thread2Cached, other.thread2Cached); |
288 | 0 | swap (ndcCached, other.ndcCached); |
289 | 0 | } |
290 | | |
291 | | |
292 | | } } // namespace log4cplus { namespace spi { |