00001 #ifndef toast_log_stream_policies_hpp_INCLUDED 00002 #define toast_log_stream_policies_hpp_INCLUDED 00003 00004 namespace toast { 00005 namespace log { 00006 00025 template <typename Policy> 00026 struct StreamPolicyConcept 00027 { 00032 void constraints() { 00033 std::basic_string<typename Policy::char_type> adjective; 00034 p.stream() << c << adjective << c << std::endl; 00035 p.stream().good(); 00036 p.stream().eof(); 00037 p.stream().fail(); 00038 p.stream().bad(); 00039 p.stream().rdstate(); 00040 p.stream().clear(); 00041 } 00042 private: 00043 typename Policy::char_type c[10]; 00044 Policy p; 00045 }; 00046 00047 template <typename CharT, typename Traits = std::char_traits<CharT> > 00048 struct basic_any_stream 00049 { 00050 typedef CharT char_type; 00051 typedef std::basic_ostream<CharT, Traits> stream_type; 00052 basic_any_stream() : os_(0) {} 00053 stream_type& stream() 00054 { 00055 if(os_) return *os_; 00056 00057 static bool do_once = true; 00058 if(do_once) { 00059 do_once = false; 00060 std::cerr << "any_stream::set_stream has not been called ignoring all" 00061 << "output\n"; 00062 } 00063 return ignore(); 00064 } 00065 void set_stream(stream_type *os) { os_ = os; } 00066 protected: 00067 ~basic_any_stream() {} 00068 private: 00069 stream_type *os_; 00070 stream_type & ignore() 00071 { 00072 static std::basic_ofstream<CharT, Traits> ignore_; 00073 return ignore_; 00074 } 00075 }; 00076 00077 typedef basic_any_stream<char> any_stream; 00078 00079 struct cout_stream 00080 { 00081 typedef char char_type; 00082 std::ostream& stream() { return std::cout; } 00083 protected: 00084 ~cout_stream() {} 00085 }; 00086 00087 struct cerr_stream 00088 { 00089 typedef char char_type; 00090 std::ostream& stream() { return std::cerr; } 00091 protected: 00092 ~cerr_stream() {} 00093 }; 00094 00095 struct clog_stream 00096 { 00097 typedef char char_type; 00098 std::ostream& stream() { return std::clog; } 00099 protected: 00100 ~clog_stream() {} 00101 }; 00102 00103 template <typename CharT, typename Traits = std::char_traits<CharT> > 00104 struct basic_file_stream 00105 { 00106 typedef CharT char_type; 00107 typedef std::basic_ostream<CharT, Traits> stream_type; 00108 stream_type& stream() 00109 { 00110 return os_; 00111 } 00112 void open(std::string const &file) 00113 { 00114 open(file.c_str()); 00115 } 00116 void open(char const *file) 00117 { 00118 os_.open(file, std::ios::out|std::ios::app); 00119 } 00120 void close() { os_.close(); } 00121 protected: 00122 ~basic_file_stream() { close(); } 00123 private: 00124 std::basic_ofstream<CharT, Traits> os_; 00125 }; 00126 00127 typedef basic_file_stream<char> file_stream; 00128 00131 } 00132 } 00133 00134 #endif // toast_log_stream_policies_hpp_INCLUDED