TPIE

2362a60
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 namespace log_bits {
38 
39 extern bool logging_disabled;
40 
41 }
42 
43 struct log_target {
44  virtual void log(log_level level, const char * message, size_t message_size) = 0;
45  virtual ~log_target() { }
46  virtual void begin_group(const std::string &) {};
47  virtual void end_group() {};
48 };
49 
50 void add_log_target(log_target * t);
51 void remove_log_target(log_target * t);
52 
53 void begin_log_group(const std::string & name);
54 void end_log_group();
55 
56 class log_stream_buf: public std::basic_streambuf<char, std::char_traits<char> > {
57 private:
58  const static size_t buff_size = 2048;
59  const static size_t max_targets = 8;
60 
61  char m_buff[buff_size];
62  log_level m_level;
63 
64 public:
66  virtual ~log_stream_buf();
67  void flush();
68  virtual int overflow(int c = traits_type::eof()) override;
69  virtual int sync() override;
70 
71  // Deprecated:
72  void add_target(log_target * t) {add_log_target(t);}
73  void remove_target(log_target * t) {remove_log_target(t);}
74 };
75 
85 class logstream: public std::ostream {
86 private:
87  log_stream_buf m_buff;
88 public:
92  inline logstream(log_level level=LOG_INFORMATIONAL): std::ostream(&m_buff), m_buff(level) {}
93 
98  void add_target(log_target * t) {add_log_target(t);}
99 
104  void remove_target(log_target * t) {remove_log_target(t);}
105 };
106 
108 private:
109  log_level level;
110 
111 public:
112  log_level_manip(log_level p) : level(p) {}
113 
114  log_level get_level() const { return level; }
115 };
116 
117 inline log_level_manip setlevel(log_level p) { return log_level_manip(p); }
118 
122 class log_group {
123 public:
124  log_group(const std::string & name);
125  ~log_group();
126 };
127 
128 } // tpie namespace
129 
130 
131 #endif //__TPIE_LOGSTREAM_H__
132 
133 #endif // _LOGSTREAM_H
A log is like a regular output stream, but it also supports messages at different priorities...
Definition: logstream.h:85
void remove_target(log_target *t)
Definition: logstream.h:104
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
Logging levels.
RAII-style management for log groups.
Definition: logstream.h:122
logstream(log_level level=LOG_INFORMATIONAL)
Constructor.
Definition: logstream.h:92
void add_target(log_target *t)
Definition: logstream.h:98