From: Gerd v. Egidy Date: Thu, 19 Oct 2006 10:26:05 +0000 (+0000) Subject: libt2n: (gerd) fix & improve logging X-Git-Tag: v0.2~140 X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=commitdiff_plain;h=d535333ffe637c9e547e68b792f334c229641520;hp=a7170401dd90dc79cc7d7a808cfe18a06c7e983b libt2n: (gerd) fix & improve logging --- diff --git a/src/client.cpp b/src/client.cpp index 9c6e05b..2ce213b 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -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. diff --git a/src/command_client.cpp b/src/command_client.cpp index 0d387b9..a603634 100644 --- a/src/command_client.cpp +++ b/src/command_client.cpp @@ -25,7 +25,6 @@ #include #include #include -#include #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); + } } } diff --git a/src/command_server.cpp b/src/command_server.cpp index d3721a8..3bfc0e3 100644 --- a/src/command_server.cpp +++ b/src/command_server.cpp @@ -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()); } diff --git a/src/server.cpp b/src/server.cpp index 14f4e2a..9187974 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -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; } }; diff --git a/src/socket_handler.cpp b/src/socket_handler.cpp index 582cb0c..11acc81 100644 --- a/src/socket_handler.cpp +++ b/src/socket_handler.cpp @@ -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 diff --git a/src/types.hxx b/src/types.hxx index 3053dc5..ef42a47 100644 --- a/src/types.hxx +++ b/src/types.hxx @@ -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 }; diff --git a/test/simplecmd.cpp b/test/simplecmd.cpp index 9c1472b..462a03f 100644 --- a/test/simplecmd.cpp +++ b/test/simplecmd.cpp @@ -19,9 +19,10 @@ #include #include - #include #include +#include +#include #include #include @@ -31,7 +32,6 @@ #include 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 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);