PlatformLab / NanoLog

Nanolog is an extremely performant nanosecond scale logging system for C++ that exposes a simple printf-like API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Log with a single "%" cannot compile

bjmiao opened this issue · comments

Hi,

Everything else as usual, the following code

NANO_LOG(NOTICE, "%")

will cause compilation errors. I believe this is due to the fact that the syntax analyzer uses "%" as an indicator of log parameters, and here is complaining the missing of variable.

I'm not sure this is a feature or a bug, but in stdio, printf("%") is valid.

I admit that I found this phenomenon by chance (I forgot to delete a '%' when modifying my program), but if it is not of your expectation, maybe you can consider supporting outputing a single "%" character? (Nevertheless, it may add difficulty to the syntax analyzer I guess)

btw, I like you project so much! It helps me a lot.

Best,
bjmiao

Actually, this is intended behavior. A lone “%” is typically undefined behavior in most printf implementations.

If you enable -Wformat or -Wall in your compiler, you should see that a lone ‘%’ will result in warnings. You can execute an example here: http://cpp.sh/8lozo . If you click 'Run' and look at the 'compilation' tab, you should see the following warnings:

In function 'int main()':
8:27: warning: unknown conversion type character 'W' in format [-Wformat=]
9:15: warning: spurious trailing '%' in format [-Wformat=]

To print a single percent sign by itself, you have to escape it with another percent. Example: NANO_LOG(ERROR, “100%% True”);

I see! Thanks for explanation (also a good lesson on my C++ basics! 👍 )