Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2008-08-20 14:36:47


Boris wrote:
> On Wed, 20 Aug 2008 18:27:39 +0200, Andrey Semashev
> <andrey.semashev_at_[hidden]> wrote:
>
> Hi Andrey,
>
>> [...]
>>> If I
>>> use boost::log::formatters::attr("TimeStamp") nothing is written to the
>>> log file.
>>
>> That's odd. Could you send a short code sample that shows the problem?
>
> here's the entire program:
>
> ----------
> #include <boost/log/sources/severity_logger.hpp>
> #include <boost/log/logging_core.hpp>
> #include <boost/log/utility/init/to_file.hpp>
> #include <boost/log/utility/init/common_attributes.hpp>
> #include <boost/log/formatters/format.hpp>
> #include <boost/log/formatters/attr.hpp>
> #include <boost/log/formatters/message.hpp>
> #include <boost/shared_ptr.hpp>
>
> boost::log::sources::severity_logger logger;
>
> int main()
> {
> boost::log::add_common_attributes();
>
> boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>>
> sink = boost::log::init_log_to_file("test.txt");
>
> sink->locked_backend()->set_formatter(boost::log::formatters::format("[%1%]
> %2%") %
> boost::log::formatters::attr("TimeStamp") %
> boost::log::formatters::message());
>
> sink->locked_backend()->auto_flush(true);
>
> BOOST_LOG(logger) << "message";
> }
> ----------
>
> If I build it with VC++ 9.0 SP1 the file test.txt contains afterwards:
>
> [] message
>
> Only if I use boost::log::formatters::date_time("TimeStamp") the
> timestamp is written.

This is not a bug. The problem is that you used attr formatter without
specifying date type. The attr formatter by default supports most
fundamental types and strings. For other types the attribute value type
(or set of types) must be specified explicitly:

  boost::log::formatters::attr< boost::posix_time::ptime >("TimeStamp")

As for the date_time formatter, it implicitly supports all date and time
types defined in the library documentation, which includes ptime. That's
why it worked right away.


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