odygrd / quill

Asynchronous Low Latency C++ Logging Library
MIT License
1.55k stars 158 forks source link

[question] no support for runtime strings in log message? #361

Closed magiruuvelvet closed 1 year ago

magiruuvelvet commented 1 year ago

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) vs 2 files

It's only a minor annoyance. Feel free to close this, if it is out of scope for this project.

odygrd commented 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);
magiruuvelvet commented 1 year ago

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.

odygrd commented 1 year ago

You're welcome, closing this for now