Luiza  v03-01
logstream.h
1 // -*- mode: c++;
2 #ifndef logstream_h
3 #define logstream_h
4 
5 #include "streamlog/prefix.h"
6 
7 #include <iostream>
8 #include <map>
9 
10 namespace streamlog{
11 
12  class prefix_base ;
13  class logbuffer ;
14  class logscope ;
15 
16 
52  class logstream {
53 
54  friend class logscope ;
55  friend class logbuffer ;
56 
57 
58  typedef std::map< std::string, unsigned > LevelMap ;
59 
60  public :
61 
62  logstream() ;
63 
64  ~logstream() ;
65 
71  void init( std::ostream& os , const std::string name ) ;
72 
77  template<class T>
78  inline bool write() {
79 
80  // dont' call chek_level if T::active == false
81  return ( T::active && check_level<T>() ) ;
82  }
83 
89  template<class T>
90  inline bool would_write() {
91 
92  return ( T::active && T::level >= _level ) ;
93  }
94 
98  std::ostream& operator()() ;
99 
108  template <class T>
109  void addLevelName() {
110 
111  _map[ T::name() ] = T::level ;
112  }
113 
114 
115  // interface for friend classes: scope and logbuffer
116 
117  protected:
118 
122  void setLevel( unsigned level ) { _level = level ; }
123 
127  unsigned setLevel( const std::string& levelName ) ;
128 
130  prefix_base* prefix() { return _prefix ; }
131 
133  template<class T>
134  bool check_level() {
135 
136  if( T::level >= _level ){
137  _active = true ;
138  _prefix->_levelName = T::name() ;
139  }
140  return _active ;
141  }
142 
143 
144  private:
145 
147  class nullstream : public std::ostream {
148  public:
149  nullstream() : std::ios( 0 ), std::ostream( 0 ) {} ;
150  } ;
151 
152 
153  nullstream* _ns ; // the nullstream
154  std::ostream* _os ; // wrapper for actual ostream
155  unsigned _level ; // current log level
156  bool _active ; // boolean helper
157  logbuffer* _lb ; // log buffer adds prefix to everu log message
158  prefix_base* _prefix ; // prefix formatter
159  LevelMap _map ; // string map of level names
160 
161  } ;
162 
163  extern logstream out ;
164 
165 }
166 #endif
bool would_write()
Definition: logstream.h:90
bool check_level()
Definition: logstream.h:134
Definition: logbuffer.h:18
void init(std::ostream &os, const std::string name)
Definition: logstream.cc:41
logstream out
Definition: logstream.cc:94
std::ostream & operator()()
Definition: logstream.cc:80
Definition: logscope.h:23
Namespace of method for logging messages based on streamlog.
Definition: baselevels.h:20
Definition: prefix.h:19
prefix_base * prefix()
Definition: logstream.h:130
bool write()
Definition: logstream.h:78
Definition: logstream.h:52
void addLevelName()
Definition: logstream.h:109
void setLevel(unsigned level)
Definition: logstream.h:122