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

added GetCurrentLogSink()

parent 821ca899
Branches
Tags
No related merge requests found
...@@ -25,7 +25,11 @@ using namespace boost::python; ...@@ -25,7 +25,11 @@ using namespace boost::python;
using namespace ost; using namespace ost;
struct WrappedLogSink : public LogSink { struct PyLogSink: public LogSink {
};
struct WrappedLogSink : public PyLogSink, public wrapper<PyLogSink> {
WrappedLogSink(PyObject* self): self_(self) WrappedLogSink(PyObject* self): self_(self)
{ } { }
virtual void LogMessage(const String& message , int severity) virtual void LogMessage(const String& message , int severity)
...@@ -73,6 +77,10 @@ void pop_log_sink() ...@@ -73,6 +77,10 @@ void pop_log_sink()
Logger::Instance().PopSink(); Logger::Instance().PopSink();
} }
LogSinkPtr get_log_sink()
{
return Logger::Instance().GetCurrentSink();
}
void log_error(const String& m) {LOG_ERROR(m);} void log_error(const String& m) {LOG_ERROR(m);}
void log_warning(const String& m) {LOG_WARNING(m);} void log_warning(const String& m) {LOG_WARNING(m);}
...@@ -88,11 +96,13 @@ void reset_sinks() ...@@ -88,11 +96,13 @@ void reset_sinks()
void export_Logger() void export_Logger()
{ {
class_<LogSink, WrappedLogSinkPtr, class_<LogSink, LogSinkPtr, boost::noncopyable>("_LogSink", no_init)
.def("LogMessage", &LogSink::LogMessage)
;
class_<PyLogSink, WrappedLogSinkPtr, bases<LogSink>,
boost::noncopyable>("LogSink") boost::noncopyable>("LogSink")
.def("LogMessage", &WrappedLogSink::LogMessageDefault) .def("LogMessage", &WrappedLogSink::LogMessageDefault)
; ;
class_<MultiLogSink, MultiLogSinkPtr, bases<LogSink>, class_<MultiLogSink, MultiLogSinkPtr, bases<LogSink>,
boost::noncopyable >("MultiLogSink", init<>()) boost::noncopyable >("MultiLogSink", init<>())
.def("AddSink",&MultiLogSink::AddSink) .def("AddSink",&MultiLogSink::AddSink)
...@@ -109,6 +119,7 @@ void export_Logger() ...@@ -109,6 +119,7 @@ void export_Logger()
def("PopVerbosityLevel",pop_verb); def("PopVerbosityLevel",pop_verb);
def("GetVerbosityLevel",get_verb); def("GetVerbosityLevel",get_verb);
def("PushLogSink",push_log_sink); def("PushLogSink",push_log_sink);
def("GetCurrentLogSink",get_log_sink);
def("PopLogSink",pop_log_sink); def("PopLogSink",pop_log_sink);
def("LogError",log_error); def("LogError",log_error);
def("LogWarning",log_warning); def("LogWarning",log_warning);
......
...@@ -35,7 +35,7 @@ class DLLEXPORT LogSink { ...@@ -35,7 +35,7 @@ class DLLEXPORT LogSink {
public: public:
LogSink(){}; LogSink(){};
virtual ~LogSink() { } virtual ~LogSink() { }
virtual void LogMessage(const String& message, int severity=0)=0; virtual void LogMessage(const String& message, int severity=0) {};
}; };
typedef boost::shared_ptr<LogSink> LogSinkPtr; typedef boost::shared_ptr<LogSink> LogSinkPtr;
......
...@@ -4,6 +4,7 @@ set(OST_BASE_UNIT_TESTS ...@@ -4,6 +4,7 @@ set(OST_BASE_UNIT_TESTS
test_pod_vector.cc test_pod_vector.cc
test_stutil.py test_stutil.py
test_table.py test_table.py
test_log.py
tests.cc tests.cc
) )
......
import unittest
import ost
# Altough the logging system might appear to be too simple to be worth writing a
# specific test case for, it actually isn't. The python export is very fragile
# and seemingly trivial changes can break the code in unexpected ways. So let's
# check for some invariants
class TestLog(unittest.TestCase):
def testGetLogSink(self):
logsink=ost.GetCurrentLogSink()
self.assertTrue(hasattr(logsink, 'LogMessage'))
# Check if the return type of logsink is sane
ost.PushLogSink(ost.GetCurrentLogSink())
def testPushPopLogSink(self):
class MyLogSink(ost.LogSink):
def __init__(self):
ost.LogSink.__init__(self)
ls=MyLogSink()
ost.PushLogSink(ls)
self.assertEqual(ls, ost.GetCurrentLogSink())
ost.PopLogSink()
self.assertNotEqual(ls, ost.GetCurrentLogSink())
def testLogMessage(self):
class CapturingLogSink(ost.LogSink):
def __init__(self):
ost.LogSink.__init__(self)
def LogMessage(self, message, severity):
self.message=message
self.severity=severity
ost.PushLogSink(ls)
ls=CapturingLogSink()
ost.PushLogSink(ls)
ost.LogError('error message')
self.assertEqual(ls.message, 'error message\n')
self.assertEqual(ls.severity, 0)
ost.PopLogSink()
if __name__ == "__main__":
try:
unittest.main()
except Exception, e:
print e
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment