I would like to include GetLastError in each log output but the value is overwritten when boost::log calls another Windows API before the attribute value is queried. Anyone know a nice way around this?
class get_last_error_attribute_impl : public boost::log::attribute::impl {
public:
boost::log::attribute_value get_value() {
return boost::log::attributes::make_attribute_value(GetLastError());
}
};
class get_last_error_attribute : public boost::log::attribute {
public:
get_last_error_attribute() :
boost::log::attribute(new get_last_error_attribute_impl()) {
}
};
void init_logging() {
auto core = boost::log::core::get();
core->add_global_attribute("GetLastError", get_last_error_attribute());
auto sink = boost::make_shared<
boost::log::sinks::synchronous_sink<boost::log::sinks::debug_output_backend>>();
sink->set_formatter(
boost::log::expressions::stream
<< boost::log::trivial::severity
<< ": " << boost::log::expressions::smessage
<< " (" << boost::log::expressions::attr<DWORD>("GetLastError") << ")"
);
core->add_sink(sink);
}
void main() {
init_logging();
if (!DeleteFileA("c:\\foo.bar")) {
BOOST_LOG_TRIVIAL(warning) << "delete failed";
}
}
Log output:
warning: delete failed (0)
I solved this by using an assert to capture
GetLastErrorinstead