From 766db39fd883b476fea0b059be4db77c9c4003ae Mon Sep 17 00:00:00 2001 From: Andreas Schenk <andreas_schenk@hms.harvard.edu> Date: Fri, 9 Mar 2012 13:51:03 -0500 Subject: [PATCH] added StringLogSink StringLogSink is very similar to the StreamLogSink, but the encapsulation of the ostringstream in the Sink allows much easier export to Python --- modules/base/pymod/export_logger.cc | 6 ++++++ modules/base/src/log_sink.hh | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/modules/base/pymod/export_logger.cc b/modules/base/pymod/export_logger.cc index f23e08714..91eccbdec 100644 --- a/modules/base/pymod/export_logger.cc +++ b/modules/base/pymod/export_logger.cc @@ -156,6 +156,12 @@ void export_Logger() .def("LogMessage", &FileLogSink::LogMessage) ; + class_<StringLogSink, StringLogSinkPtr, bases<LogSink>, + boost::noncopyable >("StringLogSink", init<>()) + .def("LogMessage", &StringLogSink::LogMessage) + .def("GetLog", &StringLogSink::GetLog) + ; + def("PushVerbosityLevel",push_verb); def("PopVerbosityLevel",pop_verb); def("GetVerbosityLevel",get_verb); diff --git a/modules/base/src/log_sink.hh b/modules/base/src/log_sink.hh index 6cf939201..f06d4ae1c 100644 --- a/modules/base/src/log_sink.hh +++ b/modules/base/src/log_sink.hh @@ -20,6 +20,7 @@ #define OST_LOG_SINK_HH #include <ostream> +#include <sstream> #include <iostream> #include <fstream> #include <stack> @@ -51,6 +52,23 @@ private: std::ostream& stream_; }; +class DLLEXPORT StringLogSink : public LogSink { +public: + StringLogSink():LogSink(),stream_(){} + virtual void LogMessage(const String& message, int severity){ + stream_ << message; + } + String GetLog() const + { + return stream_.str(); + } + +private: + std::ostringstream stream_; +}; + +typedef boost::shared_ptr<StringLogSink> StringLogSinkPtr; + class DLLEXPORT FileLogSink : public LogSink { public: FileLogSink(const String& file_name):stream_(file_name.c_str(), std::ios::out){} -- GitLab