wesnoth-cvs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Wesnoth-cvs-commits] wesnoth/src log.cpp log.hpp


From: Guillaume Melquiond
Subject: [Wesnoth-cvs-commits] wesnoth/src log.cpp log.hpp
Date: Sat, 18 Sep 2004 14:35:37 -0400

CVSROOT:        /cvsroot/wesnoth
Module name:    wesnoth
Branch:         
Changes by:     Guillaume Melquiond <address@hidden>    04/09/18 18:29:04

Modified files:
        src            : log.cpp log.hpp 

Log message:
        New logging system. First step: implementing the architecture. To get 
back the scope logging messages, add this line somewhere: 
'lg::set_log_domain_severity("general", 2);'.

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/log.cpp.diff?tr1=1.12&tr2=1.13&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/wesnoth/wesnoth/src/log.hpp.diff?tr1=1.13&tr2=1.14&r1=text&r2=text

Patches:
Index: wesnoth/src/log.cpp
diff -u wesnoth/src/log.cpp:1.12 wesnoth/src/log.cpp:1.13
--- wesnoth/src/log.cpp:1.12    Sun Aug 15 03:09:03 2004
+++ wesnoth/src/log.cpp Sat Sep 18 18:29:04 2004
@@ -1,4 +1,4 @@
-/* $Id: log.cpp,v 1.12 2004/08/15 03:09:03 Sirp Exp $ */
+/* $Id: log.cpp,v 1.13 2004/09/18 18:29:04 silene Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -11,8 +11,97 @@
    See the COPYING file for more details.
 */
 
+#include "SDL.h"
+
 #include "log.hpp"
 
+#include <algorithm>
+#include <cassert>
+#include <iostream>
 #include <sstream>
+#include <vector>
+
+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); }
+public:
+       null_streambuf() {}
+};
+
+} // anonymous namespace
+
+static std::vector< logd > log_domains;
+static std::ostream null_ostream(new null_streambuf);
+static int indent = 0;
+
+namespace lg {
+
+logger err("error", 0), warn("warning", 1), info("info", 2);
+log_domain general("general");
+
+log_domain::log_domain(char const *name) : domain_(log_domains.size())
+{
+       logd d = { name, 0 };
+       log_domains.push_back(d);
+}
+
+bool set_log_domain_severity(std::string const &name, int severity)
+{
+       std::vector< logd >::iterator
+               it = log_domains.begin(),
+               it_end = log_domains.end();
+       if (name == "all") {
+               for(; it != it_end; ++it)
+                       it->severity_ = severity;
+       } else {
+               for(; it != it_end; ++it)
+                       if (name == it->name_) break;
+               if (it == it_end)
+                       return false;
+               it->severity_ = severity;
+       }
+       return true;
+}
+
+std::ostream &logger::operator()(log_domain const &domain, bool show_names)
+{
+       logd const &d = log_domains[domain.domain_];
+       if (severity_ > d.severity_)
+               return null_ostream;
+       else {
+               if (show_names)
+                       std::cerr << name_ << " " << d.name_ << ": ";
+               return std::cerr;
+       }
+};
+
+scope_logger::scope_logger(log_domain const &domain, const std::string& str)
+       : ticks_(SDL_GetTicks()), str_(str), output_(info(domain, false))
+{
+       do_indent();
+       output_ << "BEGIN: " << str_ << "\n";
+       ++indent;
+}
+
+scope_logger::~scope_logger()
+{
+       const int ticks = SDL_GetTicks() - ticks_;
+       --indent;
+       do_indent();
+       output_ << "END: " << str_ << " (took " << ticks << "ms)\n";
+}
+
+void scope_logger::do_indent()
+{
+       for(int i = 0; i != indent; ++i)
+               output_ << "  ";
+}
 
-int scope_logger::indent = 0;
+} // namespace lg
Index: wesnoth/src/log.hpp
diff -u wesnoth/src/log.hpp:1.13 wesnoth/src/log.hpp:1.14
--- wesnoth/src/log.hpp:1.13    Fri Aug 13 03:11:31 2004
+++ wesnoth/src/log.hpp Sat Sep 18 18:29:04 2004
@@ -1,4 +1,4 @@
-/* $Id: log.hpp,v 1.13 2004/08/13 03:11:31 Sirp Exp $ */
+/* $Id: log.hpp,v 1.14 2004/09/18 18:29:04 silene Exp $ */
 /*
    Copyright (C) 2003 by David White <address@hidden>
    Part of the Battle for Wesnoth Project http://wesnoth.whitevine.net
@@ -13,56 +13,47 @@
 #ifndef LOG_HPP_INCLUDED
 #define LOG_HPP_INCLUDED
 
-#define LOG_DATA
+#include <iosfwd>
+#include <string>
 
-#ifdef LOG_DATA
+namespace lg {
 
-#include <iostream>
-#include <string>
+class logger;
 
-#include "SDL.h"
+class log_domain {
+       int domain_;
+public:
+       log_domain(char const *name);
+       friend class logger;
+};
 
-struct scope_logger
-{
-       scope_logger(const std::string& str) : ticks_(SDL_GetTicks()), 
str_(str) {
-               do_indent();
-               std::cerr << "BEGIN: " << str_ << "\n";
-               do_indent();
-               ++indent;
-       }
-
-       ~scope_logger() {
-               const int ticks = SDL_GetTicks() - ticks_;
-               --indent;
-               do_indent();
-               do_indent();
-               std::cerr << "END: " << str_ << " (took " << ticks << "ms)\n";
-       }
-
-       void do_indent()
-       {
-               for(int i = 0; i != indent; ++i)
-                       std::cerr << "  ";
-       }
+bool set_log_domain_severity(std::string const &name, int severity);
 
-private:
+class logger {
+       char const *name_;
+       int severity_;
+public:
+       logger(char const *name, int severity): name_(name), 
severity_(severity) {}
+       std::ostream &operator()(log_domain const &domain, bool show_names = 
true);
+};
+
+extern logger err, warn, info;
+extern log_domain general;
+
+class scope_logger
+{
        int ticks_;
        std::string str_;
-       static int indent;
+       std::ostream &output_;
+public:
+       scope_logger(log_domain const &domain, std::string const &str);
+       ~scope_logger();
+       void do_indent();
 };
 
-#define log_data0(a) std::cerr << a << "\n";
-#define log_data1(a,b) std::cerr << a << " info: " << b << "\n";
-#define log_data2(a,b,c) std::cerr << a << " info: " << b << ", " << c << "\n";
-
-#define log_scope(a) scope_logger scope_logging_object__(a);
-
-#else
-#define log_data0(a)
-#define log_data1(a,b)
-#define log_data2(a,b,c)
+} // namespace lg
 
-#define log_scope(a)
-#endif
+#define log_scope(a) lg::scope_logger scope_logging_object__(lg::general, a);
+#define log_scope2(a,b) lg::scope_logger scope_logging_object__(lg::a, b);
 
 #endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]