From 35212ccc906586f68b574947756bbf57aee74dab Mon Sep 17 00:00:00 2001 From: Lionel Parreaux Date: Mon, 6 May 2013 15:27:19 +0200 Subject: [PATCH] adapted eoLogger for redirecting to ostreams --- eo/src/utils/eoLogger.cpp | 48 +++++++++++++++++++++++++++------------ eo/src/utils/eoLogger.h | 31 ++++++++++++++----------- eo/test/t-eoLogger.cpp | 24 ++++++++++++++++++-- 3 files changed, 73 insertions(+), 30 deletions(-) diff --git a/eo/src/utils/eoLogger.cpp b/eo/src/utils/eoLogger.cpp index 1330c5323..3a9b20098 100644 --- a/eo/src/utils/eoLogger.cpp +++ b/eo/src/utils/eoLogger.cpp @@ -40,6 +40,11 @@ Caner Candan #include "eoLogger.h" +/* TODO? +- changer oprateurs +- virer la structure "file" +*/ + void eoLogger::_init() { _standard_io_streams[&std::cout] = 1; @@ -55,6 +60,8 @@ void eoLogger::_init() addLevel("logging", eo::logging); addLevel("debug", eo::debug); addLevel("xdebug", eo::xdebug); + + //outStream = &std::cout; } eoLogger::eoLogger() : @@ -66,13 +73,12 @@ eoLogger::eoLogger() : _selectedLevel(eo::progress), _contextLevel(eo::quiet), - _fd(2), - _obuf(_fd, _contextLevel, _selectedLevel) + _obuf(_contextLevel, _selectedLevel) { std::ostream::init(&_obuf); _init(); } - +/* eoLogger::eoLogger(eo::file file) : std::ostream(NULL), @@ -88,11 +94,11 @@ eoLogger::eoLogger(eo::file file) : std::ostream::init(&_obuf); _init(); *this << file; -} +}*/ eoLogger::~eoLogger() { - if (_fd > 2) { ::close(_fd); } + //if (_fd > 2) { ::close(_fd); } } void eoLogger::_createParameters( eoParser& parser ) @@ -112,11 +118,14 @@ void eoLogger::_createParameters( eoParser& parser ) //------------------------------------------------------------------ // we're gonna redirect the log to the given filename if -o is used. //------------------------------------------------------------------ - +/* if ( ! _output.value().empty() ) { eo::log << eo::file( _output.value() ); } +*/ +// TODO with a stream + //------------------------------------------------------------------ @@ -162,12 +171,12 @@ eoLogger& operator<<(eoLogger& l, const eo::Levels lvl) l._contextLevel = lvl; return l; } - +/* eoLogger& operator<<(eoLogger& l, eo::file f) { l._fd = ::open(f._f.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644); return l; -} +}*/ eoLogger& operator<<(eoLogger& l, eo::setlevel v) { @@ -177,26 +186,35 @@ eoLogger& operator<<(eoLogger& l, eo::setlevel v) eoLogger& operator<<(eoLogger& l, std::ostream& os) { +/* if (l._standard_io_streams.find(&os) != l._standard_io_streams.end()) { l._fd = l._standard_io_streams[&os]; } +*/ + l._obuf.outStream = &os; return l; } -eoLogger::outbuf::outbuf(const int& fd, - const eo::Levels& contexlvl, +void eoLogger::redirectTo(std::ostream& os) +{ + _obuf.outStream = &os; +} + + +eoLogger::outbuf::outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl) - : _fd(fd), _contextLevel(contexlvl), _selectedLevel(selectedlvl) + : outStream(&std::cout), /*ownedFileStream(NULL),*/ _contextLevel(contexlvl), _selectedLevel(selectedlvl) {} int eoLogger::outbuf::overflow(int_type c) { if (_selectedLevel >= _contextLevel) { - if (_fd >= 0 && c != EOF) + if (outStream && c != EOF) { - ::write(_fd, &c, 1); + //::write(_fd, &c, 1); + (*outStream) << (char) c; } } return c; @@ -204,9 +222,9 @@ int eoLogger::outbuf::overflow(int_type c) namespace eo { - file::file(const std::string f) + /*file::file(const std::string f) : _f(f) - {} + {}*/ setlevel::setlevel(const std::string v) : _v(v), _lvl((Levels)-1) diff --git a/eo/src/utils/eoLogger.h b/eo/src/utils/eoLogger.h index 9ac1c2115..99bafa001 100644 --- a/eo/src/utils/eoLogger.h +++ b/eo/src/utils/eoLogger.h @@ -91,6 +91,7 @@ Caner Candan #include #include #include +#include #include "eoObject.h" #include "eoParser.h" @@ -113,12 +114,12 @@ namespace eo /** * file * this structure combined with the friend operator<< below is an easy way to select a file as output. - */ + * struct file { explicit file(const std::string f); const std::string _f; - }; + };*/ /** * setlevel @@ -146,7 +147,7 @@ public: eoLogger(); //! overidded ctor in order to instanciate a logger with a file define in parameter - eoLogger(eo::file file); + //eoLogger(eo::file file); //! dtor ~eoLogger(); @@ -189,11 +190,12 @@ private: class outbuf : public std::streambuf { public: - outbuf(const int& fd, const eo::Levels& contexlvl, const eo::Levels& selectedlvl); + outbuf(const eo::Levels& contexlvl, const eo::Levels& selectedlvl); + std::ostream * outStream; protected: virtual int overflow(int_type c); private: - const int& _fd; + //std::ofstream * ownedFileStream; const eo::Levels& _contextLevel; const eo::Levels& _selectedLevel; }; @@ -215,7 +217,7 @@ public: * operator<< used there to set a filename through the class file. */ //! in order to use stream style to define a file to dump instead the standart output - friend eoLogger& operator<<(eoLogger&, eo::file); + //friend eoLogger& operator<<(eoLogger&, eo::file); /** * operator<< used there to set a verbose level through the class setlevel. @@ -224,12 +226,21 @@ public: friend eoLogger& operator<<(eoLogger&, eo::setlevel); /** + * DEPRECATED: Use instead the redirectTo method * operator<< used there to be able to use std::cout to say that we wish to redirect back the buffer to a standard output. */ //! in order to use stream style to go back to a standart output defined by STL //! and to get retro-compatibility +#warning deprecated friend eoLogger& operator<<(eoLogger&, std::ostream&); + /** + * Redirects the logger to a given output stream. Closing the stream and returning its memory is at the charge of the caller, + * but should not be done while the log is still redirected to it. + */ + void redirectTo(std::ostream&); + + private: friend void make_verbose(eoParser&); @@ -244,13 +255,7 @@ private: eo::Levels _contextLevel; /** - * _fd in storing the file descriptor at this place we can disable easily the buffer in - * changing the value at -1. It is used by operator <<. - */ - int _fd; - - /** - * _obuf std::ostream mandates to use a buffer. _obuf is a outbuf inheriting of std::streambuf. + * _obuf std::ostream mandates to use a buffer. _obuf is a outbuf inheriting from std::streambuf. */ outbuf _obuf; diff --git a/eo/test/t-eoLogger.cpp b/eo/test/t-eoLogger.cpp index 198a88720..65f9df603 100644 --- a/eo/test/t-eoLogger.cpp +++ b/eo/test/t-eoLogger.cpp @@ -3,6 +3,10 @@ //----------------------------------------------------------------------------- #include +//#include +#include +#include +#include //----------------------------------------------------------------------------- @@ -25,8 +29,24 @@ int main(int ac, char** av) eo::log << "We are writing on the default output stream" << std::endl; - eo::log << eo::file("test.txt") << "In FILE" << std::endl; - eo::log << std::cout << "on COUT" << std::endl; + //eo::log << eo::file("test.txt") << "In FILE" << std::endl; + + std::ofstream ofs("test.txt"); + //eo::log << ofs << "In FILE" << std::endl; + eo::log.redirectTo(ofs); + eo::log << "In FILE" << std::endl; + + std::ostringstream oss; + //eo::log << oss << "In STRINGSTREAM"; + eo::log.redirectTo(oss); + eo::log << oss << "In STRINGSTREAM"; + + //ofs << oss; + std::cout << "Content of ostringstream: " << oss.str() << std::endl; + + //eo::log << std::cout << "on COUT" << std::endl; + eo::log.redirectTo(std::cout); + eo::log << "on COUT" << std::endl; eo::log << eo::setlevel("errors"); eo::log << eo::setlevel(eo::errors);