TPIE

v1.1rc1-6-g0c97303
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 <tpie/config.h>
28 #include <tpie/logstream.h>
29 #include <fstream>
30 
31 namespace tpie {
32 
34 class file_log_target: public log_target {
35 public:
36  std::ofstream m_out;
37  std::string m_path;
38  log_level m_threshold;
39 
43  file_log_target(log_level threshold);
44 
49  void log(log_level level, const char * message, size_t);
50 };
51 
54 public:
55  log_level m_threshold;
56 
60  stderr_log_target(log_level threshold);
61 
67  void log(log_level level, const char * message, size_t size);
68 };
69 
70 
71 
72 
77 const std::string& log_name();
78 
82 void init_default_log();
83 
87 void finish_default_log();
88 
89 extern logstream log_singleton;
90 
94 inline logstream & get_log() {return log_singleton;}
95 
99 inline logstream & log_fatal() {return get_log() << setlevel(LOG_FATAL);}
100 
104 inline logstream & log_error() {return get_log() << setlevel(LOG_ERROR);}
105 
109 inline logstream & log_info() {return get_log() << setlevel(LOG_INFORMATIONAL);}
110 
114 inline logstream & log_warning() {return get_log() << setlevel(LOG_WARNING);}
115 
119 inline logstream & log_app_debug() {return get_log() << setlevel(LOG_APP_DEBUG);}
120 
124 inline logstream & log_debug() {return get_log() << setlevel(LOG_DEBUG);}
125 
129 inline logstream & log_mem_debug() {return get_log() << setlevel(LOG_MEM_DEBUG);}
130 
132 private:
133  bool m_orig;
134 public:
135  inline bool get_orig() {return m_orig;}
136  inline scoped_log_enabler(bool e) {
137  m_orig = get_log().enabled();
138  get_log().enable(e);
139  }
140  inline ~scoped_log_enabler() {
141  get_log().enable(m_orig);
142  }
143 };
144 
145 #if TPL_LOGGING
146 #define TP_LOG_FLUSH_LOG tpie::get_log().flush()
148 
150 #define TP_LOG_FATAL(msg) tpie::log_fatal() << msg
151 #define TP_LOG_WARNING(msg) tpie::log_warning() << msg
153 #define TP_LOG_APP_DEBUG(msg) tpie::log_app_debug() << msg
155 #define TP_LOG_DEBUG(msg) tpie::log_debug() << msg
157 #define TP_LOG_MEM_DEBUG(msg) tpie::log_mem_debug() << msg
159 
160 #define TP_LOG_ID_MSG __FILE__ << " line " << __LINE__ << ": "
161 
163 #define TP_LOG_FATAL_ID(msg) TP_LOG_FATAL(TP_LOG_ID_MSG << msg << std::endl)
164 
166 #define TP_LOG_WARNING_ID(msg) TP_LOG_WARNING(TP_LOG_ID_MSG << msg << std::endl)
167 
169 #define TP_LOG_APP_DEBUG_ID(msg) TP_LOG_APP_DEBUG(TP_LOG_ID_MSG << msg << std::endl)
170 
172 #define TP_LOG_DEBUG_ID(msg) TP_LOG_DEBUG(TP_LOG_ID_MSG << msg << std::endl)
173 
175 #define TP_LOG_MEM_DEBUG_ID(msg) TP_LOG_MEM_DEBUG(TP_LOG_ID_MSG << msg << std::endl)
176 
177 #else // !TPL_LOGGING
178 
179 // We are not compiling logging.
180 #define TP_LOG_FATAL(msg)
181 #define TP_LOG_WARNING(msg)
182 #define TP_LOG_APP_DEBUG(msg)
183 #define TP_LOG_DEBUG(msg)
184 #define TP_LOG_MEM_DEBUG(msg)
185 
186 #define TP_LOG_FATAL_ID(msg)
187 #define TP_LOG_WARNING_ID(msg)
188 #define TP_LOG_APP_DEBUG_ID(msg)
189 #define TP_LOG_DEBUG_ID(msg)
190 #define TP_LOG_MEM_DEBUG_ID(msg)
191 
192 #define TP_LOG_FLUSH_LOG {}
193 
194 #endif // TPL_LOGGING
195 
196 } // tpie namespace
197 
198 #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:76
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:34
logstream & log_info()
Return logstream for writing info log messages.
Definition: tpie_log.h:109
logstream & get_log()
Returns the only logstream object.
Definition: tpie_log.h:94
A simple logger that writes messages to stderr.
Definition: tpie_log.h:53
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.
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:124
logstream & log_fatal()
Return logstream for writing fatal log messages.
Definition: tpie_log.h:99
logstream & log_app_debug()
Return logstream for writing app_debug log messages.
Definition: tpie_log.h:119
void init_default_log()
Used by tpie_init to initialize the log subsystem.
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:104
logstream & log_warning()
Return logstream for writing warning log messages.
Definition: tpie_log.h:114
logstream & log_mem_debug()
Return logstream for writing mem_debug log messages.
Definition: tpie_log.h:129
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