Include line number in boost logs
Hello,
I try to implement that a log entry also logs its line number with
boost::log. If any developer of it reads here, please also consider this a
wishlist item...
I think the best way to add an additional attribute for it
core::get()->add_global_attribute(
"Line", attributes::mutable_constant
I finally got it. Shared the answer on stack overflow: http://stackoverflow.com/a/32200033/3585934 Florian Lindner wrote:
Hello,
I try to implement that a log entry also logs its line number with boost::log. If any developer of it reads here, please also consider this a wishlist item...
I think the best way to add an additional attribute for it
core::get()->add_global_attribute( "Line", attributes::mutable_constant
(0) ); in my logger config that is called once at startup.
#define logInfo(methodname, message) \ boost::log::core::get()->get_global_attributes()["Line"].set(__LINE__); \ BOOST_LOG_SEV(_log, boost::log::trivial::severity_level::trace) << message \
Is my idea, but
boostlog.cpp:126:5: error: no member named 'set' in 'boost::log::v2_mt_posix::aux::attribute_set_reference_proxy'
getting the attribute somehow seems to work, but how can I set the value of the attribute?
Thanks! Florian
On 25.08.2015 11:15, Florian Lindner wrote:
I finally got it. Shared the answer on stack overflow:
Thank you for sharing this. I'd just recommend to write macro #define's in such a way that they are syntactically and semantically neutral ... using do { .... macro contents ... } while (false) is a good way to achieve this. For instance, this: #define logInfo(methodname, message) do { \ LOG_LOCATION; \ BOOST_LOG_SEV(_log, boost::log::trivial::severity_level::trace) << message \ while (false) would allow constructs such as: if (something) logInfo("my_func", "my message"); which not only would not fail with compilation errors but would behave really strange with your original code. I never used any of my accounts on stackoverflow and can't write a comment there, hence I'm (ab)using an opportunity to comment here. Just a footnote: isn't methodname rather redundant since you already have __func__ in the LOG_LOCATION? Cheers, Leon
Leon Mlakar wrote:
On 25.08.2015 11:15, Florian Lindner wrote:
I finally got it. Shared the answer on stack overflow:
Thank you for sharing this. I'd just recommend to write macro #define's in such a way that they are syntactically and semantically neutral ... using do { .... macro contents ... } while (false) is a good way to achieve this. For instance, this:
#define logInfo(methodname, message) do { \ LOG_LOCATION; \ BOOST_LOG_SEV(_log, boost::log::trivial::severity_level::trace) << message \ while (false)
would allow constructs such as:
if (something) logInfo("my_func", "my message");
which not only would not fail with compilation errors but would behave really strange with your original code.
Ah, thanks, that sounds wise. Isn't is sufficient to just use curly braces {...} instead of do{..} while (false)?
I never used any of my accounts on stackoverflow and can't write a comment there, hence I'm (ab)using an opportunity to comment here.
I will update my anwser there.
Just a footnote: isn't methodname rather redundant since you already have __func__ in the LOG_LOCATION?
Haha, you're right. I'm designed the macros as a drop in replacement and using the same API. That's why the methodname is there. Best Regards, Florian
Cheers,
Leon
On 26.08.2015 09:51, Florian Lindner wrote:
Leon Mlakar wrote:
On 25.08.2015 11:15, Florian Lindner wrote:
I finally got it. Shared the answer on stack overflow:
Thank you for sharing this. I'd just recommend to write macro #define's in such a way that they are syntactically and semantically neutral ... using do { .... macro contents ... } while (false) is a good way to achieve this. For instance, this:
#define logInfo(methodname, message) do { \ LOG_LOCATION; \ BOOST_LOG_SEV(_log, boost::log::trivial::severity_level::trace) << message \ while (false)
would allow constructs such as:
if (something) logInfo("my_func", "my message");
which not only would not fail with compilation errors but would behave really strange with your original code. Ah, thanks, that sounds wise. Isn't is sufficient to just use curly braces {...} instead of do{..} while (false)? Curly braces (syntactically a block) would cause compilation error in:
if (something) logInfo("my_func", "my message); else whatever; due to an extra empty statement between if and else. Cheers, Leon
Leon Mlakar wrote:
On 25.08.2015 11:15, Florian Lindner wrote:
I finally got it. Shared the answer on stack overflow:
Thank you for sharing this.
I have just discovered that: http://www.boost.org/doc/libs/1_59_0/libs/log/doc/html/boost/log/add_value.h... which seems to do exactly the same without so much code hassle. I wonder why the discoverability of features with boost log is so bad? I was looking for a solution for that standard task to log line numbers for weeks, but never discovered that (and people who wrote all that question postings and answers I read either) The function add_value is not even mentioned in the manual about attributes http://www.boost.org/doc/libs/1_59_0/libs/log/doc/html/log/detailed/attribut...
participants (2)
-
Florian Lindner
-
Leon Mlakar