Skip to content

Conversation

dloman
Copy link

@dloman dloman commented Sep 22, 2025

Added convience function which avoid str format until after log has been checked.

logger.log(toolbelt::LogLevel::Error, "{} {}"_format("foo", "bar"));

goes to

logger.error("{} {}", "foo", "bar");

@dloman
Copy link
Author

dloman commented Sep 22, 2025

realized logger has capital function names. Do you want me to change to Error, Info etc?

@dallison
Copy link
Owner

Yes please, Google style FTW

Log(LogLevel::kERROR, std::move(str));
}
inline void Fatal(std::string&& str) {
Log(LogLevel::kFATAL, std::move(str));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't really a benefit to these rvalue overloads when the underlying function does not swallow up that object and thus can only really use it as a const-ref.

if (!_enabled || LogLevel::kVERBOSE_DEBUG < min_level_) {
return;
}
Log(LogLevel::kVERBOSE_DEBUG, fmt::format(fmt, std::forward<Ts>(args)...));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this file is going to be using fmt, it needs to include the relevant headers and dependency in the bazel target.

if (!_enabled || LogLevel::kDBG < min_level_) {
return;
}
Log(LogLevel::DBG, fmt::format(fmt, std::forward<Ts>(args)...));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a weird that when the user calls Log(lvl, str, a, b, c) (i.e., the vararg version) it gets formatted, under-the-hood, with the "printf" formatting, but then, if they call Info(str, a, b, c) it gets formatted with the python-style fmt specifications.

N.B.: I much prefer the fmt version, but I think it needs to be consistent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of this. In fact, in my own company's logging library, we have similar perfect forwarding of the fmt+args down to the logging functions to present the user with a similarly convenient syntax.

One big design feature that I would very much recommend (and might be able to provide the code for), is to perfectly forward all the way down to the implementation of Log / VLog functions because that will allow for logging functions that do not require any dynamic memory allocation, which is very handy in real-time and signal handler contexts, and quite easy to do with the fmt library. I did this very easily, and I can remove malloc/free in some contexts and still log messages to stdout/stderr.

But, this will require rewriting Log/VLog completely. So, maybe something for @dallison to think about.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats a good call. Will update. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants