0

I've been learning the Boost Log library

http://www.boost.org/doc/libs/develop/libs/log/doc/html/index.html

but I've been unable to figure out how to display the user's time zone. There is a %q and %Q format option that looks promising but doesn't seem to work (I'm using MSVC++ 2013). Using this format "%Y-%m-%d %H:%M:%S.%f%Q", I get the following output:

1 [2015-08-18 21:27:16.860724] main.cpp#11, Test App Started.

but I would have expected

1 [2015-08-18 21:27:16.860724-08.00] main.cpp#11, Test App Started.

as explained in:

http://www.boost.org/doc/libs/develop/libs/log/doc/html/log/detailed/expressions.html#log.detailed.expressions.formatters

Here's the code I've been trying and a few commented out lines that I have also tried with no luck:

void Log::init() const
{
    boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::utc_clock());
//  boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock());

    boost::log::register_simple_formatter_factory<Severity, char>("Severity");
//  boost::log::register_formatter_factory("TimeStamp", boost::make_shared<timestamp_formatter_factory>());

    boost::log::add_common_attributes();

    boost::log::add_file_log
    (
        boost::log::keywords::file_name = "appname_%N.log", 
        boost::log::keywords::rotation_size = 10 * 1024 * 1024, 
        boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), 
        boost::log::keywords::format = 
            boost::log::expressions::stream
            << boost::log::expressions::attr<unsigned>("LineID") << " "
            << "[" << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f%Q"<< "]" << " "
            << "<" << boost::log::expressions::attr<Severity>("Severity") << _NSTR(">") << _NSTR(" ")
            << boost::log::expressions::smessage
//          "%LineID% [%TimeStamp(format=\"%Y-%m-%d %H:%M:%S.%f%Q\")%] <%Severity%>: %%Message%"
    );

    const auto severity = boost::log::expressions::attr<Severity>("Severity");

    boost::log::core::get()->set_filter
    (
        severity >= severityThreshold_
    );
}

Any suggestions on what I might be doing wrong?

Mitch
  • 1,716
  • 3
  • 25
  • 41

1 Answers1

2

Both utc_clock and local_clock produce values of type boost::posix_time::ptime, which do not have information of a time zone. The difference between the clocks is what time ptime represents - UTC or local time according to the time zone set on the machine. The formatter has no use for %Q and %q and replaces them with an empty string.

The time zone is present in the boost::local_time::local_date_time type, the %Q and %q placeholders will work for it. The library does not have a clock attribute that produces local_date_time, so you will have to write one yourself. See here for an example.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27
  • I gave up on the documentation as soon as I started reading. For example, in Design Overview the author says in "Attributes and attribute values" - "In order to initiate logging [?comma?] a log source must pass all data[,] associated with the log record[,] to the logging core." Aside from what appear to be misplaced commas, the terms used are not clear either. I really need an example that matches much closer to what I'm doing. – Mitch Aug 19 '15 at 23:04
  • You will need the documentation in order to understand the library, and the "Design Overview" section is the best way to do that. The terms are pretty clearly defined in the "Definitions" section and further expanded on in the "Design Overview". If those sections don't clear things for you wrt "attributes" and "attribute values" then (a) the author needs to improve the docs and (b) you need to find a simpler library. – Andrey Semashev Aug 21 '15 at 08:57
  • I've read it, but found it confusing. Somewhat due to the lack of proper English and somewhat due to incomplete glossary for terms. It's probably fine code, but I can't use it if I can't understand it. Happy to switch to a different library, but I can't find one. I'd learn it if my boss would give me the time. – Mitch Aug 24 '15 at 22:24
  • Know any other libraries? Here's what I'm seeking:Required: 1)Works with MSVC++ 2)Output Date/Time, Severity, Bitflag Categories, __FILE__, __LINE__, text message 3)User configurable filtering 4)Output to self maintaining Log files (Don't collect indefinitely, don't erase until configurable setting reached) 5)Ability to output to user dialog in configuable languages 6)Ability to output to server via http protocol Ideally: 1)Open source 2)Output to OS logging, if it exists. – Mitch Aug 24 '15 at 22:25