TPIE

2362a60
tpie_log.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 2008, 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_LOG_H
21 #define _TPIE_LOG_H
22 
27 #include <vector>
28 #include <stack>
29 #include <memory>
30 #include <tpie/config.h>
31 #include <tpie/logstream.h>
32 #include <fstream>
33 
34 namespace tpie {
35 
37 class file_log_target: public log_target {
38 private:
39  std::stack<std::string> groups;
40 public:
41  std::ofstream m_out;
42  std::string m_path;
43  log_level m_threshold;
44 
48  file_log_target(log_level threshold);
49 
54  void log(log_level level, const char * message, size_t);
55 
60  void begin_group(const std::string & name);
61 
65  void end_group();
66 private:
67  std::string build_prefix(size_t length);
68 };
69 
72 private:
73  std::stack<std::string> groups;
74 public:
75  log_level m_threshold;
76 
80  stderr_log_target(log_level threshold);
81 
87  void log(log_level level, const char * message, size_t size);
88 
93  void begin_group(const std::string & name);
94 
98  void end_group();
99 private:
100  std::string build_prefix(size_t length);
101 };
102 
103 
104 
105 
110 const std::string& log_name();
111 
115 void init_default_log();
116 
120 void finish_default_log();
121 
122 namespace log_bits {
123 
124 extern std::vector<std::shared_ptr<logstream> > log_instances;
125 
126 void initiate_log_level(log_level level);
127 
128 void flush_logs();
129 
130 }
131 
132 inline logstream & get_log_by_level(log_level level) {
133  using namespace log_bits;
134  if (log_instances.size() <= level || log_instances[level].get() == 0)
135  initiate_log_level(level);
136  return *log_instances[level];
137 }
138 
142 inline logstream & log_fatal() {return get_log_by_level(LOG_FATAL);}
143 
147 inline logstream & log_error() {return get_log_by_level(LOG_ERROR);}
148 
152 inline logstream & log_info() {return get_log_by_level(LOG_INFORMATIONAL);}
153 
157 inline logstream & log_warning() {return get_log_by_level(LOG_WARNING);}
158 
162 inline logstream & log_app_debug() {return get_log_by_level(LOG_APP_DEBUG);}
163 
167 inline logstream & log_debug() {return get_log_by_level(LOG_DEBUG);}
168 
172 inline logstream & log_mem_debug() {return get_log_by_level(LOG_MEM_DEBUG);}
173 
175 private:
176  bool m_orig;
177 public:
178  inline bool get_orig() {return m_orig;}
179  inline scoped_log_enabler(bool e) {
180  m_orig = log_bits::logging_disabled;
181  log_bits::logging_disabled = !e;
182  }
183  inline ~scoped_log_enabler() {
184  log_bits::logging_disabled = m_orig;
185  }
186 };
187 
188 namespace log_bits {
189 
191 private:
192  static bool s_init;
193  static log_level s_level;
194 
195  logstream & get_log() {
196  if (!s_init) {
197  s_init = true;
198  s_level = LOG_INFORMATIONAL;
199  }
200  switch (s_level) {
201  case LOG_FATAL:
202  return log_fatal();
203  case LOG_ERROR:
204  return log_error();
205  case LOG_INFORMATIONAL:
206  return log_info();
207  case LOG_WARNING:
208  return log_warning();
209  case LOG_APP_DEBUG:
210  return log_app_debug();
211  case LOG_DEBUG:
212  return log_debug();
213  case LOG_MEM_DEBUG:
214  return log_mem_debug();
215  case LOG_USER1:
216  case LOG_USER2:
217  case LOG_USER3:
218  break;
219  }
220  return log_info();
221  }
222 
223 public:
224  log_selector & operator<<(log_level_manip mi) {
225  set_level(mi.get_level());
226  return *this;
227  }
228 
229  template <typename T>
230  logstream & operator<<(const T & x) {
231  logstream & res = get_log();
232  res << x;
233  return res;
234  }
235 
236  void flush() {
237  get_log().flush();
238  }
239 
240  void set_level(log_level level) {
241  s_init = true;
242  s_level = level;
243  }
244 
245  void add_target(log_target * t) { add_log_target(t); }
246 
247  void remove_target(log_target * t) { remove_log_target(t); }
248 };
249 
250 } // namespace log_bits
251 
256 
257 #if TPL_LOGGING
258 #define TP_LOG_FLUSH_LOG tpie::get_log().flush()
260 
262 #define TP_LOG_FATAL(msg) tpie::log_fatal() << msg
263 #define TP_LOG_WARNING(msg) tpie::log_warning() << msg
265 #define TP_LOG_APP_DEBUG(msg) tpie::log_app_debug() << msg
267 #define TP_LOG_DEBUG(msg) tpie::log_debug() << msg
269 #define TP_LOG_MEM_DEBUG(msg) tpie::log_mem_debug() << msg
271 
272 #define TP_LOG_ID_MSG __FILE__ << " line " << __LINE__ << ": "
273 
275 #define TP_LOG_FATAL_ID(msg) TP_LOG_FATAL(TP_LOG_ID_MSG << msg << std::endl)
276 
278 #define TP_LOG_WARNING_ID(msg) TP_LOG_WARNING(TP_LOG_ID_MSG << msg << std::endl)
279 
281 #define TP_LOG_APP_DEBUG_ID(msg) TP_LOG_APP_DEBUG(TP_LOG_ID_MSG << msg << std::endl)
282 
284 #define TP_LOG_DEBUG_ID(msg) TP_LOG_DEBUG(TP_LOG_ID_MSG << msg << std::endl)
285 
287 #define TP_LOG_MEM_DEBUG_ID(msg) TP_LOG_MEM_DEBUG(TP_LOG_ID_MSG << msg << std::endl)
288 
289 #else // !TPL_LOGGING
290 
291 // We are not compiling logging.
292 #define TP_LOG_FATAL(msg)
293 #define TP_LOG_WARNING(msg)
294 #define TP_LOG_APP_DEBUG(msg)
295 #define TP_LOG_DEBUG(msg)
296 #define TP_LOG_MEM_DEBUG(msg)
297 
298 #define TP_LOG_FATAL_ID(msg)
299 #define TP_LOG_WARNING_ID(msg)
300 #define TP_LOG_APP_DEBUG_ID(msg)
301 #define TP_LOG_DEBUG_ID(msg)
302 #define TP_LOG_MEM_DEBUG_ID(msg)
303 
304 #define TP_LOG_FLUSH_LOG {}
305 
306 #endif // TPL_LOGGING
307 
308 } // tpie namespace
309 
310 #endif // _TPIE_LOG_H
Logging level for warnings concerning memory allocation and deallocation.
Definition: loglevel.h:57
A log is like a regular output stream, but it also supports messages at different priorities...
Definition: logstream.h:85
LOG_FATAL is the highest error level and is used for all kinds of errors that would normally impair s...
Definition: loglevel.h:37
LOG_ERROR is used for none fatal errors.
Definition: loglevel.h:40
LOG_WARNING is used for warnings.
Definition: loglevel.h:43
A simple logger that writes messages to a tpie temporary file.
Definition: tpie_log.h:37
logstream & log_info()
Return logstream for writing info log messages.
Definition: tpie_log.h:152
A simple logger that writes messages to stderr.
Definition: tpie_log.h:71
logstream class used by definitions in tpie_log.h.
file_log_target(log_level threshold)
Construct a new file logger.
const std::string & log_name()
Returns the file name of the log stream.
void log(log_level level, const char *message, size_t size)
Implement log_target virtual method to record message.
LOG_INFORMATIONAL is used for informational messagse.
Definition: loglevel.h:46
stderr_log_target(log_level threshold)
Construct a new stderr logger.
void end_group()
Closes the most recently created logging group.
log_level
TPIE logging levels, from higest priority to lowest.
Definition: loglevel.h:33
logstream & log_debug()
Return logstream for writing debug log messages.
Definition: tpie_log.h:167
void begin_group(const std::string &name)
Creates a new logging group.
logstream & log_fatal()
Return logstream for writing fatal log messages.
Definition: tpie_log.h:142
logstream & log_app_debug()
Return logstream for writing app_debug log messages.
Definition: tpie_log.h:162
Logging levels to be further defined by user applications.
Definition: loglevel.h:60
log_bits::log_selector get_log()
Returns the only logstream object.
Definition: tpie_log.h:255
void init_default_log()
Used by tpie_init to initialize the log subsystem.
void begin_group(const std::string &name)
Creates a new logging group.
void end_group()
Closes the most recently created logging group.
LOG_DEBUG is the lowest level and is used by the TPIE library for logging debugging information...
Definition: loglevel.h:54
void finish_default_log()
Used by tpie_finish to deinitialize the log subsystem.
logstream & log_error()
Return logstream for writing error log messages.
Definition: tpie_log.h:147
logstream & log_warning()
Return logstream for writing warning log messages.
Definition: tpie_log.h:157
logstream & log_mem_debug()
Return logstream for writing mem_debug log messages.
Definition: tpie_log.h:172
void log(log_level level, const char *message, size_t)
Implement log_target virtual method to record message.
LOG_APP_DEBUG can be used by applications built on top of TPIE, for logging debugging information...
Definition: loglevel.h:50