Apache Log4cxx  Version 1.5.0
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_NS
26 {
27 
37 class LOG4CXX_EXPORT logstream_base
38 {
39  public:
47  const LOG4CXX_NS::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 
102 
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_NS::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_NS::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_NS::LoggerPtr logger;
208  LOG4CXX_NS::LevelPtr level;
212  LOG4CXX_NS::spi::LocationInfo location;
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:
234  const LOG4CXX_NS::LevelPtr& level);
235 
239  logstream(const Ch* loggerName,
240  const LOG4CXX_NS::LevelPtr& level);
241 
245  logstream(const std::basic_string<Ch>& loggerName,
246  const LOG4CXX_NS::LevelPtr& level);
247 
249 
253  logstream& operator<<(std::ios_base & (*manip)(std::ios_base&));
254 
259 
267  logstream& operator<<(const LOG4CXX_NS::spi::LocationInfo& location);
268 
273  logstream& operator>>(const LOG4CXX_NS::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_NS::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_NS::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:
337  const LOG4CXX_NS::LevelPtr& level);
338 
342  wlogstream(const Ch* loggerName,
343  const LOG4CXX_NS::LevelPtr& level);
344 
348  wlogstream(const std::basic_string<Ch>& loggerName,
349  const LOG4CXX_NS::LevelPtr& level);
350 
352 
356  wlogstream& operator<<(std::ios_base & (*manip)(std::ios_base&));
357 
362 
370  wlogstream& operator<<(const LOG4CXX_NS::spi::LocationInfo& location);
371 
376  wlogstream& operator>>(const LOG4CXX_NS::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_NS::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_NS::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
424 
433 class LOG4CXX_EXPORT ulogstream : public logstream_base
434 {
435  typedef UniChar Ch;
436  public:
441  const LOG4CXX_NS::LevelPtr& level);
442 
443 #if LOG4CXX_UNICHAR_API
444 
447  ulogstream(const Ch* loggerName,
448  const LOG4CXX_NS::LevelPtr& level);
449 
453  ulogstream(const std::basic_string<Ch>& loggerName,
454  const LOG4CXX_NS::LevelPtr& level);
455 #endif
456 
457 #if LOG4CXX_CFSTRING_API
458  ulogstream(const CFStringRef& loggerName,
459  const LOG4CXX_NS::LevelPtr& level);
460 #endif
461 
463 
467  ulogstream& operator<<(std::ios_base & (*manip)(std::ios_base&));
468 
473 
481  ulogstream& operator<<(const LOG4CXX_NS::spi::LocationInfo& location);
482 
487  ulogstream& operator>>(const LOG4CXX_NS::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_NS::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_NS::logstream& operator<<(LOG4CXX_NS::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_NS::wlogstream& operator<<(LOG4CXX_NS::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_NS::logstream_manipulator) LOG4CXX_NS::logstream_base::nop >> LOG4CXX_LOCATION << (LOG4CXX_NS::logstream_manipulator) LOG4CXX_NS::logstream_base::endmsg
581  #else
582  #define LOG4CXX_ENDMSG LOG4CXX_LOCATION << (LOG4CXX_NS::logstream_manipulator) LOG4CXX_NS::logstream_base::endmsg
583  #endif
584 #endif
585 
586 
587 #endif //_LOG4CXX_STREAM_H
log4cxx::logstream::refresh_stream_state
virtual void refresh_stream_state()
logger.h
log4cxx::ulogstream::erase
virtual void erase()
Erase any content in the message construction buffer.
log4cxx::logstream_base::~logstream_base
virtual ~logstream_base()
Destructor.
log4cxx::logstream_base::fill
int fill(int newval)
Set fill character.
log4cxx::logstream_base::width
int width()
get width.
log4cxx::logstream_base::isEnabled
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
log4cxx::wlogstream::wlogstream
wlogstream(const log4cxx::LoggerPtr &logger, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::logstream::operator>>
logstream & operator>>(const log4cxx::spi::LocationInfo &location)
Alias for insertion operator for location.
log4cxx::wlogstream::wlogstream
wlogstream(const std::basic_string< Ch > &loggerName, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::UniChar
unsigned short UniChar
Definition: logstring.h:38
log4cxx::logstream::logstream
logstream(const Ch *loggerName, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::ulogstream::~ulogstream
~ulogstream()
log4cxx::logstream_manipulator
logstream_base &(* logstream_manipulator)(logstream_base &)
Definition: stream.h:215
log4cxx::logstream_base::logstream_base
logstream_base(const log4cxx::LoggerPtr &logger, const log4cxx::LevelPtr &level)
Create new instance.
log4cxx::logstream_base::isEnabledFor
bool isEnabledFor(const LevelPtr &level) const
Returns if logger is currently enabled for the specified level.
log4cxx::logstream::logstream
logstream(const std::basic_string< Ch > &loggerName, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::ulogstream::refresh_stream_state
virtual void refresh_stream_state()
log4cxx::logstream_base::get_stream_state
virtual void get_stream_state(std::ios_base &base, std::ios_base &mask, int &fill, bool &fillSet) const =0
Copy state of embedded stream (if any) to value and mask instances of std::ios_base and return fill c...
log4cxx::logstream_base::set_stream_state
bool set_stream_state(std::ios_base &os, int &fillchar)
Sets the state of the embedded stream (if any) to the state of the formatting info.
log4cxx::logstream::operator<<
logstream & operator<<(const log4cxx::LevelPtr &level)
Insertion operator for level.
log4cxx::wlogstream::operator>>
wlogstream & operator>>(const log4cxx::spi::LocationInfo &location)
Alias for insertion operator for location.
log4cxx::logstream_base::log
virtual void log(LoggerPtr &logger, const LevelPtr &level, const log4cxx::spi::LocationInfo &location)=0
Dispatches the pending log request.
CFStringRef
const struct __CFString * CFStringRef
Definition: logstring.h:30
log4cxx::LevelPtr
std::shared_ptr< Level > LevelPtr
Definition: optionconverter.h:27
log4cxx::wlogstream::operator<<
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
log4cxx::logstream_base::endmsg
static logstream_base & endmsg(logstream_base &)
end of message manipulator, triggers logging.
log4cxx::logstream_base::flags
std::ios_base::fmtflags flags(std::ios_base::fmtflags newflags)
Set flags.
log4cxx::ulogstream::operator<<
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
log4cxx::ulogstream::operator>>
ulogstream & operator>>(const log4cxx::spi::LocationInfo &location)
Alias for insertion operator for location.
log4cxx::logstream_base::fill
int fill()
Get fill character.
log4cxx::logstream::get_stream_state
virtual void get_stream_state(std::ios_base &base, std::ios_base &mask, int &fill, bool &fillSet) const
Copy state of embedded stream (if any) to value and mask instances of std::ios_base and return fill c...
log4cxx::logstream_base::precision
int precision()
get precision.
log4cxx::logstream::log
virtual void log(LoggerPtr &logger, const LevelPtr &level, const log4cxx::spi::LocationInfo &location)
Dispatches the pending log request.
log4cxx::wlogstream::erase
virtual void erase()
Erase any content in the message construction buffer.
log4cxx::logstream_base::setLevel
void setLevel(const LevelPtr &level)
Set the level.
log4cxx::logstream::logstream
logstream(const log4cxx::LoggerPtr &logger, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::logstream_base::insert
void insert(std::ios_base &(*manip)(std::ios_base &))
Insertion operator for std::fixed and similar manipulators.
log4cxx::ulogstream::ulogstream
ulogstream(const CFStringRef &loggerName, const log4cxx::LevelPtr &level)
log4cxx::logstream_base::erase
virtual void erase()=0
Erase any content in the message construction buffer.
log4cxx::logstream::operator<<
logstream & operator<<(std::ios_base &(*manip)(std::ios_base &))
Insertion operator for std::fixed and similar manipulators.
log4cxx::logstream::erase
virtual void erase()
Erase any content in the message construction buffer.
log4cxx::wlogstream::log
virtual void log(LoggerPtr &logger, const LevelPtr &level, const log4cxx::spi::LocationInfo &location)
Dispatches the pending log request.
log4cxx::wlogstream::operator<<
wlogstream & operator<<(std::ios_base &(*manip)(std::ios_base &))
Insertion operator for std::fixed and similar manipulators.
log4cxx::ulogstream::operator<<
ulogstream & operator<<(const log4cxx::spi::LocationInfo &location)
Insertion operator for location.
log4cxx::logstream_base::setLocation
void setLocation(const log4cxx::spi::LocationInfo &location)
Sets the location for subsequent log requests.
log4cxx::wlogstream::operator<<
wlogstream & operator<<(const log4cxx::LevelPtr &level)
Insertion operator for level.
log4cxx::wlogstream::refresh_stream_state
virtual void refresh_stream_state()
log4cxx::wlogstream::~wlogstream
~wlogstream()
log4cxx::ulogstream::ulogstream
ulogstream(const log4cxx::LoggerPtr &logger, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::logstream_base::width
int width(int newval)
set width.
log4cxx::logstream
An STL-like stream API for log4cxx using char as the character type.
Definition: stream.h:227
log4cxx::logstream::operator<<
logstream & operator<<(logstream_manipulator manip)
Insertion operator for logstream_base::endmsg.
log4cxx::logstream_base::precision
int precision(int newval)
set precision.
log4cxx::logstream_base::end_message
void end_message()
end of message action.
log4cxx::ulogstream::operator<<
ulogstream & operator<<(const log4cxx::LevelPtr &level)
Insertion operator for level.
log4cxx::ulogstream::log
virtual void log(LoggerPtr &logger, const LevelPtr &level, const log4cxx::spi::LocationInfo &location)
Dispatches the pending log request.
locationinfo.h
log4cxx::ulogstream::ulogstream
ulogstream(const Ch *loggerName, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::wlogstream
An STL-like stream API for log4cxx using wchar_t as the character type.
Definition: stream.h:330
log4cxx::wlogstream::operator<<
wlogstream & operator<<(logstream_manipulator manip)
Insertion operator for logstream_base::endmsg.
log4cxx::ulogstream::operator<<
ulogstream & operator<<(logstream_manipulator manip)
Insertion operator for logstream_base::endmsg.
log4cxx::logstream::operator<<
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
log4cxx::logstream::operator<<
logstream & operator<<(const log4cxx::spi::LocationInfo &location)
Insertion operator for location.
log4cxx::ulogstream::get_stream_state
virtual void get_stream_state(std::ios_base &base, std::ios_base &mask, int &fill, bool &fillSet) const
Copy state of embedded stream (if any) to value and mask instances of std::ios_base and return fill c...
log4cxx::logstream_base::refresh_stream_state
virtual void refresh_stream_state()=0
log4cxx::ulogstream::ulogstream
ulogstream(const std::basic_string< Ch > &loggerName, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::wlogstream::operator<<
wlogstream & operator<<(const log4cxx::spi::LocationInfo &location)
Insertion operator for location.
log4cxx::logstream_base::setf
std::ios_base::fmtflags setf(std::ios_base::fmtflags newflags, std::ios_base::fmtflags mask)
Set flags.
log4cxx::helpers::operator<<
std::basic_ostream< char > & operator<<(CharMessageBuffer &os, const V &val)
Definition: log4cxx/helpers/messagebuffer.h:208
LOG4CXX_UNLIKELY
#define LOG4CXX_UNLIKELY(expr)
Provides optimization hint to the compiler to optimize for the expression being false.
Definition: log4cxx/logger.h:2141
log4cxx::ulogstream
An STL-like stream API for log4cxx using UniChar as the character type.
Definition: stream.h:434
log4cxx::LoggerPtr
std::shared_ptr< Logger > LoggerPtr
Definition: defaultloggerfactory.h:26
log4cxx::ulogstream::operator<<
ulogstream & operator<<(std::ios_base &(*manip)(std::ios_base &))
Insertion operator for std::fixed and similar manipulators.
log4cxx::wlogstream::wlogstream
wlogstream(const Ch *loggerName, const log4cxx::LevelPtr &level)
Constructor.
log4cxx::wlogstream::get_stream_state
virtual void get_stream_state(std::ios_base &base, std::ios_base &mask, int &fill, bool &fillSet) const
Copy state of embedded stream (if any) to value and mask instances of std::ios_base and return fill c...
log4cxx::logstream_base::setf
std::ios_base::fmtflags setf(std::ios_base::fmtflags newflags)
Set flags.
log4cxx::logstream::~logstream
~logstream()
log4cxx::logstream_base
Base class for the basic_logstream template which attempts to emulate std::basic_ostream but attempts...
Definition: stream.h:38
log4cxx::logstream_base::nop
static logstream_base & nop(logstream_base &)
no-operation manipulator, Used to avoid ambiguity with VC6.