Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef LOG_HPP_INCLUDED
00023 #define LOG_HPP_INCLUDED
00024
00025 #ifndef __func__
00026 #ifdef __FUNCTION__
00027 #define __func__ __FUNCTION__
00028 #endif
00029 #endif
00030
00031 #include <iostream>
00032 #include <sstream>
00033 #include <string>
00034 #include <utility>
00035
00036 namespace lg {
00037
00038
00039
00040
00041
00042
00043
00044 class tredirect_output_setter
00045 {
00046 public:
00047
00048
00049
00050
00051
00052
00053 explicit tredirect_output_setter(std::ostream& stream);
00054
00055 ~tredirect_output_setter();
00056
00057 private:
00058
00059
00060
00061
00062
00063
00064 std::ostream* old_stream_;
00065 };
00066
00067 class logger;
00068
00069 typedef std::pair<const std::string, int> logd;
00070
00071 class log_domain {
00072 logd *domain_;
00073 public:
00074 log_domain(char const *name);
00075 friend class logger;
00076 };
00077
00078 bool set_log_domain_severity(std::string const &name, int severity);
00079 std::string list_logdomains(const std::string& filter);
00080
00081 class logger {
00082 char const *name_;
00083 int severity_;
00084 public:
00085 logger(char const *name, int severity): name_(name), severity_(severity) {}
00086 std::ostream &operator()(log_domain const &domain,
00087 bool show_names = true, bool do_indent = false) const;
00088
00089 bool dont_log(log_domain const &domain) const
00090 {
00091 return severity_ > domain.domain_->second;
00092 }
00093 };
00094
00095 void timestamps(bool);
00096 std::string get_timestamp(const time_t& t, const std::string& format="%Y%m%d %H:%M:%S ");
00097
00098 extern logger err, warn, info, debug;
00099 extern log_domain general;
00100
00101 class scope_logger
00102 {
00103 int ticks_;
00104 std::ostream *output_;
00105 std::string str_;
00106 public:
00107 scope_logger(log_domain const &domain, const char* str) :
00108 ticks_(0),
00109 output_(NULL),
00110 str_()
00111 {
00112 if (!debug.dont_log(domain)) do_log_entry(domain, str);
00113 }
00114 scope_logger(log_domain const &domain, const std::string& str) :
00115 ticks_(0),
00116 output_(NULL),
00117 str_()
00118 {
00119 if (!debug.dont_log(domain)) do_log_entry(domain, str);
00120 }
00121 ~scope_logger()
00122 {
00123 if (output_) do_log_exit();
00124 }
00125 void do_indent() const;
00126 private:
00127 void do_log_entry(log_domain const &domain, const std::string& str);
00128 void do_log_exit();
00129 };
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 extern std::stringstream wml_error;
00141
00142 }
00143
00144 #define log_scope(a) lg::scope_logger scope_logging_object__(lg::general, a);
00145 #define log_scope2(a,b) lg::scope_logger scope_logging_object__(a, b);
00146
00147 #define LOG_STREAM(a, b) if (lg::a.dont_log(b)) ; else lg::a(b)
00148
00149
00150 #define LOG_STREAM_INDENT(a,b) if (lg::a.dont_log(b)) ; else lg::a(b, true, true)
00151
00152 #endif