log4tango  5.0.0
LoggerStream.hh
Go to the documentation of this file.
1 //
2 // LoggerStream.hh
3 //
4 // Copyright (C) : 2000 - 2002
5 // LifeLine Networks BV (www.lifeline.nl). All rights reserved.
6 // Bastiaan Bakker. All rights reserved.
7 //
8 // 2004,2005,2006,2007,2008,2009,2010,2011,2012
9 // Synchrotron SOLEIL
10 // L'Orme des Merisiers
11 // Saint-Aubin - BP 48 - France
12 //
13 // This file is part of log4tango.
14 //
15 // Log4ango is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU Lesser General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 // Log4tango is distributed in the hope that it will be useful,
21 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 // GNU Lesser General Public License for more details.
24 //
25 // You should have received a copy of the GNU Lesser General Public License
26 // along with Log4Tango. If not, see <http://www.gnu.org/licenses/>.
27 
28 #ifndef _LOG4TANGO_LOGGER_STREAM_H
29 #define _LOG4TANGO_LOGGER_STREAM_H
30 
31 #include <log4tango/Portability.hh>
32 #include <log4tango/Level.hh>
33 #ifdef LOG4TANGO_HAVE_SSTREAM
34 # include <sstream>
35 #endif
37 
38 namespace log4tango {
39 
40 //-----------------------------------------------------------------------------
41 // FORWARD DECLARATION
42 //-----------------------------------------------------------------------------
44 class LoggerStream;
45 
46 //-----------------------------------------------------------------------------
47 // DEFINE WHAT IS A LS_TERMINATOR
48 //-----------------------------------------------------------------------------
49 typedef LoggerStream& (*ls_terminator) (LoggerStream&);
50 
51 
52 typedef class std::ios_base&(*StdIosFlag)(class std::ios_base&);
53 
54 
55 //-----------------------------------------------------------------------------
56 // class : LoggerStream
57 //-----------------------------------------------------------------------------
59 {
60 public:
61 
69  LOG4TANGO_EXPORT LoggerStream(Logger& logger, Level::Value level, bool filter = true);
70 
75 
80  inline LOG4TANGO_EXPORT Logger& get_logger (void) const {
81  return _logger;
82  };
83 
88  inline LOG4TANGO_EXPORT Level::Value get_level (void) const {
89  return _level;
90  };
91 
98  return *this;
99  }
100 
108  flush();
109  return *this;
110  }
111 
118  inline LOG4TANGO_EXPORT LoggerStream& operator<< (LOG4TANGO_UNUSED(ls_terminator endoflog)) {
119  flush();
120  return *this;
121  }
122 
127  LOG4TANGO_EXPORT void flush (void);
128 
134 #ifdef WIN32
135  inline LOG4TANGO_EXPORT LoggerStream& operator<< (std::ios_base&(_cdecl *_F)(std::ios_base&)) {
136 #else
137  inline LOG4TANGO_EXPORT LoggerStream& operator<< (std::ios_base&(*_F)(std::ios_base&)) {
138 #endif
139 #ifndef LOG4TANGO_HAVE_SSTREAM
140  if (!_buffer)
141  _buffer = new std::ostringstream;
142 #endif
143  if (_buffer)
144  (*_F)(*(std::ios_base *)(_buffer));
145  return *this;
146  }
147 
153  template<typename T> LoggerStream& operator<< (const T& t) {
154  if (_level != Level::OFF) {
155 #ifndef LOG4TANGO_HAVE_SSTREAM
156  if (!_buffer)
157  _buffer = new std::ostringstream;
158 #endif
159  if (_buffer)
160  (*_buffer) << t;
161  }
162  return *this;
163  }
164 
165 private:
166 
167  Logger& _logger;
168 
169  Level::Value _level;
170 
171  bool _filter;
172 
173  std::ostringstream* _buffer;
174 };
175 
176 } // namespace log4tango
177 
178 namespace std {
179  //-- A dummy <ls_terminator>
181  return ls;
182  }
183 } // namespace std
184 
185 #endif // _LOG4TANGO_LOGGER_STREAM_H