diff --git a/modules/base/pymod/export_logger.cc b/modules/base/pymod/export_logger.cc index c76589d60cd4de40d41405342ff15f46b9cc42ed..f23e0871434a4e3dafd7baa55132f43d34fdc17d 100644 --- a/modules/base/pymod/export_logger.cc +++ b/modules/base/pymod/export_logger.cc @@ -17,6 +17,7 @@ // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //------------------------------------------------------------------------------ #include <boost/python.hpp> +#include <boost/python/raw_function.hpp> using namespace boost::python; #include <ost/log.hh> @@ -82,11 +83,51 @@ LogSinkPtr get_log_sink() return Logger::Instance().GetCurrentSink(); } -void log_error(const String& m) {LOG_ERROR(m);} -void log_warning(const String& m) {LOG_WARNING(m);} -void log_script(const String& m) {LOG_SCRIPT(m);} -void log_info(const String& m) {LOG_INFO(m);} -void log_verbose(const String& m) {LOG_VERBOSE(m);} +String args_to_string(tuple args, dict kwargs) +{ + std::stringstream ss; + bool empty=true; + for (size_t i=0, l=len(args); i<l; ++i) { + if (!empty) { + ss << " "; + } + empty=false; + String string_val; + try { + string_val=extract<String>(args[i]); + } catch (...) { + string_val=extract<String>(args[i].attr("__str__")()); + } + ss << string_val; + } + return ss.str(); +} + +object log_error(tuple args, dict kwargs) +{ + LOG_ERROR(args_to_string(args, kwargs)); + return object(); +} +object log_warning(tuple args, dict kwargs) +{ + LOG_WARNING(args_to_string(args, kwargs)); + return object(); +} +object log_script(tuple args, dict kwargs) +{ + LOG_SCRIPT(args_to_string(args, kwargs)); + return object(); +} +object log_info(tuple args, dict kwargs) +{ + LOG_INFO(args_to_string(args, kwargs)); + return object(); +} +object log_verbose(tuple args, dict kwargs) +{ + LOG_VERBOSE(args_to_string(args, kwargs)); + return object(); +} void reset_sinks() @@ -121,11 +162,11 @@ void export_Logger() def("PushLogSink",push_log_sink); def("GetCurrentLogSink",get_log_sink); def("PopLogSink",pop_log_sink); - def("LogError",log_error); - def("LogWarning",log_warning); - def("LogInfo",log_info); - def("LogScript", log_script); - def("LogVerbose", log_verbose); + def("LogError", raw_function(log_error, 1)); + def("LogWarning",raw_function(log_warning, 1)); + def("LogInfo", raw_function(log_info, 1)); + def("LogScript", raw_function(log_script, 1)); + def("LogVerbose", raw_function(log_verbose, 1)); // this relatively ugly construct is required to work around a problem with // the "ost" command-line interpreter. If we don't remove all the sinks from diff --git a/modules/base/tests/test_log.py b/modules/base/tests/test_log.py index 7ea86c5bcf5b88fa6fbb6e7c6437cbfb71aa209e..9b6952c912dc28f6252360ea8c5e9f5a4879b189 100644 --- a/modules/base/tests/test_log.py +++ b/modules/base/tests/test_log.py @@ -34,6 +34,9 @@ class TestLog(unittest.TestCase): ost.LogError('error message') self.assertEqual(ls.message, 'error message\n') self.assertEqual(ls.severity, 0) + ost.LogWarning(1, 2, 3) + self.assertEqual(ls.message, '1 2 3\n') + self.assertEqual(ls.severity, 1) ost.PopLogSink() if __name__ == "__main__": try: