00001 #ifndef toast_log_header_policies_hpp_INCLUDED 00002 #define toast_log_header_policies_hpp_INCLUDED 00003 00004 #include <boost/date_time/posix_time/posix_time.hpp> 00005 #include <toast/log/severity.hpp> 00006 #include <toast/concept_check.hpp> 00007 00018 namespace toast { 00019 namespace log { 00020 00038 template <typename Policy> 00039 struct HeaderPolicyConcept 00040 { 00045 void constraints() { 00046 s = p.header(log::INFO); 00047 } 00048 private: 00049 Policy p; 00050 std::string s; 00051 }; 00052 00056 struct no_header { 00057 static char const* header(log::severity severity) { return ""; } 00058 protected: 00059 ~no_header() {} 00060 }; 00061 00066 struct microseconds 00067 { 00068 static std::string header(log::severity severity) 00069 { 00070 return to_iso_extended_string(boost::posix_time::microsec_clock::local_time() ) + ": "; 00071 } 00072 protected: 00073 ~microseconds() {} 00074 }; 00075 00080 struct severity_header 00081 { 00082 static char const* header(log::severity severity) 00083 { 00084 switch(severity) { 00085 case DEFAULT: return ""; 00086 case FATAL: return "FATAL: "; 00087 case ALERT: return "ALERT: "; 00088 case CRITICAL: return "CRITICAL: "; 00089 case ERROR: return "ERROR: "; 00090 case WARNING: return "WARNING: "; 00091 case NOTICE: return "NOTICE: "; 00092 case INFO: return "INFO: "; 00093 case DEBUG: return "DEBUG: "; 00094 default: return "UNKNOWN: "; 00095 } 00096 } 00097 protected: 00098 ~severity_header() {} 00099 }; 00100 00101 template <typename FirstHeaderPolicy, typename SecondHeaderPolicy> 00102 struct header_policy_aggregator 00103 : public FirstHeaderPolicy, public SecondHeaderPolicy 00104 { 00105 TOAST_CLASS_REQUIRE(FirstHeaderPolicy, toast::log, HeaderPolicyConcept); 00106 TOAST_CLASS_REQUIRE(SecondHeaderPolicy, toast::log, HeaderPolicyConcept); 00107 00108 static std::string header(log::severity severity) 00109 { 00110 std::string ret = FirstHeaderPolicy::header(severity); 00111 ret += SecondHeaderPolicy::header(severity); 00112 return ret; 00113 } 00114 protected: 00115 ~header_policy_aggregator() {} 00116 }; 00117 00120 } 00121 } 00122 00123 #endif // toast_log_header_policies_hpp_INCLUDED