-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Boost.Log rotation may throw boost::filesystem::filesystem_error #211
Comments
As any filesystem operation, rotating log files may fail, this is to be expected. From your information, it isn't clear what exactly happened, so I can't tell whether the error is legitimate. In any case, in order to handle exceptions, you can set an exception handler to the sink frontend. |
@Lastique I already set exception handler, still can NOT catch the exception. struct ExceptionHandler {
typedef void result_type;
void operator()(std::exception const& e) const noexcept {
OutputDebugStringA(std::format("std::runtime_error: {}\n", e.what()).data());
}
void operator()(boost::filesystem::filesystem_error const& e) const noexcept {
OutputDebugStringA(
std::format("boost::filesystem::filesystem_error: {}\n", e.what()).data());
}
};
// catch nothing
auto core = logging::core::get();
core->set_exception_handler(
boost::log::make_exception_handler<std::exception, boost::filesystem::filesystem_error>(
ExceptionHandler()));
// catch nothing
file_sink->set_exception_handler(
boost::log::make_exception_handler<std::exception, boost::filesystem::filesystem_error>(
ExceptionHandler())); |
If it doesn't catch then the exception is not one of the types you listed. BTW, it doesn't make sense to list Also note that Getting back to the problem, you should debug to see what the exception type is and where it is being thrown. |
Thank you! I'll improve my codes. From minidump, I see the exception is a boost::filesystem::filesystem_error. Since the crash is on other users' machine, I can't debug. |
For the record, the log/include/boost/log/sinks/basic_sink_frontend.hpp Lines 436 to 480 in 28c6b1d
As you can see, the function does catch all exceptions thrown by |
I think I've solved this issue by adding directory to target_file_name. keywords::target_file_name =
config.directory / (config.stem + L"_%Y%m%d_%H%M%S_%5N.log") If the directory is not specified, the new log file will be created on //! The method rotates the file
BOOST_LOG_API void text_file_backend::rotate_file()
{
filesystem::path prev_file_name = m_pImpl->m_FileName;
close_file();
// Check if the file has been created in the first place
system::error_code ec;
filesystem::file_status status = filesystem::status(prev_file_name, ec);
if (status.type() == filesystem::regular_file)
{
if (!!m_pImpl->m_TargetFileNameGenerator)
{
filesystem::path new_file_name = m_pImpl->m_TargetStorageDir / m_pImpl->m_TargetFileNameGenerator(m_pImpl->m_FileCounter);
if (new_file_name != prev_file_name)
{
filesystem::create_directories(new_file_name.parent_path());
move_file(prev_file_name, new_file_name);
prev_file_name.swap(new_file_name);
}
}
if (!!m_pImpl->m_pFileCollector)
m_pImpl->m_pFileCollector->store_file(prev_file_name);
}
} |
@Lastique How about specifying |
Good, it explains why you were getting an exception. It doesn't explain why the exception wasn't caught, though. Which means, if an exception is thrown for whatever other reason, you will have the same problem again. I'd recommend continuing debugging to fix this. Double-check that you set an exception handler for all sinks. Check if it's actually being called by setting a custom function object with console output or something instead of the one generated by |
I don't like introducing this kind of non-obvious behavior. Currently, any relative paths you provide to the library are interpreted the same way as anywhere else in the process - they are relative to the current path for the process. Which means, it is probably a good idea to always provide absolute paths. But that doesn't mean it is a good idea to replace the path behind user's back. |
When I wrote the following codes, I would naturally assume that target_file_name would be in the same directory as file_name. New log file is temporarily create in m_TargetStorageDir, and will be move to m_StorageDir. So I think this behavior do some good. keywords::file_name = log_file,
keywords::target_file_name = config.stem + L"_%Y%m%d_%H%M%S_%5N.log" |
Only throw on a few machine, but I think it's worth reporting.
Unhandled exception at 0x00007FF8A2CECD29 in 20230710175425258.dmp: Microsoft C++ exception: boost::filesystem::filesystem_error at memory location 0x000000A617BFF6E8.
The codes:
The text was updated successfully, but these errors were encountered: