00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #ifndef SCALESTACK_KERNEL_LOGGER_H
00025 #define SCALESTACK_KERNEL_LOGGER_H
00026
00027 #include <cstdarg>
00028 #include <exception>
00029
00030 #include <scalestack/kernel/log.h>
00031
00032 namespace scalestack
00033 {
00034 namespace kernel
00035 {
00036
00040 class logger
00041 {
00042 public:
00043
00044 logger(size_t threshold);
00045
00046 virtual ~logger();
00047
00051 size_t get_threshold(void);
00052
00058 void set_threshold(size_t threshold);
00059
00066 void log_fatal(const char* format, ...) const;
00067
00073 void log_error(const char* format, ...) const;
00074
00080 void log_notice(const char* format, ...) const;
00081
00087 void log_info(const char* format, ...) const;
00088
00094 void log_debug(const char* format, ...) const;
00095
00096 protected:
00097
00098 size_t _threshold;
00099
00100 private:
00101
00105 logger(const logger&);
00106
00110 logger& operator=(const logger&);
00111
00119 virtual void _set_fatal_message(const char* format,
00120 std::va_list arguments) const = 0;
00121
00129 virtual void _log_message(log::level log_level,
00130 const char* format,
00131 std::va_list arguments) const = 0;
00132 };
00133
00134
00135
00136
00137
00138 inline logger::logger(size_t threshold):
00139 _threshold(threshold)
00140 {
00141 }
00142
00143 inline logger::~logger()
00144 {
00145 }
00146
00147 inline size_t logger::get_threshold(void)
00148 {
00149 return _threshold;
00150 }
00151
00152 inline void logger::set_threshold(size_t threshold)
00153 {
00154 _threshold = threshold;
00155 }
00156
00157 inline void logger::log_fatal(const char* format, ...) const
00158 {
00159 std::va_list arguments;
00160 va_start(arguments, format);
00161
00162
00163
00164
00165
00166
00167 try
00168 {
00169
00170
00171
00172
00173 if (_threshold >= log::LEVEL_ERROR)
00174 _log_message(log::LEVEL_FATAL, format, arguments);
00175 else
00176 _set_fatal_message(format, arguments);
00177 }
00178 catch (std::exception&)
00179 {
00180 va_end(arguments);
00181 throw;
00182 }
00183
00184 va_end(arguments);
00185 }
00186
00187 inline void logger::log_error(const char* format, ...) const
00188 {
00189 if (_threshold >= log::LEVEL_ERROR)
00190 {
00191 std::va_list arguments;
00192 va_start(arguments, format);
00193 _log_message(log::LEVEL_ERROR, format, arguments);
00194 va_end(arguments);
00195 }
00196 }
00197
00198 inline void logger::log_notice(const char* format, ...) const
00199 {
00200 if (_threshold >= log::LEVEL_NOTICE)
00201 {
00202 std::va_list arguments;
00203 va_start(arguments, format);
00204 _log_message(log::LEVEL_NOTICE, format, arguments);
00205 va_end(arguments);
00206 }
00207 }
00208
00209 inline void logger::log_info(const char* format, ...) const
00210 {
00211 if (_threshold >= log::LEVEL_INFO)
00212 {
00213 std::va_list arguments;
00214 va_start(arguments, format);
00215 _log_message(log::LEVEL_INFO, format, arguments);
00216 va_end(arguments);
00217 }
00218 }
00219
00220 inline void logger::log_debug(const char* format, ...) const
00221 {
00222 if (_threshold >= log::LEVEL_DEBUG)
00223 {
00224 std::va_list arguments;
00225 va_start(arguments, format);
00226 _log_message(log::LEVEL_DEBUG, format, arguments);
00227 va_end(arguments);
00228 }
00229 }
00230
00231 }
00232 }
00233
00234 #endif