|
Boost : |
Subject: Re: [boost] Phoenix V3 and Logging library help.
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2011-07-04 14:23:50
Hi Fernando,
On Mon, Jul 4, 2011 at 5:04 PM, Fernando Pelliccioni
<fpelliccioni_at_[hidden]> wrote:
> Hi Thomas, all,
>
> I am researching about logging libraries.
> I had to discard Boost.Log ( from Andrey) because this library does not
> support GCC 3.4.
> I've seen other libraries, but none convinced me.
Yes, Phoenix seems to work with 3.4.6. For a complete overview see:
http://www.boost.org/development/tests/trunk/developer/phoenix.html
If you run into any problems, let me know.
> I'm thinking of writing a simple logging library, and to avoid the use of
> macros, I decided to do something like this:
Will comment inline.
> namespace prototype1
> {
> namespace levels
> {
This won't work. If you want to get the stuff working you sketched further down,
All these placeholders would need to be of a different type.
I would suggest to define different expression nodes for every level,
please have a look at the docs:
http://beta.boost.org/doc/libs/1_47_0_beta1/libs/phoenix/doc/html/phoenix/examples/adding_an_expression.html
> boost::phoenix::expression::argument<1>::type const trace = {{{}}};
> boost::phoenix::expression::argument<1>::type const debug = {{{}}};
> boost::phoenix::expression::argument<1>::type const info = {{{}}};
> boost::phoenix::expression::argument<1>::type const warn = {{{}}};
> boost::phoenix::expression::argument<1>::type const error = {{{}}};
> boost::phoenix::expression::argument<1>::type const fatal = {{{}}};
> } //namespace levels
>
> struct dynamic_logger
> {
> Â Â template <typename Pred>
> Â Â void log( Pred const & pred ) const
> Â Â {
> Â Â Â Â streams_type::const_iterator it = streams_.begin();
> Â Â Â Â streams_type::const_iterator end = streams_.end();
>
> Â Â Â Â for ( ; it != end; ++it )
> Â Â Â Â {
> Â Â Â Â Â Â pred( **it );
> Â Â Â Â }
> Â Â }
>
> Â Â void add_target ( std::ostream & ostr )
> Â Â {
> Â Â Â Â streams_.push_back( &ostr );
> Â Â }
>
>
> Â Â typedef std::vector< std::ostream* > streams_type; //to improve
> Â Â streams_type streams_;
> };
>
>
> template <typename Logger, typename Pred>
> void log( Logger & logger, Pred const & pred )
> {
> Â Â return logger.log(pred);
> }
>
>
> } //namespace prototype1
>
>
>
> int main(/*int argc, char* argv[]*/)
> {
> Â Â {
> Â Â Â Â using namespace prototype1;
> Â Â Â Â using namespace prototype1::levels;
>
> Â Â Â Â std::ofstream outfile ("test.log");
>
> Â Â Â Â dynamic_logger dl;
> Â Â Â Â dl.add_target( std::cout );
> Â Â Â Â dl.add_target( outfile );
>
> Â Â Â Â dl.log ( info << "hello" << " world!" << std::endl );
> Â Â Â Â dl.log ( warn << "hello" << " world!" << std::endl );
> Â Â Â Â dl.log ( error << "hello" << " world!" << std::endl );
>
> Â Â Â Â log( dl, info "hello" << " world!" << std::endl ); //free function
> Â Â }
>
> Â Â return 0;
> }
>
>
>
> This code is a simple prototype,
>
> This code is a prototype should be improved.
> The questions are:
>
> 1. Does  PhoenixV3 (and Proto) supports GCC 3?
See above
> 2. How to make the above code write the following ... ?
>
> Â Â Â Â INFO Â - hello world!
> Â Â Â Â WARN Â - hello world!
> Â Â Â Â ERROR - hello world!
> Â Â Â Â INFO Â - hello world!
>
> Is it possible?
Yes, please work through the docs, the link i posted you above should
get you started.
> The key is to identify the different Levels. (see namespace
> prototype1::levels)
>
> Please, suggestions and criticisms are welcome.
HTH,
Thomas
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk