-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add support for customizing the prefix format #554
Conversation
d7a2913
to
34295dc
Compare
This currently fails the CI step that builds with C++98, which does not provide |
@santigl great work, I really wanted that functionality as well. For the whole C++11 thing - why not just use a function pointer instead of Also, why using |
Thanks for your input, @RippeR37! An Having said that, I have not done any performance testing (I was hoping to get reviews on the overall idea first). But, if benchmarks show that it's faster, I'm happy to add support/make it a raw pointer across all implementations. +1 on the |
Yes, but as far as I know, glog is "static"/global anyway (i.e. you can't have two instances at the same tme) so you can do the same by having global state and a free function which reads that state (instead of using
I did a quick benchmark of overhead of calling function via std::function versus function pointer. In the field it might be bigger or smaller difference (and might depend on compiler/stdlib/switches/...) but in some cases I've seen a difference of function pointers being up to 7x faster then https://quick-bench.com/q/5VlPI3I0QPW3-RT_Ywvc8g3ZT_E
IMHO it would still be better to not waste cpu cycles on enum=>string conversion. Exposing level as an int (0=INFO, 1=WARNING, 2=ERROR, 3=FATAL, verbose(N)=-N) or enum (+ something for verbose levels, e.g. old plain |
That is a quite a big difference, I'll be happy to change the callback parameter to a pointer. For the log levels strings, glog already has an array with the human-readable representations. If we pass a pointer to them there wouldn't be an additional cost, since glog currently indexes into that array to generate the prefix. |
063697a
to
78cbe34
Compare
Thanks for the change! Now it looks like it shouldn't have a noticeable performance regression and does introduce much better flexibility for the users. 👍 |
Thanks for the feedback, @RippeR37! |
I'm not convinced to have a feature to change logging format. |
Personally, I would like to see a customizable log message format. Logging frameworks that I use in other languages can be customized. For an organization that creates software in multiple languages, it can be hugely beneficial (and perhaps a requirement) that messages are logged in a consistent way regardless of which language that software is written in. |
@ukai There are many other logging libraries available in C++, all of which have the functionality to customise logging. This is a sorely missing feature of glog.
Allowing the user to customise the format would make for a really portable, performant and feature rich logging library. What is the downside that concerns you with this feature? |
That'd be great! Thank you, @sergiud. I will address those comments and document the feature in the README. |
8651f88
to
f9c5957
Compare
f9c5957
to
6f3779c
Compare
6f3779c
to
f69afa8
Compare
9a27b4e
to
078d5c5
Compare
If I read this correctly, even with |
Since there are some reservations regarding this feature e.g. from @ukai, the compromise is to declare it experimental and allow future (potentially breaking) changes based on user comments. Meanwhile, users (and package maintainers) still have the ability to enable the option at anytime. Hence, I don't see any issue here. |
078d5c5
to
0cbc235
Compare
This PR proposes a way of adding support to customize the prefix format used by glog.
With it users can declare a callback (
PrefixAttacherFunction(std::ostream&, const LogMessageInfo&)
) that will receive the relevant log details for the line and a stream to write a corresponding prefix.That callback is enabled by passing it to a new overloaded
InitGoogleLogging()
. (The callback cannot be changed once the instance is initialized.)If no callback is specified,
LogMessage
will follow the same path as it does now and write the current hardcoded format.For an example use case, see the unit tests in
src/logging_custom_prefix_unittest.cc
.