00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LOG_HPP_INCLUDED
00020 #define LOG_HPP_INCLUDED
00021
00022 #include <iosfwd>
00023 #include <iostream>
00024 #include <string>
00025 #include <vector>
00026
00027 namespace lg {
00028
00029 class logger;
00030
00031 struct logd {
00032 char const *name_;
00033 int severity_;
00034 };
00035
00036 class log_domain {
00037 int domain_;
00038 public:
00039 log_domain(char const *name);
00040 friend class logger;
00041 };
00042
00043
00044 extern std::vector<logd> log_domains;
00045
00046 bool set_log_domain_severity(std::string const &name, int severity);
00047 std::string list_logdomains();
00048
00049 class logger {
00050 char const *name_;
00051 int severity_;
00052 public:
00053 logger(char const *name, int severity): name_(name), severity_(severity) {}
00054 std::ostream &operator()(log_domain const &domain,
00055 bool show_names = true, bool do_indent = false) const;
00056
00057 bool dont_log(log_domain const &domain) const
00058 {
00059 logd const &d = log_domains[domain.domain_];
00060 return severity_ > d.severity_;
00061 }
00062 };
00063
00064 void timestamps(bool);
00065 std::string get_timestamp(const time_t& t, const std::string& format="%Y%m%d %T ");
00066
00067 extern logger err, warn, info, debug;
00068 extern log_domain general, ai, config, display, engine, network, mp_server,
00069 filesystem, audio, notifs, replay, help, gui, gui_parse, gui_draw, gui_event;
00070
00071 class scope_logger
00072 {
00073 int ticks_;
00074 std::ostream *output_;
00075 const char* str_;
00076 public:
00077 scope_logger(log_domain const &domain, const char* str)
00078 : output_(0)
00079 {
00080 if (!debug.dont_log(domain)) do_log_entry(domain, str);
00081 }
00082 scope_logger(log_domain const &domain, const std::string& str)
00083 : output_(0)
00084 {
00085 if (!debug.dont_log(domain)) do_log_entry(domain, str.c_str());
00086 }
00087 ~scope_logger()
00088 {
00089 if (output_) do_log_exit();
00090 }
00091 void do_indent() const;
00092 private:
00093 void do_log_entry(log_domain const &domain, const char* str);
00094 void do_log_exit();
00095 };
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 extern std::stringstream wml_error;
00107
00108 }
00109
00110 #define log_scope(a) lg::scope_logger scope_logging_object__(lg::general, a);
00111 #define log_scope2(a,b) lg::scope_logger scope_logging_object__(lg::a, b);
00112
00113 #define LOG_STREAM(a, b) if (lg::a.dont_log(lg::b)) ; else lg::a(lg::b)
00114
00115
00116 #define LOG_STREAM_INDENT(a,b) if (lg::a.dont_log(lg::b)) ; else lg::a(lg::b, true, true)
00117
00118 #endif