00001 #ifndef toast_log_log_handle_hpp_INCLUDED
00002 #define toast_log_log_handle_hpp_INCLUDED
00003
00004 #include <iostream>
00005 #include <boost/bind.hpp>
00006 #include <boost/function.hpp>
00007 #include <boost/ref.hpp>
00008
00009 #include <toast/log/severity.hpp>
00010
00017 namespace toast {
00018
00034 template <typename CharT>
00035 class basic_log_handle
00036 {
00037 public:
00038 typedef CharT char_type;
00039
00040 template <typename Logger>
00041 basic_log_handle(Logger &logger)
00042 : message_(boost::bind(&Logger::message_hook,
00043 boost::ref(logger), _1, _2)),
00044 forced_message_(boost::bind(&Logger::forced_message_hook,
00045 boost::ref(logger), _1, _2)),
00046 check_(boost::bind(&Logger::check, boost::ref(logger), _1)),
00047 consume_message_(boost::bind(&Logger::consume_message_hook,
00048 boost::ref(logger), _1, _2)),
00049 consume_forced_message_(boost::bind(&Logger::consume_forced_message_hook,
00050 boost::ref(logger), _1, _2))
00051 {}
00052
00053 basic_log_handle() {}
00054
00055 basic_log_handle(basic_log_handle const &other)
00056 : message_(other.message_),
00057 forced_message_(other.forced_message_),
00058 check_(other.check_),
00059 consume_message_(other.consume_message_),
00060 consume_forced_message_(other.consume_forced_message_)
00061 {}
00062
00063 basic_log_handle(basic_log_handle &other)
00064 : message_(other.message_),
00065 forced_message_(other.forced_message_),
00066 check_(other.check_),
00067 consume_message_(other.consume_message_),
00068 consume_forced_message_(other.consume_forced_message_)
00069 {}
00070
00071 basic_log_handle& operator=(basic_log_handle temp)
00072 {
00073 swap(temp);
00074 return *this;
00075 }
00076
00077 void swap(basic_log_handle &other)
00078 {
00079 message_.swap(other.message_);
00080 forced_message_.swap(other.forced_message_);
00081 check_.swap(other.check_);
00082 consume_message_.swap(other.consume_message_);
00083 consume_forced_message_.swap(other.consume_forced_message_);
00084 }
00085
00089 template <typename Traits, typename Alloc>
00090 void message(std::basic_string<char_type, Traits, Alloc> const &m,
00091 log::severity s = log::DEFAULT) const {
00092 message(m.c_str(), s);
00093 }
00097 void message(char_type const *m, log::severity s = log::DEFAULT) const {
00098 if(message_) message_(m, s);
00099 }
00100
00104 char_type* consume_message(char_type *m, log::severity s = log::DEFAULT) const {
00105 if(consume_message_) return consume_message_(m, s);
00106 return m;
00107 }
00108
00112 char_type* consume_forced_message(char_type *m, log::severity s = log::DEFAULT) const {
00113 if(consume_forced_message_) return consume_forced_message_(m, s);
00114 return m;
00115 }
00116
00120 template <typename Traits, typename Alloc>
00121 void forced_message(std::basic_string<char_type, Traits, Alloc> const &m,
00122 log::severity s = log::DEFAULT) const {
00123 forced_message(m.c_str(), s);
00124 }
00128 void forced_message(char_type const *m,
00129 log::severity s = log::DEFAULT) const {
00130 if(forced_message_) forced_message_(m, s);
00131 }
00132
00137 bool check(log::severity s) const {
00138 if(check_) return check_(s);
00139 return false;
00140 }
00141
00142 private:
00143 boost::function<void (char_type const *, log::severity)> message_;
00144 boost::function<void (char_type const *, log::severity)> forced_message_;
00145 boost::function<bool (log::severity)> check_;
00146 boost::function<char_type* (char_type *, log::severity)> consume_message_;
00147 boost::function<char_type* (char_type *, log::severity)> consume_forced_message_;
00148 };
00149
00151 typedef basic_log_handle<char> log_handle;
00152
00155 namespace log {
00156
00157
00158 using ::toast::log_handle;
00159
00160 }
00161 }
00162
00163 #endif // toast_log_log_handle_hpp_INCLUDED