I've been struggling with boost log for a while now - I got their simple example writing to a log file (http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_file.cpp).The problem is when I use this part of the code ...
void init()
{
logging::add_file_log("sample.log");
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
Instead of sample.log file, 00000.log file is created. And when I use this part of the code ...
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%]: %Message%"
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
I get the following error message: Exception thrown at 0x00007FF787AD0393 in Boost_log_sample.exe: 0xC0000005: Access violation reading location 0x00003BBA20597184.
I'm on win10 64bit and used VS2015,boost_1_71_0.Installing Boost With NuGet (https://www.scalyr.com/blog/getting-started-quickly-c-logging/)
I don't have this problem with boost_1_66_0.
Here's my code:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log", /*< file name pattern >*/
keywords::rotation_size = 10 * 1024 * 1024, /*< rotate files every 10 MiB... >*/
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
keywords::format = "[%TimeStamp%]: %Message%" /*< log record format >*/
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
int main(int, char*[])
{
init();
logging::add_common_attributes();
using namespace logging::trivial;
src::severity_logger< severity_level > lg;
BOOST_LOG_SEV(lg, trace) << "A trace severity message";
BOOST_LOG_SEV(lg, debug) << "A debug severity message";
BOOST_LOG_SEV(lg, info) << "An informational severity message";
BOOST_LOG_SEV(lg, warning) << "A warning severity message";
BOOST_LOG_SEV(lg, error) << "An error severity message";
BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
return 0;
}
I also stumbled upon the problem of the log being called 00000.log
while following the Boost.Log tutorial on a system with Boost 1.72.0 installed, using the code in the tutorial, i.e.:
logging::add_file_log("sample.log");
Additionally, as explained in the question, I could not reproduce the problem on a system with an older version of Boost, 1.67.0 to be precise. The log was named sample.log
there.
After some experimentation, I found that on the system with 1.72.0, by using the target_file_name
parameter, the log is written to sample.log
as desired:
// DO NOT USE IN PRODUCTION, SEE BELOW
logging::add_file_log
(
keywords::file_name = "sample.log",
keywords::target_file_name = "sample.log"
);
While I am not too familiar with Boost.Log, it appears that the documentation for the target_file_name
parameter implies that this should not be necessary in this case, so the above should likely be seen as a workaround. The target_file_name
parameter also doesn't exist on Boost 1.67.0 and below so this workaround will also cause compilation problems there.
So this looks like either a regression or a poorly documented breaking change. It would be interesting if someone more familiar with the library could shed further light on the issue.
EDIT: I have opened an issue upstream: https://github.com/boostorg/log/issues/104
(Unfortunately, I can't reproduce the crash problem in the second half of the question)
User contributions licensed under CC BY-SA 3.0