TPIE

11a2c2d
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 #include <tpie/tpie_export.h>
28 #include <tpie/config.h>
29 #include <tpie/loglevel.h>
30 #include <streambuf>
31 #include <ostream>
32 
33 namespace tpie {
34 
35 namespace log_bits {
36 
37 extern bool logging_disabled;
38 
39 }
40 
41 struct log_target {
42  virtual void log(log_level level, const char * message, size_t message_size) = 0;
43  virtual ~log_target() { }
44  virtual void begin_group(const std::string &) {};
45  virtual void end_group() {};
46 };
47 
48 TPIE_EXPORT void add_log_target(log_target * t);
49 TPIE_EXPORT void remove_log_target(log_target * t);
50 TPIE_EXPORT void log_to_targets(log_level level, const char * data, size_t size);
51 
52 TPIE_EXPORT void begin_log_group(const std::string & name);
53 TPIE_EXPORT void end_log_group();
54 
55 class log_stream_buf: public std::basic_streambuf<char, std::char_traits<char> > {
56 private:
57  const static size_t buff_size = 2048;
58  const static size_t max_targets = 8;
59 
60  char m_buff[buff_size];
61  log_level m_level;
62 
63 public:
65  virtual ~log_stream_buf();
66  void flush();
67  virtual int overflow(int c = traits_type::eof()) override;
68  virtual int sync() override;
69 
70  // Deprecated:
71  void add_target(log_target * t) {add_log_target(t);}
72  void remove_target(log_target * t) {remove_log_target(t);}
73 };
74 
84 class logstream: public std::ostream {
85 private:
86  log_stream_buf m_buff;
87 public:
91  inline logstream(log_level level=LOG_INFORMATIONAL): std::ostream(&m_buff), m_buff(level) {}
92 
97  void add_target(log_target * t) {add_log_target(t);}
98 
103  void remove_target(log_target * t) {remove_log_target(t);}
104 };
105 
107 private:
108  log_level level;
109 
110 public:
111  log_level_manip(log_level p) : level(p) {}
112 
113  log_level get_level() const { return level; }
114 };
115 
116 inline log_level_manip setlevel(log_level p) { return log_level_manip(p); }
117 
121 class TPIE_EXPORT log_group {
122 public:
123  log_group(const std::string & name);
124  ~log_group();
125 };
126 
127 } // tpie namespace
128 
129 
130 #endif //__TPIE_LOGSTREAM_H__
131 
tpie::log_level_manip
Definition: logstream.h:106
tpie::LOG_INFORMATIONAL
@ LOG_INFORMATIONAL
LOG_INFORMATIONAL is used for informational messagse.
Definition: loglevel.h:46
tpie::log_level
log_level
TPIE logging levels, from higest priority to lowest.
Definition: loglevel.h:33
loglevel.h
Logging levels.
tpie::logstream::remove_target
void remove_target(log_target *t)
Definition: logstream.h:103
tpie::log_target
Definition: logstream.h:41
tpie::logstream::add_target
void add_target(log_target *t)
Definition: logstream.h:97
tpie::logstream::logstream
logstream(log_level level=LOG_INFORMATIONAL)
Constructor.
Definition: logstream.h:91
tpie::logstream
A log is like a regular output stream, but it also supports messages at different priorities,...
Definition: logstream.h:84
tpie::log_stream_buf
Definition: logstream.h:55
tpie::log_group
RAII-style management for log groups.
Definition: logstream.h:121
tpie
Definition: access_type.h:26