adapted eoLogger for redirecting to ostreams
This commit is contained in:
parent
ea62b371e5
commit
146aff8a3b
3 changed files with 73 additions and 30 deletions
|
|
@ -40,6 +40,11 @@ Caner Candan <caner.candan@thalesgroup.com>
|
||||||
|
|
||||||
#include "eoLogger.h"
|
#include "eoLogger.h"
|
||||||
|
|
||||||
|
/* TODO?
|
||||||
|
- changer oprateurs
|
||||||
|
- virer la structure "file"
|
||||||
|
*/
|
||||||
|
|
||||||
void eoLogger::_init()
|
void eoLogger::_init()
|
||||||
{
|
{
|
||||||
_standard_io_streams[&std::cout] = 1;
|
_standard_io_streams[&std::cout] = 1;
|
||||||
|
|
@ -55,6 +60,8 @@ void eoLogger::_init()
|
||||||
addLevel("logging", eo::logging);
|
addLevel("logging", eo::logging);
|
||||||
addLevel("debug", eo::debug);
|
addLevel("debug", eo::debug);
|
||||||
addLevel("xdebug", eo::xdebug);
|
addLevel("xdebug", eo::xdebug);
|
||||||
|
|
||||||
|
//outStream = &std::cout;
|
||||||
}
|
}
|
||||||
|
|
||||||
eoLogger::eoLogger() :
|
eoLogger::eoLogger() :
|
||||||
|
|
@ -66,13 +73,12 @@ eoLogger::eoLogger() :
|
||||||
|
|
||||||
_selectedLevel(eo::progress),
|
_selectedLevel(eo::progress),
|
||||||
_contextLevel(eo::quiet),
|
_contextLevel(eo::quiet),
|
||||||
_fd(2),
|
_obuf(_contextLevel, _selectedLevel)
|
||||||
_obuf(_fd, _contextLevel, _selectedLevel)
|
|
||||||
{
|
{
|
||||||
std::ostream::init(&_obuf);
|
std::ostream::init(&_obuf);
|
||||||
_init();
|
_init();
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
eoLogger::eoLogger(eo::file file) :
|
eoLogger::eoLogger(eo::file file) :
|
||||||
std::ostream(NULL),
|
std::ostream(NULL),
|
||||||
|
|
||||||
|
|
@ -88,11 +94,11 @@ eoLogger::eoLogger(eo::file file) :
|
||||||
std::ostream::init(&_obuf);
|
std::ostream::init(&_obuf);
|
||||||
_init();
|
_init();
|
||||||
*this << file;
|
*this << file;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
eoLogger::~eoLogger()
|
eoLogger::~eoLogger()
|
||||||
{
|
{
|
||||||
if (_fd > 2) { ::close(_fd); }
|
//if (_fd > 2) { ::close(_fd); }
|
||||||
}
|
}
|
||||||
|
|
||||||
void eoLogger::_createParameters( eoParser& parser )
|
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.
|
// we're gonna redirect the log to the given filename if -o is used.
|
||||||
//------------------------------------------------------------------
|
//------------------------------------------------------------------
|
||||||
|
/*
|
||||||
if ( ! _output.value().empty() )
|
if ( ! _output.value().empty() )
|
||||||
{
|
{
|
||||||
eo::log << eo::file( _output.value() );
|
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;
|
l._contextLevel = lvl;
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
eoLogger& operator<<(eoLogger& l, eo::file f)
|
eoLogger& operator<<(eoLogger& l, eo::file f)
|
||||||
{
|
{
|
||||||
l._fd = ::open(f._f.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
|
l._fd = ::open(f._f.c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644);
|
||||||
return l;
|
return l;
|
||||||
}
|
}*/
|
||||||
|
|
||||||
eoLogger& operator<<(eoLogger& l, eo::setlevel v)
|
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)
|
eoLogger& operator<<(eoLogger& l, std::ostream& os)
|
||||||
{
|
{
|
||||||
|
/*
|
||||||
if (l._standard_io_streams.find(&os) != l._standard_io_streams.end())
|
if (l._standard_io_streams.find(&os) != l._standard_io_streams.end())
|
||||||
{
|
{
|
||||||
l._fd = l._standard_io_streams[&os];
|
l._fd = l._standard_io_streams[&os];
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
l._obuf.outStream = &os;
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
eoLogger::outbuf::outbuf(const int& fd,
|
void eoLogger::redirectTo(std::ostream& os)
|
||||||
const eo::Levels& contexlvl,
|
{
|
||||||
|
_obuf.outStream = &os;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
eoLogger::outbuf::outbuf(const eo::Levels& contexlvl,
|
||||||
const eo::Levels& selectedlvl)
|
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)
|
int eoLogger::outbuf::overflow(int_type c)
|
||||||
{
|
{
|
||||||
if (_selectedLevel >= _contextLevel)
|
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;
|
return c;
|
||||||
|
|
@ -204,9 +222,9 @@ int eoLogger::outbuf::overflow(int_type c)
|
||||||
|
|
||||||
namespace eo
|
namespace eo
|
||||||
{
|
{
|
||||||
file::file(const std::string f)
|
/*file::file(const std::string f)
|
||||||
: _f(f)
|
: _f(f)
|
||||||
{}
|
{}*/
|
||||||
|
|
||||||
setlevel::setlevel(const std::string v)
|
setlevel::setlevel(const std::string v)
|
||||||
: _v(v), _lvl((Levels)-1)
|
: _v(v), _lvl((Levels)-1)
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,7 @@ Caner Candan <caner.candan@thalesgroup.com>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include "eoObject.h"
|
#include "eoObject.h"
|
||||||
#include "eoParser.h"
|
#include "eoParser.h"
|
||||||
|
|
@ -113,12 +114,12 @@ namespace eo
|
||||||
/**
|
/**
|
||||||
* file
|
* file
|
||||||
* this structure combined with the friend operator<< below is an easy way to select a file as output.
|
* this structure combined with the friend operator<< below is an easy way to select a file as output.
|
||||||
*/
|
*
|
||||||
struct file
|
struct file
|
||||||
{
|
{
|
||||||
explicit file(const std::string f);
|
explicit file(const std::string f);
|
||||||
const std::string _f;
|
const std::string _f;
|
||||||
};
|
};*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* setlevel
|
* setlevel
|
||||||
|
|
@ -146,7 +147,7 @@ public:
|
||||||
eoLogger();
|
eoLogger();
|
||||||
|
|
||||||
//! overidded ctor in order to instanciate a logger with a file define in parameter
|
//! overidded ctor in order to instanciate a logger with a file define in parameter
|
||||||
eoLogger(eo::file file);
|
//eoLogger(eo::file file);
|
||||||
|
|
||||||
//! dtor
|
//! dtor
|
||||||
~eoLogger();
|
~eoLogger();
|
||||||
|
|
@ -189,11 +190,12 @@ private:
|
||||||
class outbuf : public std::streambuf
|
class outbuf : public std::streambuf
|
||||||
{
|
{
|
||||||
public:
|
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:
|
protected:
|
||||||
virtual int overflow(int_type c);
|
virtual int overflow(int_type c);
|
||||||
private:
|
private:
|
||||||
const int& _fd;
|
//std::ofstream * ownedFileStream;
|
||||||
const eo::Levels& _contextLevel;
|
const eo::Levels& _contextLevel;
|
||||||
const eo::Levels& _selectedLevel;
|
const eo::Levels& _selectedLevel;
|
||||||
};
|
};
|
||||||
|
|
@ -215,7 +217,7 @@ public:
|
||||||
* operator<< used there to set a filename through the class file.
|
* 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
|
//! 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.
|
* operator<< used there to set a verbose level through the class setlevel.
|
||||||
|
|
@ -224,12 +226,21 @@ public:
|
||||||
friend eoLogger& operator<<(eoLogger&, eo::setlevel);
|
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.
|
* 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
|
//! in order to use stream style to go back to a standart output defined by STL
|
||||||
//! and to get retro-compatibility
|
//! and to get retro-compatibility
|
||||||
|
#warning deprecated
|
||||||
friend eoLogger& operator<<(eoLogger&, std::ostream&);
|
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:
|
private:
|
||||||
friend void make_verbose(eoParser&);
|
friend void make_verbose(eoParser&);
|
||||||
|
|
||||||
|
|
@ -244,13 +255,7 @@ private:
|
||||||
eo::Levels _contextLevel;
|
eo::Levels _contextLevel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* _fd in storing the file descriptor at this place we can disable easily the buffer in
|
* _obuf std::ostream mandates to use a buffer. _obuf is a outbuf inheriting from std::streambuf.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
outbuf _obuf;
|
outbuf _obuf;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,10 @@
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#include <eo>
|
#include <eo>
|
||||||
|
//#include <paradiseo/eo.h>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -25,8 +29,24 @@ int main(int ac, char** av)
|
||||||
|
|
||||||
eo::log << "We are writing on the default output stream" << std::endl;
|
eo::log << "We are writing on the default output stream" << std::endl;
|
||||||
|
|
||||||
eo::log << eo::file("test.txt") << "In FILE" << std::endl;
|
//eo::log << eo::file("test.txt") << "In FILE" << std::endl;
|
||||||
eo::log << std::cout << "on COUT" << 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("errors");
|
||||||
eo::log << eo::setlevel(eo::errors);
|
eo::log << eo::setlevel(eo::errors);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue