Skip to content
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

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

Closed
magiruuvelvet opened this issue Oct 28, 2023 · 3 comments
Closed

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

magiruuvelvet opened this issue Oct 28, 2023 · 3 comments
Labels
question Further information is requested

Comments

@magiruuvelvet
Copy link

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
Copy link
Owner

odygrd commented Oct 31, 2023

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);

@odygrd odygrd added the question Further information is requested label Oct 31, 2023
@magiruuvelvet
Copy link
Author

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
Copy link
Owner

odygrd commented Nov 1, 2023

You're welcome, closing this for now

@odygrd odygrd closed this as completed Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants