Author: ilor
Date: Fri Apr  4 20:49:17 2008
New Revision: 25549

URL: http://svn.gna.org/viewcvs/wesnoth?rev=25549&view=rev
Log:
log_scope and logging performance tweaks (more inlining, less overhead when 
logging is off) - patch #1038

Modified:
    trunk/src/log.cpp
    trunk/src/log.hpp

Modified: trunk/src/log.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/log.cpp?rev=25549&r1=25548&r2=25549&view=diff
==============================================================================
--- trunk/src/log.cpp (original)
+++ trunk/src/log.cpp Fri Apr  4 20:49:17 2008
@@ -31,11 +31,6 @@
 
 namespace {
 
-struct logd {
-       char const *name_;
-       int severity_;
-};
-
 class null_streambuf : public std::streambuf
 {
        virtual int overflow(int c) { return std::char_traits< char 
>::not_eof(c); }
@@ -45,13 +40,13 @@
 
 } // end anonymous namespace
 
-static std::vector< logd > log_domains;
 static std::ostream null_ostream(new null_streambuf);
 static int indent = 0;
 static bool timestamp = true;
 
 namespace lg {
 
+std::vector< lg::logd > log_domains;
 void timestamps(bool t) { timestamp = t; }
 
 logger err("error", 0), warn("warning", 1), info("info", 2), debug("debug", 3);
@@ -100,12 +95,6 @@
        return domainlist;
 }
 
-bool logger::dont_log(log_domain const &domain) const
-{
-       logd const &d = log_domains[domain.domain_];
-       return severity_ > d.severity_;
-}
-
 std::string get_timestamp(const time_t& t, const std::string& format) {
        char buf[100];
        strftime(buf, 100, format.c_str(), localtime(&t));
@@ -126,27 +115,29 @@
        }
 }
 
-scope_logger::scope_logger(log_domain const &domain, const std::string& str)
-       : ticks_(SDL_GetTicks()), str_(str), output_(debug(domain, false))
+void scope_logger::do_log_entry(log_domain const &domain, const char* str)
 {
+       output_ = &debug(domain, false);
+       str_ = str;
+       ticks_ = SDL_GetTicks();
        do_indent();
-       output_ << "BEGIN: " << str_ << "\n";
+       (*output_) << "BEGIN: " << str_ << "\n";
        ++indent;
-}
+}      
 
-scope_logger::~scope_logger()
+void scope_logger::do_log_exit()
 {
        const int ticks = SDL_GetTicks() - ticks_;
        --indent;
        do_indent();
-       if (timestamp) output_ << get_timestamp(time(NULL));
-       output_ << "END: " << str_ << " (took " << ticks << "ms)\n";
-}
+       if (timestamp) (*output_) << get_timestamp(time(NULL));
+       (*output_) << "END: " << str_ << " (took " << ticks << "ms)\n";
+}      
 
 void scope_logger::do_indent() const
 {
        for(int i = 0; i != indent; ++i)
-               output_ << "  ";
+               (*output_) << "  ";
 }
 
 std::stringstream wml_error;

Modified: trunk/src/log.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/log.hpp?rev=25549&r1=25548&r2=25549&view=diff
==============================================================================
--- trunk/src/log.hpp (original)
+++ trunk/src/log.hpp Fri Apr  4 20:49:17 2008
@@ -21,10 +21,16 @@
 
 #include <iosfwd>
 #include <string>
+#include <vector>
 
 namespace lg {
 
 class logger;
+
+struct logd {
+       char const *name_;
+       int severity_;
+};
 
 class log_domain {
        int domain_;
@@ -32,6 +38,9 @@
        log_domain(char const *name);
        friend class logger;
 };
+
+//exposed to make inlining possible
+extern std::vector<logd> log_domains;
 
 bool set_log_domain_severity(std::string const &name, int severity);
 std::string list_logdomains();
@@ -42,7 +51,11 @@
 public:
        logger(char const *name, int severity): name_(name), 
severity_(severity) {}
        std::ostream &operator()(log_domain const &domain, bool show_names = 
true) const;
-       bool dont_log(log_domain const &domain) const;
+       bool dont_log(log_domain const &domain) const
+       {
+               logd const &d = log_domains[domain.domain_];
+               return severity_ > d.severity_;
+       }       
 };
 
 void timestamps(bool);
@@ -55,12 +68,27 @@
 class scope_logger
 {
        int ticks_;
-       std::string str_;
-       std::ostream &output_;
+       std::ostream *output_;
+       const char* str_;
 public:
-       scope_logger(log_domain const &domain, std::string const &str);
-       ~scope_logger();
+       scope_logger(log_domain const &domain, const char* str)
+       : output_(0)
+       {
+               if (!debug.dont_log(domain)) do_log_entry(domain, str);
+       }
+       scope_logger(log_domain const &domain, const std::string& str)
+       : output_(0)
+       {
+               if (!debug.dont_log(domain)) do_log_entry(domain, str.c_str());
+       }
+       ~scope_logger()
+       {
+               if (output_) do_log_exit();
+       }
        void do_indent() const;
+private:
+       void do_log_entry(log_domain const &domain, const char* str);
+       void do_log_exit();
 };
 
 /**


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to