diff --git a/doc/additionalDoc/debug-logger.h b/doc/additionalDoc/debug-logger.h index 53514b582eb914310ff21e385c4f867239fba328..dce8cde54c49ba4171fc25a0e6c6e93498d38790 100644 --- a/doc/additionalDoc/debug-logger.h +++ b/doc/additionalDoc/debug-logger.h @@ -35,11 +35,11 @@ Inside the constructor of the entity: \endcode The first line sets the frequency at which the logger will be updated.<br> -The second line specifies at which frequency the message should be +The second line specifies at which frequency the STREAM messages should be printed.<br> The third line specifies the level of message to accept.<br> The fourth line returns the level of verbosity. -In this case, all messages are accepted. <br> +In this case, all messages are accepted and the STREAM message are displayed on the output streams once on five. <br> The full list of options are: <ul> @@ -54,15 +54,19 @@ The full list of options are: Here is some example on how to display or record some information. \code - sendMsg("This is a message of level MSG_TYPE_DEBUG",MSG_TYPE_DEBUG); - sendMsg("This is a message of level MSG_TYPE_INFO",MSG_TYPE_INFO); - sendMsg("This is a message of level MSG_TYPE_WARNING",MSG_TYPE_WARNING); - sendMsg("This is a message of level MSG_TYPE_ERROR",MSG_TYPE_ERROR); - sendMsg("This is a message of level MSG_TYPE_DEBUG_STREAM",MSG_TYPE_DEBUG_STREAM); - sendMsg("This is a message of level MSG_TYPE_INFO_STREAM",MSG_TYPE_INFO_STREAM); - sendMsg("This is a message of level MSG_TYPE_WARNING_STREAM",MSG_TYPE_WARNING_STREAM); - sendMsg("This is a message of level MSG_TYPE_ERROR_STREAM",MSG_TYPE_ERROR_STREAM); + sendMsg("This is a message of level MSG_TYPE_DEBUG",MSG_TYPE_DEBUG, __FILE__,__LINE__); + sendMsg("This is a message of level MSG_TYPE_INFO",MSG_TYPE_INFO, __FILE__,__LINE__); + sendMsg("This is a message of level MSG_TYPE_WARNING",MSG_TYPE_WARNING, __FILE__,__LINE__); + sendMsg("This is a message of level MSG_TYPE_ERROR",MSG_TYPE_ERROR, __FILE__,__LINE__); + sendMsg("This is a message of level MSG_TYPE_DEBUG_STREAM",MSG_TYPE_DEBUG_STREAM, __FILE__,__LINE__); + sendMsg("This is a message of level MSG_TYPE_INFO_STREAM",MSG_TYPE_INFO_STREAM, __FILE__,__LINE__); + sendMsg("This is a message of level MSG_TYPE_WARNING_STREAM",MSG_TYPE_WARNING_STREAM, __FILE__,__LINE__); + sendMsg("This is a message of level MSG_TYPE_ERROR_STREAM",MSG_TYPE_ERROR_STREAM, __FILE__,__LINE__); logger_.countdown(); \endcode + +Specifying the file with __FILE__ and the line inside the file by __LINE__ are necessary for the +STREAM messages. Indeed they are indexed using the two values. As the default value are "" and 0 +the counting will be confused. */ diff --git a/include/dynamic-graph/entity.h b/include/dynamic-graph/entity.h index 5c57b5f37cf7a8e8f7530ca960acdc6e687d48fa..1db47a3796efa14964594e79dab6bf968d5bc206 100644 --- a/include/dynamic-graph/entity.h +++ b/include/dynamic-graph/entity.h @@ -112,7 +112,24 @@ namespace dynamicgraph /// \brief Get the logger's verbosity level. LoggerVerbosity getLoggerVerbosityLevel() - { return logger_.getVerbosity(); }; + { return logger_.getVerbosity(); } + + /// \brief Set the time sample. + bool setTimeSample(double t) + { return logger_.setTimeSample(t); } + + /// \brief Get the time sample. + double getTimeSample() + { return logger_.getTimeSample();} + + /// \brief Set the period of the stream period + bool setStreamPrintPeriod(double t) + { return logger_.setStreamPrintPeriod(t); } + + /// \brief Get the period of the stream period + double getStreamPrintPeriod() + { return logger_.getStreamPrintPeriod();} + protected: void addCommand(const std::string& name,command::Command* command); diff --git a/include/dynamic-graph/logger.h b/include/dynamic-graph/logger.h index 46a58ca3380ba5234490c3270a092c849ee14887..a88e9501c3d85f1816d4517031194a128623cfbf 100644 --- a/include/dynamic-graph/logger.h +++ b/include/dynamic-graph/logger.h @@ -178,9 +178,16 @@ namespace dynamicgraph { * is going to be called. */ bool setTimeSample(double t); + /** Get the sampling time at which the method countdown() + * is going to be called. */ + double getTimeSample(); + /** Set the time period for printing of streaming messages. */ bool setStreamPrintPeriod(double s); + /** Get the time period for printing of streaming messages. */ + double getStreamPrintPeriod(); + /** Set the verbosity level of the logger. */ void setVerbosity(LoggerVerbosity lv); diff --git a/include/dynamic-graph/signal-ptr.h b/include/dynamic-graph/signal-ptr.h index 4acdffed1b433d0c2ec5e46c0a31ed20def7ab5f..29969ce6fc5ebc15ed2830764b64b3bd737c0b0e 100644 --- a/include/dynamic-graph/signal-ptr.h +++ b/include/dynamic-graph/signal-ptr.h @@ -129,8 +129,9 @@ namespace dynamicgraph std::string next2="" ) const; protected: // Interdiction of the rest of the heritage - + using Signal<T,Time>::addDependency; virtual void addDependency () {} + using Signal<T,Time>::removeDependency; virtual void removeDependency () {} virtual void clearDependencies () {} diff --git a/src/debug/logger.cpp b/src/debug/logger.cpp index 7aa01ccf7e0e9872013cb6425be0527bf7c95b0a..b4c2fc3bad2ff3ea4d6db0e255b9e142fcabd61c 100644 --- a/src/debug/logger.cpp +++ b/src/debug/logger.cpp @@ -62,7 +62,7 @@ namespace dynamicgraph return; // if print is allowed by current verbosity level - if(isStreamMsg(type)) + if( isStreamMsg(type)) { // check whether counter already exists string id = file+toString(line); @@ -78,7 +78,10 @@ namespace dynamicgraph if(it->second>0.0) { it->second -= m_timeSample; - return; + if (it->second<=0.0) + it->second = m_streamPrintPeriod; + else + return; } else // otherwise reset counter and print it->second = m_streamPrintPeriod; @@ -101,4 +104,14 @@ namespace dynamicgraph m_streamPrintPeriod = s; return true; } + + double Logger::getTimeSample() + { + return m_timeSample; + } + + double Logger::getStreamPrintPeriod() + { + return m_streamPrintPeriod; + } } // namespace dynamicgraph diff --git a/src/debug/real-time-logger.cpp b/src/debug/real-time-logger.cpp index 340738054f903b13fb8104b6569ed8b6c406d35c..c78348b31e87d02664b07d3e7683be70fd1be823 100644 --- a/src/debug/real-time-logger.cpp +++ b/src/debug/real-time-logger.cpp @@ -87,22 +87,22 @@ namespace dynamicgraph int threadPolicy; struct sched_param threadParam; if (pthread_getschedparam (pthread_self(), &threadPolicy, &threadParam) == 0) - { - threadPolicy = SCHED_OTHER; - threadParam.sched_priority -= 5; - if (threadParam.sched_priority < sched_get_priority_min (threadPolicy)) - threadParam.sched_priority = sched_get_priority_min (threadPolicy); + { + threadPolicy = SCHED_OTHER; + threadParam.sched_priority -= 5; + if (threadParam.sched_priority < sched_get_priority_min (threadPolicy)) + threadParam.sched_priority = sched_get_priority_min (threadPolicy); - pthread_setschedparam (pthread_self(), threadPolicy, &threadParam); - } + pthread_setschedparam (pthread_self(), threadPolicy, &threadParam); + } while (!requestShutdown_ || !logger->empty()) - { - // If the logger did not write anything, it means the buffer is empty. - // Do a pause - if (!logger->spinOnce()) - boost::this_thread::sleep(boost::posix_time::milliseconds(100)); - } + { + // If the logger did not write anything, it means the buffer is empty. + // Do a pause + if (!logger->spinOnce()) + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + } } }; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 75081de942c483a26ad9879560ab969eedabbefd..a7a181548fe485e8e5221c107a2e35345e46279e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -61,3 +61,4 @@ DYNAMIC_GRAPH_TEST(debug-tracer) TARGET_LINK_LIBRARIES(debug-tracer tracer) DYNAMIC_GRAPH_TEST(debug-logger) DYNAMIC_GRAPH_TEST(debug-logger-winit) +DYNAMIC_GRAPH_TEST(command-test) diff --git a/tests/command-test.cpp b/tests/command-test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c04780cad31b09ecd8dce2966b1fe9c6c59015b --- /dev/null +++ b/tests/command-test.cpp @@ -0,0 +1,166 @@ +/* Copyright 2019, LAAS-CNRS + * + * Olivier Stasse + * + * See LICENSE file + * + */ +#include <sstream> +#include <iostream> +#include <dynamic-graph/entity.h> +#include <dynamic-graph/exception-factory.h> +#include "dynamic-graph/factory.h" +#include "dynamic-graph/pool.h" +#include "dynamic-graph/command-bind.h" + +#define ENABLE_RT_LOG +#include <dynamic-graph/real-time-logger.h> +#include <dynamic-graph/logger.h> + +#define BOOST_TEST_MODULE debug-logger + +#include <boost/test/unit_test.hpp> +#include <boost/test/output_test_stream.hpp> + +using boost::test_tools::output_test_stream; + +using namespace dynamicgraph::command; + +namespace dynamicgraph +{ + class CustomEntity : public Entity + { + public: + static const std::string CLASS_NAME; + bool test_zero_arg_; + bool test_one_arg_; + bool test_two_args_; + bool test_three_args_; + bool test_four_args_; + + virtual const std::string& getClassName () const + { + return CLASS_NAME; + } + CustomEntity (const std::string n) + : Entity (n) + { + test_zero_arg_ = false; + test_one_arg_ = false; + test_two_args_ = false; + test_three_args_ = false; + test_four_args_ = false; + + addCommand("0_arg", + makeCommandVoid0(*this, &CustomEntity::zero_arg, + docCommandVoid0("zero arg"))); + + addCommand("1_arg", + makeCommandVoid1(*this, &CustomEntity::one_arg, + docCommandVoid1("one arg", + "int"))); + + addCommand("2_args", + makeCommandVoid2(*this, &CustomEntity::two_args, + docCommandVoid2("two args", + "int", + "int"))); + + addCommand("3_args", + makeCommandVoid3(*this, &CustomEntity::three_args, + docCommandVoid3("three args", + "int","int","int"))); + + addCommand("4_args", + makeCommandVoid4(*this, &CustomEntity::four_args, + docCommandVoid4("four args", + "int", + "int", + "int", + "int"))); + + } + + ~CustomEntity() + { + } + + void zero_arg() + { + test_zero_arg_ = true; + } + + void one_arg(const int & ) + { + test_one_arg_ = true; + } + + void two_args(const int & , + const int & ) + { + test_two_args_ = true; + } + + void three_args(const int & , + const int & , + const int & ) + { + test_three_args_ = true; + } + + void four_args(const int & , + const int & , + const int & , + const int & ) + { + test_four_args_ = true; + } + + }; + DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN (CustomEntity,"CustomEntity"); +} + + +BOOST_AUTO_TEST_CASE(command_test) +{ + dynamicgraph::CustomEntity& entity = + *(dynamic_cast<dynamicgraph::CustomEntity *> + (dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", + "my-entity"))); + + std::map<const std::string,Command *> aCommandMap = + entity.getNewStyleCommandMap(); + + std::map<const std::string,Command *>::iterator it_map; + + it_map = aCommandMap.find("0_arg"); + if (it_map==aCommandMap.end()) + BOOST_CHECK(false); + it_map->second->execute(); + BOOST_CHECK(entity.test_zero_arg_); + + int first_arg=1; + Value aValue(first_arg); + std::vector<std::string> vec_fname(4); + vec_fname[0]="1_arg"; + vec_fname[1]="2_args"; + vec_fname[2]="3_args"; + vec_fname[3]="4_args"; + std::vector<Value> values; + + for (unsigned int i=0;i<4;i++) + { + it_map = aCommandMap.find(vec_fname[i]); + if (it_map==aCommandMap.end()) + BOOST_CHECK(false); + values.push_back(aValue); + it_map->second->setParameterValues(values); + it_map->second->execute(); + } + + BOOST_CHECK(entity.test_one_arg_); + BOOST_CHECK(entity.test_two_args_); + BOOST_CHECK(entity.test_three_args_); + BOOST_CHECK(entity.test_four_args_); + +} diff --git a/tests/debug-logger-winit.cpp b/tests/debug-logger-winit.cpp index da951130b554dc4414123c233e9b1740a04562d2..d043b5396972fe10821726194280d30130d21913 100644 --- a/tests/debug-logger-winit.cpp +++ b/tests/debug-logger-winit.cpp @@ -7,20 +7,25 @@ */ #include <sstream> #include <iostream> + +#define ENABLE_RT_LOG #include <dynamic-graph/entity.h> #include <dynamic-graph/exception-factory.h> #include "dynamic-graph/factory.h" #include "dynamic-graph/pool.h" -#define ENABLE_RT_LOG #include <dynamic-graph/real-time-logger.h> #include <dynamic-graph/logger.h> + #define BOOST_TEST_MODULE debug-logger #include <boost/test/unit_test.hpp> #include <boost/test/output_test_stream.hpp> +#include <boost/thread/thread.hpp> +#include <boost/date_time/posix_time/posix_time.hpp> + using boost::test_tools::output_test_stream; @@ -78,7 +83,7 @@ BOOST_AUTO_TEST_CASE(debug_logger_wrong_initialization) dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity-2"))); - for(unsigned int i=0;i<10000;i++) + for(unsigned int i=0;i<1000;i++) { entity.testDebugTrace(); } diff --git a/tests/debug-logger.cpp b/tests/debug-logger.cpp index e36483f42d4002eaa6a5e863018614d57773740a..7499e0b100922d97b517b691f294cab3a16857c7 100644 --- a/tests/debug-logger.cpp +++ b/tests/debug-logger.cpp @@ -78,6 +78,11 @@ BOOST_AUTO_TEST_CASE(debug_logger) dynamicgraph::FactoryStorage::getInstance()->newEntity("CustomEntity", "my-entity"))); + entity.setTimeSample(0.002); + BOOST_CHECK_EQUAL(entity.getTimeSample(),0.002); + entity.setStreamPrintPeriod(0.004); + BOOST_CHECK_EQUAL(entity.getStreamPrintPeriod(),0.004); + for(unsigned int i=0;i<10000;i++) { entity.testDebugTrace();