Author: mordante
Date: Wed Mar  2 22:40:59 2011
New Revision: 48725

URL: http://svn.gna.org/viewcvs/wesnoth?rev=48725&view=rev
Log:
Add a helper class to redirect the logger output.

This is added for some unit tests that will check the output of the logs
for validation. (The unit tests will be committed once their classes are
finished.)

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=48725&r1=48724&r2=48725&view=diff
==============================================================================
--- trunk/src/log.cpp (original)
+++ trunk/src/log.cpp Wed Mar  2 22:40:59 2011
@@ -46,7 +46,28 @@
 static int indent = 0;
 static bool timestamp = true;
 
+static std::ostream *output_stream = NULL;
+
+static std::ostream& output()
+{
+       if(output_stream) {
+               return *output_stream;
+       }
+       return std::cerr;
+}
+
 namespace lg {
+
+tredirect_output_setter::tredirect_output_setter(std::ostream& stream)
+       : old_stream_(output_stream)
+{
+       output_stream = &stream;
+}
+
+tredirect_output_setter::~tredirect_output_setter()
+{
+       output_stream = old_stream_;
+}
 
 typedef std::map<std::string, int> domain_map;
 static domain_map *domains;
@@ -108,17 +129,18 @@
        if (severity_ > domain.domain_->second)
                return null_ostream;
        else {
+               std::ostream& stream = output();
                if(do_indent) {
                        for(int i = 0; i != indent; ++i)
-                               std::cerr << "  ";
+                               stream << "  ";
                        }
                if (timestamp) {
-                       std::cerr << get_timestamp(time(NULL));
+                       stream << get_timestamp(time(NULL));
                }
                if (show_names) {
-                       std::cerr << name_ << ' ' << domain.domain_->first << 
": ";
+                       stream << name_ << ' ' << domain.domain_->first << ": ";
                }
-               return std::cerr;
+               return stream;
        }
 }
 

Modified: trunk/src/log.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/log.hpp?rev=48725&r1=48724&r2=48725&view=diff
==============================================================================
--- trunk/src/log.hpp (original)
+++ trunk/src/log.hpp Wed Mar  2 22:40:59 2011
@@ -34,6 +34,35 @@
 #include <utility>
 
 namespace lg {
+
+/**
+ * Helper class to redirect the output of the logger in a certain scope.
+ *
+ * The main usage of the redirection is for the unit tests to validate the
+ * ourput on the logger with the expected output.
+ */
+class tredirect_output_setter
+{
+public:
+
+       /**
+        * Constructor.
+        *
+        * @param stream              The stream to direct the output to.
+        */
+       explicit tredirect_output_setter(std::ostream& stream);
+
+       ~tredirect_output_setter();
+
+private:
+
+       /**
+        * The previously set redirection.
+        *
+        * This value is stored here to be restored in this destructor.
+        */
+       std::ostream* old_stream_;
+};
 
 class logger;
 


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

Reply via email to