Boost logo

Boost :

From: JD (jean.daniel.michaud_at_[hidden])
Date: 2007-08-23 13:56:35


Hello,

   Discussion about a logging library started over some time ago. In the
vault my implementation. As for now, I am implementing some of the
requirements from the boost wiki (http://minilien.com/?rQB7NorvIs):

Functional Requirements
  3. Eliminate log statements from generated code
      through the definition a macro.
  4. Full lazy evaluation
  5. Sinks
  7. Configurable log message attributes

Design Requirements
  1. Configurable log message attributes
  2. Macro access

   Here is an example of use:

// Code
#include <fstream>
#include <logging.hpp>

using namespace boost::logging;
namespace bl = boost::logging;

void overheat(int d)
{
   BOOST_LOG(2, bl::log, "overheat called");
   BOOST_LOG(1, bl::warning, "Warning: Tube overheat! Shutdown system
immediatly");
   BOOST_LOG(1, bl::error, "ERROR: tube overheat: " << d << "d C.
Shutdown should follow");
}

int do_something()
{
   BOOST_LOG(2, bl::log, "do_something called");
   BOOST_LOG(2, bl::notice, "do_something is performing some fancy
processing (useless log don't you think?)");

   return 7;
}

int main(int argc, char **argv)
{
   boost::logging::logger *l = boost::logging::logger::get_instance();

   boost::logging::format display_format(bl::trace >> bl::eol);
   boost::logging::format file_format("[" >> bl::level >> "],"
>> bl::filename >> "("
>> bl::line >> "),"
>> bl::time >> ","
>> bl::trace
>> bl::eol); // log format
   l->add_format(display_format);
   l->add_format(file_format);

   boost::logging::sink file_sink(new std::ofstream("./output.log"), 3);
   file_sink.attach_qualifier(bl::log);
   file_sink.attach_qualifier(bl::error);
   l->add_sink(file_sink, file_format);

   boost::logging::sink display_sink(&std::cout, 1);
   display_sink.attach_qualifier(bl::notice);
   display_sink.attach_qualifier(bl::warning);
   l->add_sink(display_sink, display_format);

   BOOST_LOG(1, bl::log, "Application starting");
   BOOST_LOG(1, bl::notice, "Application version 1.0.3 - Copyright(2007)
World Company");

   BOOST_LOG(1, bl::log, "do_something returned: " << do_something());
   overheat(87);
   return 0;
}

Standard output:
Application version 1.0.3 - Copyright(2007) World Company
Warning: Tube overheat! Shutdown system immediatly

Content of output.log:
[1],g:\projects\loglite\test\logging_test_qualifier.cpp(57),2007-Aug-23
19:44:26.411856,Application starting
[2],g:\projects\loglite\test\logging_test_qualifier.cpp(26),2007-Aug-23
19:44:26.451913,do_something called
[1],g:\projects\loglite\test\logging_test_qualifier.cpp(60),2007-Aug-23
19:44:26.451913,do_something returned: 7
[2],g:\projects\loglite\test\logging_test_qualifier.cpp(19),2007-Aug-23
19:44:26.451913,overheat called
[1],g:\projects\loglite\test\logging_test_qualifier.cpp(21),2007-Aug-23
19:44:26.461928,ERROR: tube overheat: 87d C. Shutdown should follow

   I am welcoming any comment if you think it worth it.
   The library can be found in the vault here:
http://minilien.com/?QO9kodiNIs
   The google code page is here: http://code.google.com/p/loglite/
   The checkout the svn repository: svn checkout
http://loglite.googlecode.com/svn/trunk/ loglite

   Note that Andrey Semashev is also working on an implementation that
you can find here: http://sourceforge.net/projects/boost-log

Regards,

JD


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk