Apache log4cxx  Version 0.12.1
stream.h
Go to the documentation of this file.
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>
24 
25 namespace log4cxx
26 {
27 
37 class LOG4CXX_EXPORT logstream_base
38 {
39  public:
46  logstream_base(const log4cxx::LoggerPtr& logger,
47  const log4cxx::LevelPtr& level);
51  virtual ~logstream_base();
55  void insert(std::ios_base & (*manip)(std::ios_base&));
56 
60  int precision();
64  int width();
69  int precision(int newval);
74  int width(int newval);
78  int fill();
82  int fill(int newval);
83 
87  std::ios_base::fmtflags flags(std::ios_base::fmtflags newflags);
91  std::ios_base::fmtflags setf(std::ios_base::fmtflags newflags, std::ios_base::fmtflags mask);
95  std::ios_base::fmtflags setf(std::ios_base::fmtflags newflags);
96 
97 
101  static logstream_base& endmsg(logstream_base&);
102 
106  static logstream_base& nop(logstream_base&);
107 
111  void end_message();
112 
113 
114 
119  void setLevel(const LevelPtr& level);
124  inline bool isEnabled() const
125  {
126  return enabled;
127  }
128 
132  bool isEnabledFor(const LevelPtr& level) const;
133 
137  void setLocation(const log4cxx::spi::LocationInfo& location);
138 
146  bool set_stream_state(std::ios_base& os, int& fillchar);
147 
148  protected:
152  virtual void log(LoggerPtr& logger,
153  const LevelPtr& level,
154  const log4cxx::spi::LocationInfo& location) = 0;
158  virtual void erase() = 0;
164  virtual void get_stream_state(std::ios_base& base,
165  std::ios_base& mask,
166  int& fill,
167  bool& fillSet) const = 0;
168  virtual void refresh_stream_state() = 0;
169 
170  private:
178  logstream_base& operator=(logstream_base&);
183  class LOG4CXX_EXPORT logstream_ios_base : public std::ios_base
184  {
185  public:
186  logstream_ios_base(std::ios_base::fmtflags initval,
187  int initsize);
188  } initset, initclear;
192  int fillchar;
196  bool fillset;
200  bool enabled;
204  log4cxx::LoggerPtr logger;
208  log4cxx::LevelPtr level;
213 };
214 
215 typedef logstream_base& (*logstream_manipulator)(logstream_base&);
216 
226 class LOG4CXX_EXPORT logstream : public logstream_base
227 {
228  typedef char Ch;
229  public:
233  logstream(const log4cxx::LoggerPtr& logger,
234  const log4cxx::LevelPtr& level);
235 
239  logstream(const Ch* loggerName,
240  const log4cxx::LevelPtr& level);
241 
245  logstream(const std::basic_string<Ch>& loggerName,
246  const log4cxx::LevelPtr& level);
247 
248  ~logstream();
249 
253  logstream& operator<<(std::ios_base & (*manip)(std::ios_base&));
254 
259 
263  logstream& operator<<(const log4cxx::LevelPtr& level);
268 
273  logstream& operator>>(const log4cxx::spi::LocationInfo& location);
274 
278  operator std::basic_ostream<Ch>& ();
279 
280 #if !(LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE)
281 
285  template <class V>
286  inline log4cxx::logstream& operator<<(const V& val)
287  {
288  if (LOG4CXX_UNLIKELY(isEnabled()))
289  {
290  ((std::basic_ostream<char>&) *this) << val;
291  }
292 
293  return *this;
294  }
295 #endif
296 
297 
298  protected:
299  virtual void log(LoggerPtr& logger,
300  const LevelPtr& level,
301  const log4cxx::spi::LocationInfo& location);
302 
303  virtual void erase();
304 
305  virtual void get_stream_state(std::ios_base& base,
306  std::ios_base& mask,
307  int& fill,
308  bool& fillSet) const;
309  virtual void refresh_stream_state();
310 
311 
312  private:
313  logstream(const logstream&);
314  logstream& operator=(const logstream&);
315  std::basic_stringstream<Ch>* stream;
316 
317 };
318 
319 #if LOG4CXX_WCHAR_T_API
320 
329 class LOG4CXX_EXPORT wlogstream : public logstream_base
330 {
331  typedef wchar_t Ch;
332  public:
336  wlogstream(const log4cxx::LoggerPtr& logger,
337  const log4cxx::LevelPtr& level);
338 
342  wlogstream(const Ch* loggerName,
343  const log4cxx::LevelPtr& level);
344 
348  wlogstream(const std::basic_string<Ch>& loggerName,
349  const log4cxx::LevelPtr& level);
350 
351  ~wlogstream();
352 
356  wlogstream& operator<<(std::ios_base & (*manip)(std::ios_base&));
357 
362 
366  wlogstream& operator<<(const log4cxx::LevelPtr& level);
371 
376  wlogstream& operator>>(const log4cxx::spi::LocationInfo& location);
377 
378 
382  operator std::basic_ostream<Ch>& ();
383 
384 #if !(LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE)
385 
389  template <class V>
390  inline log4cxx::wlogstream& operator<<(const V& val)
391  {
392  if (LOG4CXX_UNLIKELY(isEnabled()))
393  {
394  ((std::basic_ostream<wchar_t>&) *this) << val;
395  }
396 
397  return *this;
398  }
399 #endif
400 
401  protected:
402  virtual void log(LoggerPtr& logger,
403  const LevelPtr& level,
404  const log4cxx::spi::LocationInfo& location);
405 
406  virtual void erase();
407 
408  virtual void get_stream_state(std::ios_base& base,
409  std::ios_base& mask,
410  int& fill,
411  bool& fillSet) const;
412  virtual void refresh_stream_state();
413 
414 
415  private:
416  wlogstream(const wlogstream&);
417  wlogstream& operator=(const wlogstream&);
418  std::basic_stringstream<Ch>* stream;
419 
420 };
421 #endif
422 
423 #if LOG4CXX_UNICHAR_API || LOG4CXX_CFSTRING_API
424 
433 class LOG4CXX_EXPORT ulogstream : public logstream_base
434 {
435  typedef UniChar Ch;
436  public:
440  ulogstream(const log4cxx::LoggerPtr& logger,
441  const log4cxx::LevelPtr& level);
442 
443 #if LOG4CXX_UNICHAR_API
444 
447  ulogstream(const Ch* loggerName,
448  const log4cxx::LevelPtr& level);
449 
453  ulogstream(const std::basic_string<Ch>& loggerName,
454  const log4cxx::LevelPtr& level);
455 #endif
456 
457 #if LOG4CXX_CFSTRING_API
458  ulogstream(const CFStringRef& loggerName,
459  const log4cxx::LevelPtr& level);
460 #endif
461 
462  ~ulogstream();
463 
467  ulogstream& operator<<(std::ios_base & (*manip)(std::ios_base&));
468 
473 
477  ulogstream& operator<<(const log4cxx::LevelPtr& level);
482 
487  ulogstream& operator>>(const log4cxx::spi::LocationInfo& location);
488 
489 
493  operator std::basic_ostream<Ch>& ();
494 
495 #if !(LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE)
496 
500  template <class V>
501  inline ulogstream& operator<<(const V& val)
502  {
503  if (LOG4CXX_UNLIKELY(isEnabled()))
504  {
505  ((std::basic_ostream<Ch>&) *this) << val;
506  }
507 
508  return *this;
509  }
510 #endif
511 
512  protected:
513  virtual void log(LoggerPtr& logger,
514  const LevelPtr& level,
515  const log4cxx::spi::LocationInfo& location);
516 
517  virtual void erase();
518 
519  virtual void get_stream_state(std::ios_base& base,
520  std::ios_base& mask,
521  int& fill,
522  bool& fillSet) const;
523  virtual void refresh_stream_state();
524 
525 
526  private:
527  ulogstream(const ulogstream&);
528  ulogstream& operator=(const ulogstream&);
529  std::basic_stringstream<Ch>* stream;
530 
531 };
532 #endif
533 
534 
535 } // namespace log4cxx
536 
537 
538 #if LOG4CXX_USE_GLOBAL_SCOPE_TEMPLATE
539 //
540 // VC6 will fail to compile if class-scope templates
541 // are used to handle arbitrary insertion operations.
542 // However, using global namespace insertion operations
543 // run into LOGCXX-150.
544 
549 template <class V>
550 inline log4cxx::logstream& operator<<(log4cxx::logstream& os, const V& val)
551 {
552  if (LOG4CXX_UNLIKELY(os.isEnabled()))
553  {
554  ((std::basic_ostream<char>&) os) << val;
555  }
556 
557  return os;
558 }
559 
560 #if LOG4CXX_WCHAR_T_API
561 
565 template <class V>
566 inline log4cxx::wlogstream& operator<<(log4cxx::wlogstream& os, const V& val)
567 {
568  if (LOG4CXX_UNLIKELY(os.isEnabled()))
569  {
570  ((std::basic_ostream<wchar_t>&) os) << val;
571  }
572 
573  return os;
574 }
575 #endif
576 #endif
577 
578 #if !defined(LOG4CXX_ENDMSG)
579  #if LOG4CXX_LOGSTREAM_ADD_NOP
580  #define LOG4CXX_ENDMSG (log4cxx::logstream_manipulator) log4cxx::logstream_base::nop >> LOG4CXX_LOCATION << (log4cxx::logstream_manipulator) log4cxx::logstream_base::endmsg
581  #else
582  #define LOG4CXX_ENDMSG LOG4CXX_LOCATION << (log4cxx::logstream_manipulator) log4cxx::logstream_base::endmsg
583  #endif
584 #endif
585 
586 
587 #endif //_LOG4CXX_STREAM_H
logstream_base &(* logstream_manipulator)(logstream_base &)
Definition: stream.h:215
log4cxx::logstream & operator<<(const V &val)
Template to allow any class with an std::basic_ostream inserter to be applied to this class...
Definition: stream.h:286
An STL-like stream API for log4cxx using char as the character type.
Definition: stream.h:226
std::shared_ptr< Level > LevelPtr
Definition: optionconverter.h:27
unsigned short UniChar
Definition: logstring.h:44
log4cxx::wlogstream & operator<<(const V &val)
Template to allow any class with an std::basic_ostream inserter to be applied to this class...
Definition: stream.h:390
Base class for the basic_logstream template which attempts to emulate std::basic_ostream but attempts...
Definition: stream.h:37
const struct __CFString * CFStringRef
Definition: logstring.h:36
std::basic_ostream< char > & operator<<(CharMessageBuffer &os, const V &val)
Definition: messagebuffer.h:199
An STL-like stream API for log4cxx using wchar_t as the character type.
Definition: stream.h:329
An STL-like stream API for log4cxx using UniChar as the character type.
Definition: stream.h:433
ulogstream & operator<<(const V &val)
Template to allow any class with an std::basic_ostream inserter to be applied to this class...
Definition: stream.h:501
This class represents the location of a logging statement.
Definition: locationinfo.h:33
#define LOG4CXX_UNLIKELY(expr)
Provides optimization hint to the compiler to optimize for the expression being false.
Definition: logger.h:1756
std::shared_ptr< Logger > LoggerPtr
Definition: defaultloggerfactory.h:26
Definition: appender.h:32
bool isEnabled() const
Returns true if the current level is the same or high as the level of logger at time of construction ...
Definition: stream.h:124