Closed magiruuvelvet closed 1 year ago
I don't think it will be easy to support this. The format string has to be compile time string and can not be dynamic.
You can use dynamic strings in the log message if you use {}
for the format and then just pass the dynamic string as an argument
Have you tried something like the below ? It is not good enough ?
std::string const sentence = "files";
size_t num_files = 1;
LOG_INFO(logger, "{} {}", num_files, sentence);
size_t num_files = 2;
LOG_INFO(logger, "{} {}", num_files, sentence);
How did I not think about using a format string? 😁 I currently use an if-else block where I check the count variable and do a separate logger invocation in each block, but using a format string is a neat idea too. If I ever decide to translate the log file of my application, this works:
LOG_INFO(logger, "{}", i18n_library_call_here("message_id", variables...));
In comparison to this, which works just fine if only supporting English:
if (count == 1)
{
LOG_INFO(logger, "processing {} file", count);
}
else
{
LOG_INFO(logger, "processing {} files", count));
}
Thank you very much.
You're welcome, closing this for now
I've run into a scenario where I would like to have the ability to use a runtime string as the log message (i18n pluralization). I can work around the compile-time requirement, but the code doesn't look nice anymore. The log file only supports English, but having correct grammar and pluralization rules would still be nice.
eg:
1 files
(wrong grammar) vs2 files
It's only a minor annoyance. Feel free to close this, if it is out of scope for this project.