Skip to content
Snippets Groups Projects
Commit b21aa572 authored by Marco Biasini's avatar Marco Biasini
Browse files

python logging functions accept variable number of args

parent 8e05d5a1
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment