libt2n: (gerd) fix & improve logging
authorGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 19 Oct 2006 10:26:05 +0000 (10:26 +0000)
committerGerd v. Egidy <gerd.von.egidy@intra2net.com>
Thu, 19 Oct 2006 10:26:05 +0000 (10:26 +0000)
src/client.cpp
src/command_client.cpp
src/command_server.cpp
src/server.cpp
src/socket_handler.cpp
src/types.hxx
test/simplecmd.cpp

index 9c6e05b..2ce213b 100644 (file)
@@ -33,8 +33,10 @@ client_connection::client_connection()
 /// get pointer to logging stream, returns NULL if no logging needed
 std::ostream* client_connection::get_logstream(log_level_values level)
 {
-    if (logstream && level >= log_level)
+    if (logstream && log_level >= level)
         return logstream;
+    else
+        return NULL;
 }
 
 /// activate logging to the given stream. everything above the given level is logged.
index 0d387b9..a603634 100644 (file)
@@ -25,7 +25,6 @@
 #include <boost/archive/xml_oarchive.hpp>
 #include <boost/archive/xml_iarchive.hpp>
 #include <boost/serialization/serialization.hpp>
-#include <boost/serialization/export.hpp>
 
 #include "command_client.hxx"
 
@@ -43,6 +42,14 @@ void command_client::send_command(command* cmd, result_container &res)
     // TODO: exceptions
     oa << cc;
 
+    std::ostream* ostr;
+    if ((ostr=c.get_logstream(fulldebug))!=NULL)
+    {
+        (*ostr) << "sending command, decoded data: " << std::endl;
+        boost::archive::xml_oarchive xo(*ostr);
+        xo << BOOST_SERIALIZATION_NVP(cc);
+    }
+
     c.write(ofs.str());
 
     // TODO: fix timeout
@@ -55,6 +62,13 @@ void command_client::send_command(command* cmd, result_container &res)
 
     // TODO: exceptions
     ia >> res;
+
+    if ((ostr=c.get_logstream(fulldebug))!=NULL)
+    {
+        (*ostr) << "received result, decoded data: " << std::endl;
+        boost::archive::xml_oarchive xo(*ostr);
+        xo << BOOST_SERIALIZATION_NVP(res);
+    }
 }
 
 }
index d3721a8..3bfc0e3 100644 (file)
@@ -49,6 +49,14 @@ void command_server::handle_packet(const std::string& packet, server_connection*
     // TODO: catch
     ia >> ccont;
 
+    std::ostream* ostr;
+    if ((ostr=s.get_logstream(fulldebug))!=NULL)
+    {
+        (*ostr) << "decoded packet data: " << std::endl;
+        boost::archive::xml_oarchive xo(*ostr);
+        xo << BOOST_SERIALIZATION_NVP(ccont);
+    }
+
     // TODO: cast to command subclass (template)
     command *cmd=ccont.get_command();
 
@@ -74,6 +82,13 @@ void command_server::handle_packet(const std::string& packet, server_connection*
     // TODO: catch
     oa << res;
 
+    if ((ostr=s.get_logstream(fulldebug))!=NULL)
+    {
+        (*ostr) << "returning result, decoded data: " << std::endl;
+        boost::archive::xml_oarchive xo(*ostr);
+        xo << BOOST_SERIALIZATION_NVP(res);
+    }
+
     conn->write(ofs.str());
 }
 
index 14f4e2a..9187974 100644 (file)
@@ -39,7 +39,14 @@ server_connection::server_connection(int _timeout)
 std::ostream* server_connection::get_logstream(log_level_values level)
 {
     if (my_server != NULL)
-        return my_server->get_logstream(level);
+    {
+        std::ostream* ostr=my_server->get_logstream(level);
+        if (ostr != NULL)
+            (*ostr) << "connection " << get_id() << ": ";
+        return ostr;
+    }
+    else
+        return NULL;
 }
 
 /// check if timeout is expired, close connection if so
@@ -158,7 +165,9 @@ bool server::get_packet(std::string& data, unsigned int& conn_id)
 /// get pointer to logging stream, returns NULL if no logging needed
 std::ostream* server::get_logstream(log_level_values level)
 {
-    if (logstream && level >= log_level)
+    if (logstream && log_level >= level)
         return logstream;
+    else
+        return NULL;
 }
 };
index 582cb0c..11acc81 100644 (file)
@@ -185,7 +185,7 @@ bool socket_handler::fill_buffer(std::string& buffer)
     if (nbytes > 0)
     {
         buffer.assign(socket_buffer,nbytes);
-        LOGSTREAM(debug,nbytes << " read");
+        LOGSTREAM(debug,nbytes << " bytes read");
     }
 
     // more data waiting -> recurse
index 3053dc5..ef42a47 100644 (file)
@@ -23,7 +23,7 @@ namespace libt2n
 {
 
 /// possible levels for logging
-enum log_level_values { none=0, error=1, debug=2 };
+enum log_level_values { none=0, error=1, debug=2, fulldebug=3 };
 
 /// possible types of a socket (tcp and unix)
 enum socket_type_value { tcp_s, unix_s };
index 9c1472b..462a03f 100644 (file)
 #include <cppunit/ui/text/TestRunner.h>
 #include <cppunit/extensions/HelperMacros.h>
 
-
 #include <boost/archive/binary_oarchive.hpp>
 #include <boost/archive/binary_iarchive.hpp>
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
 #include <boost/serialization/serialization.hpp>
 
 #include <container.hxx>
@@ -31,7 +32,6 @@
 #include <command_server.hxx>
 
 using namespace std;
-using namespace libt2n;
 using namespace CppUnit;
 
 string testfunc(const string& str)
@@ -41,7 +41,7 @@ string testfunc(const string& str)
     return ret;
 }
 
-class testfunc_res : public result
+class testfunc_res : public libt2n::result
 {
     private:
         string res;
@@ -50,7 +50,7 @@ class testfunc_res : public result
         template<class Archive>
         void serialize(Archive & ar, const unsigned int version)
         {
-            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(result);
+            ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
             ar & BOOST_SERIALIZATION_NVP(res);
         }
 
@@ -92,7 +92,7 @@ class testfunc_cmd : public libt2n::command
             param=str;
         }
 
-        result* operator()()
+        libt2n::result* operator()()
         {
             return new testfunc_res(testfunc(param));
         }
@@ -103,6 +103,8 @@ class testfunc_cmd : public libt2n::command
 BOOST_CLASS_EXPORT(testfunc_cmd)
 BOOST_CLASS_EXPORT(testfunc_res)
 
+using namespace libt2n;
+
 class test_simplecmd : public TestFixture
 {
     CPPUNIT_TEST_SUITE(test_simplecmd);