TPIE

v1.1rc1-6-g0c97303
logstream.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 // Copyright 2011, The TPIE development team
4 //
5 // This file is part of TPIE.
6 //
7 // TPIE is free software: you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
19 
20 #ifndef _TPIE_LOGSTREAM_H
21 #define _TPIE_LOGSTREAM_H
22 
27 #ifndef __TPIE_LOGSTREAM_H__
28 #define __TPIE_LOGSTREAM_H__
29 
30 #include <tpie/config.h>
31 #include <tpie/loglevel.h>
32 #include <streambuf>
33 #include <ostream>
34 
35 namespace tpie {
36 
37 struct log_target {
38  virtual void log(log_level level, const char * message, size_t message_size) = 0;
39  virtual ~log_target() { }
40 };
41 
42 class log_stream_buf: public std::basic_streambuf<char, std::char_traits<char> > {
43 private:
44  const static size_t buff_size = 2048;
45  const static size_t max_targets = 8;
46 
47  char m_buff[buff_size];
48  log_target * m_log_targets[max_targets];
49  size_t m_log_target_count;
50  log_level m_level;
51  bool m_enabled;
52 
53 public:
55  virtual ~log_stream_buf();
56  void flush();
57  virtual int overflow(int c = traits_type::eof());
58  virtual int sync();
59  void set_level(log_level level);
60  void add_target(log_target * t);
61  void remove_target(log_target * t);
62  inline void enable(bool e) {flush(); m_enabled=e;}
63  inline bool enabled() {return m_enabled;}
64 };
65 
66 
76 class logstream: public std::ostream {
77 private:
78  log_stream_buf m_buff;
79 public:
83  inline logstream(log_level level=LOG_INFORMATIONAL): std::ostream(&m_buff), m_buff(level) {}
84 
88  inline void add_target(log_target * t) {m_buff.add_target(t);}
89 
93  inline void remove_target(log_target * t) {m_buff.remove_target(t);}
94 
98  inline void set_level(log_level level) {m_buff.set_level(level);}
99 
100  inline void disable(bool d=false) {m_buff.enable(!d);}
101  inline void enable(bool e=true) {m_buff.enable(e);}
102  inline bool enabled() {return m_buff.enabled();}
103 };
104 
105 
110 template <class TP> class logmanip {
111  logstream& (*_f)(logstream&, TP);
112  TP _a;
113 public:
117  logmanip(logstream& (*f)(logstream&, TP), TP a) : _f(f), _a(a) {}
118 
123  friend logstream& operator<< (logstream& o, const logmanip<TP>& m) {
124  (*m._f)(o, m._a);
125  return o;
126  }
127 };
128 
129 logstream& manip_level(logstream& tpl, log_level p);
130 logmanip<log_level> setlevel(log_level p);
131 
132 } // tpie namespace
133 
134 
135 #endif //__TPIE_LOGSTREAM_H__
136 
137 #endif // _LOGSTREAM_H
A log is like a regular output stream, but it also supports messages at different priorities...
Definition: logstream.h:76
void set_level(log_level level)
Set the current level of logging.
Definition: logstream.h:98
The logmanip template is based on the omanip template from iomanip.h in the libg++ sources...
Definition: logstream.h:110
void remove_target(log_target *t)
Remove a target for the log messages.
Definition: logstream.h:93
LOG_INFORMATIONAL is used for informational messagse.
Definition: loglevel.h:46
log_level
TPIE logging levels, from higest priority to lowest.
Definition: loglevel.h:33
logmanip(logstream &(*f)(logstream &, TP), TP a)
Constructor.
Definition: logstream.h:117
Logging levels.
logstream(log_level level=LOG_INFORMATIONAL)
Constructor.
Definition: logstream.h:83
void add_target(log_target *t)
Add a target for the log messages.
Definition: logstream.h:88