Boost Log fails when trying to execute the first log statement (if not an administrator)

I am trying to deploy my application right now that uses Boost Log (Boost 1.58). This is a simple console application that runs on Windows 7. Logging works fine on my personal desktop.

However, when I deploy the application to a Win7 virtual machine, it crashes to my first log statement:

boost::log::sources::severity_logger<SeverityLevel> slg; BOOST_LOG_SEV(slg, SeverityLevel::Notification) << L"Application loaded"; // <-- Crash here 

A log directory is created, but the log file is never created and the application does not work.

I tried the journal directory in the% APPDATA% directory, as well as in my My Documents directory.

The odd thing: when I run the application as administrator, it works!

So this should be a permissions thing, but I have rights to these folders, so ...

Any ideas?

* MORE *

Here is the code to configure my registrar:

  wstring appData = GetMyAppDataPath(); boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > > textFileSink(new boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend >(to store rotated files boost::log::keywords::file_name = "log_%7N.log", // file name pattern boost::log::keywords::rotation_size = 16384 // rotation size, in characters )); // Set up where the rotated files will be stored textFileSink->locked_backend()->set_file_collector(boost::log::sinks::file::make_collector( boost::log::keywords::target = appData + L"\\logs", // where to store rotated files boost::log::keywords::max_size = 64 * 1024 * 1024, // maximum total size of the stored files, in bytes (64 MiB) boost::log::keywords::min_free_space = 100 * 1024 * 1024 // minimum free space on the drive, in bytes )); // Upon restart, scan the target directory for files matching the file_name pattern textFileSink->locked_backend()->scan_for_files(); // Set up the format for output to the text file. textFileSink->set_formatter(boost::log::expressions::stream << "[" << boost::log::expressions::attr< boost::posix_time::ptime >("TimeStamp") << " " << boost::log::expressions::attr< SeverityLevel, severity_tag >("Severity") << "] " << boost::log::expressions::message ); // Add it to the core boost::log::core::get()->add_sink(textFileSink); 

which is mainly taken here: http://www.boost.org/doc/libs/1_58_0/libs/log/example/rotating_file/main.cpp .

* ALSO *

I added an exception handler to Boost Logger, adding:

 boost::log::core::get()->set_exception_handler(boost::log::make_exception_handler< std::runtime_error, std::logic_error, std::exception >(pbb_boost_log_exception_handler())); 

Then add a handler. When I run, I can catch the following exception before the crash:

 std::exception: Failed to open file for writing: Input/output error: "C:\Program Files\My App\log_0000000.log" 

WTF? I definitely set the location of the log file to appData , which I checked correctly. In addition, if I run this application as an administrator, the log file ends where I expect it (appdata folder). Therefore, it should simply create a temporary file in an executable location. Is this normal behavior? I can’t imagine that this is so ... so what did I do?

+4
source share
1 answer

Ok, I figured it out.

The problem was this line, creating a text server:

 boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > > textFileSink(new boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > boost::log::keywords::file_name = "log_%7N.log", // file name pattern boost::log::keywords::rotation_size = 16384 // rotation size, in characters )); 

A temporary log file has been defined which is written to the local directory (the directory of program files, which is bad). Based on the documentation here , I saw that there is a temporary file written and then transferred to the file collector.

So, for the solution, I changed the code to this:

 boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > > textFileSink(new boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend >( boost::log::keywords::file_name = appData + L"\\log_%7N.log", // file name pattern boost::log::keywords::rotation_size = 16384 // rotation size, in characters )); 

Note that now I am specifying file_name as being in the AppData directory.

This solved the problem .

I find it hard to believe that I am the first to encounter this problem, but I could not find it anywhere on the Internet. This would be a recurring issue for window developers, so hopefully this helps someone else.

+4
source

All Articles