syslog and "ISO C++ does not support the %m printf format"

This is a discussion on syslog and "ISO C++ does not support the %m printf format" within the C++ Programming forums, part of the General Programming Boards category; Hello all, I'm compiling my program (using the pedantic, Wall flags). I'm using the syslog facility for logging. The syslog ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262

    syslog and "ISO C++ does not support the %m printf format"

    Hello all,

    I'm compiling my program (using the pedantic, Wall flags). I'm using the syslog facility for logging. The syslog manual specifically specifies that you can use %m in the format string, which will be replaced by the strerror(errno) string. However, when I use the %m, the compiler throws the warning "ISO C++ does not support the %m printf format".
    I reckon they use the specifier in the syslog function declaration that adds the printf-like checks. However, as %m is not valid in printf but it is valid in syslog, this feels wrong to me.
    My question is: how can I work around this? How can I "fix" those warnings without having to hack the syslog.h include file.

    Two side questions: does anybody know what the speed is for syslog? Is it fast? Also, I use it in a multi threaded environment; man pthreads says this is safe, but apparently it's not safe on BSD, where reentrent versions exist for these functions... Is it right that it is thread safe on Linux?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm confused. Shouldn't you be using that %m in a syslog call? Why are you trying to use %m in a printf call?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Sorry for the crappy explanation. I was a bit in a hurry when I wrote my last topic. I'm not using it in a printf call, I am using it in a syslog call. However, this is the declaration (in sys/syslog.h):
    Code:
    extern void syslog (int __pri, __const char *__fmt, ...)
         __attribute__ ((__format__ (__printf__, 2, 3)));
    So, g++ checks whether it's a valid format string for printf. That is what that attribute does. However, it's not a valid printf format string because it allows %m, which is not allowed by printf. That's what it complains about.
    Right now, I modified the header file in /usr/include (I removed the attribute), as I feel that it is actually this header file that is incorrect here. According to the manuals it seems to be at least. And now there aren't any warnings anymore. But do you have any idea on how to fix this properly?

    I mean, I really can't be the only person to get this warning, as I reckon %m in syslog is really common.

    Just an example snippet of my code that results in that warning:
    Code:
    syslog(LOG_WARNING, "accept: %m");
    Thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you get the errors if you take away -pedantic? (I'm guessing the line of thinking was "anyone who's using system calls knows better than to try to compile with -pedantic, so this won't be a problem".)

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by tabstop View Post
    Do you get the errors if you take away -pedantic? (I'm guessing the line of thinking was "anyone who's using system calls knows better than to try to compile with -pedantic, so this won't be a problem".)
    Without -pedantic it doesn't give the warnings. But I prefer to compile with pedantic, as I find it produces quite accurate warnings that often allow me to fix bugs before even testing the application. Also, system calls are nothing that goes against the C standard, are they? So why should you not use pedantic if you do use system calls? I use it all the time and this is the first time it actually gave me a problem.
    Isn't there a compiler flag to turn off this specific type of warning?

    I still feel that it is a bug in the header file to be honest. Or am I wrong?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > I still feel that it is a bug in the header file to be honest. Or am I wrong?
    Yeah, that's what I think as well.
    The __format__ thing makes out that it is exactly like printf, but that isn't true. It extends printf().

    > But do you have any idea on how to fix this properly?
    Nothing easy.
    I guess one view is that benefiting from all the other printf format checks is a good thing, and that the %m thing is a small irritation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by EVOEx View Post
    Without -pedantic it doesn't give the warnings. But I prefer to compile with pedantic, as I find it produces quite accurate warnings that often allow me to fix bugs before even testing the application. Also, system calls are nothing that goes against the C standard, are they? So why should you not use pedantic if you do use system calls? I use it all the time and this is the first time it actually gave me a problem.
    Isn't there a compiler flag to turn off this specific type of warning?

    I still feel that it is a bug in the header file to be honest. Or am I wrong?
    The difference with -pedantic is the attitude that "if the standard doesn't say it, it doesn't exist". For instance, I doubt that there's a printf() out there anywhere that has just what the standard says it should have, and no more -- so using, say "%'d" is fine on a *nix system, because that's posix, but -pedantic will throw a fit. It's the same deal here -- the compiler knows what's going on, but you specifically told it that it can't know, so it has to throw the warning.

    To turn off printf-format-checking, you can use -Wno-format. (Of course, that'll kill all format warnings, not just this one.)

    I don't know whether anyone considered what would happen with -pedantic on when the implementors added that bit. (I don't have that line in my file here on the Mac, so I can't really check.) You can check the BUGS section on the bottom of your man page to see what it says; and you can fire off an e-mail about it to the implementors (who would probably like to know, if they don't already, even if they don't end up changing it).

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by tabstop View Post
    The difference with -pedantic is the attitude that "if the standard doesn't say it, it doesn't exist". For instance, I doubt that there's a printf() out there anywhere that has just what the standard says it should have, and no more -- so using, say "%'d" is fine on a *nix system, because that's posix, but -pedantic will throw a fit. It's the same deal here -- the compiler knows what's going on, but you specifically told it that it can't know, so it has to throw the warning.
    Thank you all for your answers.
    I quote your first paragraph here, because I disagree with it. Let me explain. Pedantic should complain on functions use that isn't supported by what the standard says, even though it is supported in your OS. However, I don't believe the syslog facility is part of the standard. It's an extension in Linux and several other OSes. That means that it's nothing different from if you write it yourself, include that header file, and link it with another object file. The standard doesn't say anything about syslog(), so it doesn't say anything about it's format string, so the compiler is not supposed to complain in pedantic mode.

    To be honest I do understand the idea of the coders. It's the choice of either no checks at all, or a not completely valid check. I think they're bound to know about this, as I can't be the only one who tried to use syslog() using pedantic mode, right?
    -Wno-format worked like a charm though. No checks at all for format strings, but those are hardly ever the source for bugs, at least in my code. My bugs are more commonly typos that just happen to work properly.
    So for me, it works fine. Thank you very much.

    My own personal rule for coding is: there may not be a single warning from a compiler when I compile it. Though right now I achieved that by turning off warning messages. Hmmmm... Well, it'll have to do here.


    Thanks all.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by EVOEx View Post
    Thank you all for your answers.
    I quote your first paragraph here, because I disagree with it. Let me explain. Pedantic should complain on functions use that isn't supported by what the standard says, even though it is supported in your OS. However, I don't believe the syslog facility is part of the standard. It's an extension in Linux and several other OSes. That means that it's nothing different from if you write it yourself, include that header file, and link it with another object file. The standard doesn't say anything about syslog(), so it doesn't say anything about it's format string, so the compiler is not supposed to complain in pedantic mode.
    Well, yes. My point was more that "this function (rightly or wrongly) acts like it passes %m to printf", so when you use it you (rightly or wrongly) use %m with printf, which causes the whole big mess. (Of course it doesn't actually do so, I'm pretty sure, which is why it's so annoying.)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21