eXpress “1.5”
|
00001 00009 #ifndef __express__logger__ 00010 #define __express__logger__ 00011 00012 #include <iostream> 00013 #include <boost/thread.hpp> 00014 #include <boost/date_time/posix_time/posix_time.hpp> 00015 #include <cstdarg> 00016 #include <cstdio> 00017 00018 namespace pt = boost::posix_time; 00019 00020 class Logger { 00021 const static size_t BUFF_SIZE = 4096; 00022 00023 std::ostream* _info_out; 00024 std::ostream* _warn_out; 00025 std::ostream* _severe_out; 00026 mutable boost::mutex _mut; 00027 00028 std::string get_time() const { 00029 return pt::to_simple_string(pt::second_clock::local_time()); 00030 } 00031 00032 public: 00033 Logger() 00034 : _info_out(&std::cout), _warn_out(&std::cerr), _severe_out(&std::cerr) {} 00035 00036 void info_out(std::ostream* out) { 00037 boost::unique_lock<boost::mutex>(_mut); 00038 _info_out = out; 00039 } 00040 00041 void warn_out(std::ostream* out) { 00042 boost::unique_lock<boost::mutex>(_mut); 00043 _warn_out = out; 00044 } 00045 00046 void severe_out(std::ostream* out) { 00047 boost::unique_lock<boost::mutex>(_mut); 00048 _severe_out = out; 00049 } 00050 00051 void info(const char* msg, ...) const { 00052 boost::unique_lock<boost::mutex>(_mut); 00053 char buffer[BUFF_SIZE]; 00054 std::va_list arg; 00055 va_start(arg, msg); 00056 vsnprintf(buffer, BUFF_SIZE, msg, arg); 00057 va_end(arg); 00058 *_info_out << get_time() << " - " << buffer << std::endl; 00059 } 00060 00061 void warn(const char* msg, ...) const { 00062 boost::unique_lock<boost::mutex>(_mut); 00063 char buffer[BUFF_SIZE]; 00064 std::va_list arg; 00065 va_start(arg, msg); 00066 vsnprintf(buffer, BUFF_SIZE, msg, arg); 00067 va_end(arg); 00068 *_warn_out << get_time() << " - WARNING: " << buffer << std::endl; 00069 } 00070 00071 void severe(const char* msg, ...) const { 00072 boost::unique_lock<boost::mutex>(_mut); 00073 char buffer[BUFF_SIZE]; 00074 std::va_list arg; 00075 va_start(arg, msg); 00076 vsnprintf(buffer, BUFF_SIZE, msg, arg); 00077 va_end(arg); 00078 *_severe_out << get_time() << " - SEVERE: " << buffer << std::endl; 00079 exit(1); 00080 } 00081 }; 00082 00083 #endif /* defined(__express__logger__) */