source: pacpusframework/branches/2.0-beta1/include/extlib/apache-log4cxx/log4cxx/stream.h@ 89

Last change on this file since 89 was 89, checked in by morasjul, 11 years ago

PACPUS 2.0 Beta deployed in new branch

Major changes:
-Add communication interface between components
-Add examples for communications interface (TestComponents)
-Move to Qt5 support

  • Property svn:executable set to *
File size: 20.3 KB
Line 
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_STREAM_H
19#define _LOG4CXX_STREAM_H
20
21#include <log4cxx/logger.h>
22#include <sstream>
23#include <log4cxx/spi/location/locationinfo.h>
24
25namespace log4cxx
26{
27
28 /**
29 * Base class for the basic_logstream template which attempts
30 * to emulate std::basic_ostream but attempts to short-circuit
31 * unnecessary operations.
32 *
33 * The logstream has a logger and level that are used for logging
34 * requests. The level of the stream is compared against the
35 * current level of the logger to determine if the request should be processed.
36 */
37 class LOG4CXX_EXPORT logstream_base {
38 public:
39 /**
40 * Create new instance.
41 * @param logger logger logger used in log requests.
42 * @param level indicates level that will be used in log requests. Can
43 * be modified later by inserting a level or calling setLevel.
44 */
45 logstream_base(const log4cxx::LoggerPtr& logger,
46 const log4cxx::LevelPtr& level);
47 /**
48 * Destructor.
49 */
50 virtual ~logstream_base();
51 /**
52 * Insertion operator for std::fixed and similar manipulators.
53 */
54 void insert(std::ios_base& (*manip)(std::ios_base&));
55
56 /**
57 * get precision.
58 */
59 int precision();
60 /**
61 * get width.
62 */
63 int width();
64 /**
65 * set precision. This should be used in preference to inserting an std::setprecision(n)
66 * since the other requires construction of an STL stream which may be expensive.
67 */
68 int precision(int newval);
69 /**
70 * set width. This should be used in preference to inserting an std::setw(n)
71 * since the other requires construction of an STL stream which may be expensive.
72 */
73 int width(int newval);
74 /**
75 * Get fill character.
76 */
77 int fill();
78 /**
79 * Set fill character.
80 */
81 int fill(int newval);
82
83 /**
84 * Set flags. see std::ios_base.
85 */
86 std::ios_base::fmtflags flags(std::ios_base::fmtflags newflags);
87 /**
88 * Set flags. see std::ios_base.
89 */
90 std::ios_base::fmtflags setf(std::ios_base::fmtflags newflags, std::ios_base::fmtflags mask);
91 /**
92 * Set flags. see std::ios_base.
93 */
94 std::ios_base::fmtflags setf(std::ios_base::fmtflags newflags);
95
96
97 /**
98 * end of message manipulator, triggers logging.
99 */
100 static logstream_base& endmsg(logstream_base&);
101
102 /**
103 * no-operation manipulator, Used to avoid ambiguity with VC6.
104 */
105 static logstream_base& nop(logstream_base&);
106
107 /**
108 * end of message action.
109 */
110 void end_message();
111
112
113
114 /**
115 * Set the level.
116 * @param level level
117 */
118 void setLevel(const LevelPtr& level);
119 /**
120 * Returns true if the current level is the same or high as the
121 * level of logger at time of construction or last setLevel.
122 */
123 inline bool isEnabled() const {
124 return enabled;
125 }
126
127 /**
128 * Returns if logger is currently enabled for the specified level.
129 */
130 bool isEnabledFor(const LevelPtr& level) const;
131
132 /**
133 * Sets the location for subsequent log requests.
134 */
135 void setLocation(const log4cxx::spi::LocationInfo& location);
136
137 /**
138 * Sets the state of the embedded stream (if any)
139 * to the state of the formatting info.
140 * @param os stream to receive formatting info.
141 * @param fillchar receives fill charater.
142 * @return true if fill character was specified.
143 */
144 bool set_stream_state(std::ios_base& os, int& fillchar);
145
146 protected:
147 /**
148 * Dispatches the pending log request.
149 */
150 virtual void log(LoggerPtr& logger,
151 const LevelPtr& level,
152 const log4cxx::spi::LocationInfo& location) = 0;
153 /**
154 * Erase any content in the message construction buffer.
155 */
156 virtual void erase() = 0;
157 /**
158 * Copy state of embedded stream (if any)
159 * to value and mask instances of std::ios_base
160 * and return fill character value.
161 */
162 virtual void get_stream_state(std::ios_base& base,
163 std::ios_base& mask,
164 int& fill,
165 bool& fillSet) const = 0;
166 virtual void refresh_stream_state() = 0;
167
168 private:
169 /**
170 * prevent copy constructor.
171 */
172 logstream_base(logstream_base&);
173 /**
174 * prevent copy operatpr.
175 */
176 logstream_base& operator=(logstream_base&);
177 /**
178 * Minimal extension of std::ios_base to allow creation
179 * of embedded IO states.
180 */
181 class LOG4CXX_EXPORT logstream_ios_base : public std::ios_base {
182 public:
183 logstream_ios_base(std::ios_base::fmtflags initval,
184 int initsize);
185 } initset, initclear;
186 /**
187 * fill character.
188 */
189 int fillchar;
190 /**
191 * true if fill character is set.
192 */
193 bool fillset;
194 /**
195 * true if assigned level was same or higher than level of associated logger.
196 */
197 bool enabled;
198 /**
199 * associated logger.
200 */
201 log4cxx::LoggerPtr logger;
202 /**
203 * associated level.
204 */
205 log4cxx::LevelPtr level;
206 /**
207 * associated level.
208 */
209 log4cxx::spi::LocationInfo location;
210 };
211
212 typedef logstream_base& (*logstream_manipulator)(logstream_base&);
213
214 /**
215 * An STL-like stream API for log4cxx using char as the character type.
216 *. Instances of log4cxx::logstream
217 * are not designedfor use by multiple threads and in general should be short-lived
218 * function scoped objects. Using log4cxx::basic_logstream as a class member or
219 * static instance should be avoided in the same manner as you would avoid placing a std::ostringstream
220 * in those locations. Insertion operations are generally short-circuited if the
221 * level for the stream is not the same of higher that the level of the associated logger.
222 */
223 class LOG4CXX_EXPORT logstream : public logstream_base {
224 typedef char Ch;
225 public:
226 /**
227 * Constructor.
228 */
229 logstream(const log4cxx::LoggerPtr& logger,
230 const log4cxx::LevelPtr& level);
231
232 /**
233 * Constructor.
234 */
235 logstream(const Ch* loggerName,
236 const log4cxx::LevelPtr& level);
237
238 /**
239 * Constructor.
240 */
241 logstream(const std::basic_string<Ch>& loggerName,
242 const log4cxx::LevelPtr& level);
243
244 ~logstream();
245
246 /**
247 * Insertion operator for std::fixed and similar manipulators.
248 */
249 logstream& operator<<(std::ios_base& (*manip)(std::ios_base&));
250
251 /**
252 * Insertion operator for logstream_base::endmsg.
253 */
254 logstream& operator<<(logstream_manipulator manip);
255
256 /**
257 * Insertion operator for level.
258 */
259 logstream& operator<<(const log4cxx::LevelPtr& level);
260 /**
261 * Insertion operator for location.
262 */
263 logstream& operator<<(const log4cxx::spi::LocationInfo& location);
264
265 /**
266 * Alias for insertion operator for location. Kludge to avoid
267 * inappropriate compiler ambiguity.
268 */
269 logstream& operator>>(const log4cxx::spi::LocationInfo& location);
270
271 /**
272 * Cast operator to provide access to embedded std::basic_ostream.
273 */
274 operator std::basic_ostream<Ch>&();
275
276#if !(LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE)
277 /**
278 * Template to allow any class with an std::basic_ostream inserter
279 * to be applied to this class.
280 */
281 template <class V>
282 inline log4cxx::logstream& operator<<(const V& val) {
283 if (LOG4CXX_UNLIKELY(isEnabled())) {
284 ((std::basic_ostream<char>&) *this) << val;
285 }
286 return *this;
287 }
288#endif
289
290
291 protected:
292 virtual void log(LoggerPtr& logger,
293 const LevelPtr& level,
294 const log4cxx::spi::LocationInfo& location);
295
296 virtual void erase();
297
298 virtual void get_stream_state(std::ios_base& base,
299 std::ios_base& mask,
300 int& fill,
301 bool& fillSet) const;
302 virtual void refresh_stream_state();
303
304
305 private:
306 logstream(const logstream&);
307 logstream& operator=(const logstream&);
308 std::basic_stringstream<Ch>* stream;
309
310 };
311
312#if LOG4CXX_WCHAR_T_API
313 /**
314 * An STL-like stream API for log4cxx using wchar_t as the character type.
315 *. Instances of log4cxx::logstream
316 * are not designedfor use by multiple threads and in general should be short-lived
317 * function scoped objects. Using log4cxx::basic_logstream as a class member or
318 * static instance should be avoided in the same manner as you would avoid placing a std::ostringstream
319 * in those locations. Insertion operations are generally short-circuited if the
320 * level for the stream is not the same of higher that the level of the associated logger.
321 */
322 class LOG4CXX_EXPORT wlogstream : public logstream_base {
323 typedef wchar_t Ch;
324 public:
325 /**
326 * Constructor.
327 */
328 wlogstream(const log4cxx::LoggerPtr& logger,
329 const log4cxx::LevelPtr& level);
330
331 /**
332 * Constructor.
333 */
334 wlogstream(const Ch* loggerName,
335 const log4cxx::LevelPtr& level);
336
337 /**
338 * Constructor.
339 */
340 wlogstream(const std::basic_string<Ch>& loggerName,
341 const log4cxx::LevelPtr& level);
342
343 ~wlogstream();
344
345 /**
346 * Insertion operator for std::fixed and similar manipulators.
347 */
348 wlogstream& operator<<(std::ios_base& (*manip)(std::ios_base&));
349
350 /**
351 * Insertion operator for logstream_base::endmsg.
352 */
353 wlogstream& operator<<(logstream_manipulator manip);
354
355 /**
356 * Insertion operator for level.
357 */
358 wlogstream& operator<<(const log4cxx::LevelPtr& level);
359 /**
360 * Insertion operator for location.
361 */
362 wlogstream& operator<<(const log4cxx::spi::LocationInfo& location);
363
364 /**
365 * Alias for insertion operator for location. Kludge to avoid
366 * inappropriate compiler ambiguity.
367 */
368 wlogstream& operator>>(const log4cxx::spi::LocationInfo& location);
369
370
371 /**
372 * Cast operator to provide access to embedded std::basic_ostream.
373 */
374 operator std::basic_ostream<Ch>&();
375
376#if !(LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE)
377 /**
378 * Template to allow any class with an std::basic_ostream inserter
379 * to be applied to this class.
380 */
381 template <class V>
382 inline log4cxx::wlogstream& operator<<(const V& val) {
383 if (LOG4CXX_UNLIKELY(isEnabled())) {
384 ((std::basic_ostream<wchar_t>&) *this) << val;
385 }
386 return *this;
387 }
388#endif
389
390 protected:
391 virtual void log(LoggerPtr& logger,
392 const LevelPtr& level,
393 const log4cxx::spi::LocationInfo& location);
394
395 virtual void erase();
396
397 virtual void get_stream_state(std::ios_base& base,
398 std::ios_base& mask,
399 int& fill,
400 bool& fillSet) const;
401 virtual void refresh_stream_state();
402
403
404 private:
405 wlogstream(const wlogstream&);
406 wlogstream& operator=(const wlogstream&);
407 std::basic_stringstream<Ch>* stream;
408
409 };
410#endif
411
412#if LOG4CXX_UNICHAR_API || LOG4CXX_CFSTRING_API
413 /**
414 * An STL-like stream API for log4cxx using UniChar as the character type.
415 *. Instances of log4cxx::logstream
416 * are not designedfor use by multiple threads and in general should be short-lived
417 * function scoped objects. Using log4cxx::basic_logstream as a class member or
418 * static instance should be avoided in the same manner as you would avoid placing a std::ostringstream
419 * in those locations. Insertion operations are generally short-circuited if the
420 * level for the stream is not the same of higher that the level of the associated logger.
421 */
422 class LOG4CXX_EXPORT ulogstream : public logstream_base {
423 typedef UniChar Ch;
424 public:
425 /**
426 * Constructor.
427 */
428 ulogstream(const log4cxx::LoggerPtr& logger,
429 const log4cxx::LevelPtr& level);
430
431#if LOG4CXX_UNICHAR_API
432 /**
433 * Constructor.
434 */
435 ulogstream(const Ch* loggerName,
436 const log4cxx::LevelPtr& level);
437
438 /**
439 * Constructor.
440 */
441 ulogstream(const std::basic_string<Ch>& loggerName,
442 const log4cxx::LevelPtr& level);
443#endif
444
445#if LOG4CXX_CFSTRING_API
446 ulogstream(const CFStringRef& loggerName,
447 const log4cxx::LevelPtr& level);
448#endif
449
450 ~ulogstream();
451
452 /**
453 * Insertion operator for std::fixed and similar manipulators.
454 */
455 ulogstream& operator<<(std::ios_base& (*manip)(std::ios_base&));
456
457 /**
458 * Insertion operator for logstream_base::endmsg.
459 */
460 ulogstream& operator<<(logstream_manipulator manip);
461
462 /**
463 * Insertion operator for level.
464 */
465 ulogstream& operator<<(const log4cxx::LevelPtr& level);
466 /**
467 * Insertion operator for location.
468 */
469 ulogstream& operator<<(const log4cxx::spi::LocationInfo& location);
470
471 /**
472 * Alias for insertion operator for location. Kludge to avoid
473 * inappropriate compiler ambiguity.
474 */
475 ulogstream& operator>>(const log4cxx::spi::LocationInfo& location);
476
477
478 /**
479 * Cast operator to provide access to embedded std::basic_ostream.
480 */
481 operator std::basic_ostream<Ch>&();
482
483#if !(LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE)
484 /**
485 * Template to allow any class with an std::basic_ostream inserter
486 * to be applied to this class.
487 */
488 template <class V>
489 inline ulogstream& operator<<(const V& val) {
490 if (LOG4CXX_UNLIKELY(isEnabled())) {
491 ((std::basic_ostream<Ch>&) *this) << val;
492 }
493 return *this;
494 }
495#endif
496
497 protected:
498 virtual void log(LoggerPtr& logger,
499 const LevelPtr& level,
500 const log4cxx::spi::LocationInfo& location);
501
502 virtual void erase();
503
504 virtual void get_stream_state(std::ios_base& base,
505 std::ios_base& mask,
506 int& fill,
507 bool& fillSet) const;
508 virtual void refresh_stream_state();
509
510
511 private:
512 ulogstream(const ulogstream&);
513 ulogstream& operator=(const ulogstream&);
514 std::basic_stringstream<Ch>* stream;
515
516 };
517#endif
518
519
520} // namespace log4cxx
521
522
523#if LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE
524//
525// VC6 will fail to compile if class-scope templates
526// are used to handle arbitrary insertion operations.
527// However, using global namespace insertion operations
528// run into LOGCXX-150.
529
530/**
531 * Template to allow any class with an std::basic_ostream inserter
532 * to be applied to this class.
533 */
534template <class V>
535inline log4cxx::logstream& operator<<(log4cxx::logstream& os, const V& val) {
536 if (LOG4CXX_UNLIKELY(os.isEnabled())) {
537 ((std::basic_ostream<char>&) os) << val;
538 }
539 return os;
540}
541
542#if LOG4CXX_WCHAR_T_API
543/**
544 * Template to allow any class with an std::basic_ostream inserter
545 * to be applied to this class.
546 */
547template <class V>
548inline log4cxx::wlogstream& operator<<(log4cxx::wlogstream& os, const V& val) {
549 if (LOG4CXX_UNLIKELY(os.isEnabled())) {
550 ((std::basic_ostream<wchar_t>&) os) << val;
551 }
552 return os;
553}
554#endif
555#endif
556
557#if !defined(LOG4CXX_ENDMSG)
558#if LOG4CXX_LOGSTREAM_ADD_NOP
559#define LOG4CXX_ENDMSG (log4cxx::logstream_manipulator) log4cxx::logstream_base::nop >> LOG4CXX_LOCATION << (log4cxx::logstream_manipulator) log4cxx::logstream_base::endmsg
560#else
561#define LOG4CXX_ENDMSG LOG4CXX_LOCATION << (log4cxx::logstream_manipulator) log4cxx::logstream_base::endmsg
562#endif
563#endif
564
565
566#endif //_LOG4CXX_STREAM_H
Note: See TracBrowser for help on using the repository browser.