diff options
author | Ralf Baechle <ralf@linux-mips.org> | 2013-06-17 14:40:56 +0200 |
---|---|---|
committer | Ralf Baechle <ralf@linux-mips.org> | 2013-06-17 14:40:56 +0200 |
commit | 1d0442da72fc734f12767ad019010ccb7e0d12cd (patch) | |
tree | 770213ecbaa2e326b26a40ec15531fa4925b18c2 /ax25ipd | |
parent | 7cc473aed830db1395ed0d0701791889a6e96e4f (diff) |
ax25ipd: Make LOGx() macros bulletproof.
The LOGL1..LOGL4 macros were defined like:
Now consider an invocation like:
if (condition)
LOGL1(...);
else
something_different();
CPP will expand this like:
if (condition)
if (loglevel>0)
syslog(LOG_DAEMON | LOG_WARNING, ...);
else
something_different();
That is the else would now be considered associated with the wrong if.
Macro arguments may also not have been evaluated on every invocation
making the use of these function-like looking macros not function like.
For example:
LOGL1("%d", i++);
would be expanded to
if (loglevel>0)
syslog(LOG_DAEMON | LOG_WARNING, i++);
That is depending on the value of loglevel i++ would only be incremented if
syslog was actually being called.
Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
Diffstat (limited to 'ax25ipd')
-rw-r--r-- | ax25ipd/Makefile.am | 1 | ||||
-rw-r--r-- | ax25ipd/ax25ipd.h | 10 | ||||
-rw-r--r-- | ax25ipd/syslog.c | 16 |
3 files changed, 23 insertions, 4 deletions
diff --git a/ax25ipd/Makefile.am b/ax25ipd/Makefile.am index 6994c68..efc3c14 100644 --- a/ax25ipd/Makefile.am +++ b/ax25ipd/Makefile.am @@ -40,6 +40,7 @@ ax25ipd_SOURCES = \ ax25ipd.h \ process.c \ routing.c \ + syslog.c \ bpqether.c # Needed so that install is optional diff --git a/ax25ipd/ax25ipd.h b/ax25ipd/ax25ipd.h index 06efd58..b088247 100644 --- a/ax25ipd/ax25ipd.h +++ b/ax25ipd/ax25ipd.h @@ -111,10 +111,12 @@ struct { #define MAX_FRAME 2048 -#define LOGL1(arg...) if(loglevel>0)syslog(LOG_DAEMON | LOG_WARNING, ##arg) -#define LOGL2(arg...) if(loglevel>1)syslog(LOG_DAEMON | LOG_WARNING, ##arg) -#define LOGL3(arg...) if(loglevel>2)syslog(LOG_DAEMON | LOG_WARNING, ##arg) -#define LOGL4(arg...) if(loglevel>3)(void)syslog(LOG_DAEMON | LOG_DEBUG, ##arg) +extern void LOGLn(int level, const char *str, ...); + +#define LOGL1(arg...) LOGLn(1, ##arg) +#define LOGL2(arg...) LOGLn(2, ##arg) +#define LOGL3(arg...) LOGLn(3, ##arg) +#define LOGL4(arg...) LOGLn(4, ##arg) #define AXRT_BCAST 1 #define AXRT_DEFAULT 2 diff --git a/ax25ipd/syslog.c b/ax25ipd/syslog.c new file mode 100644 index 0000000..493e21f --- /dev/null +++ b/ax25ipd/syslog.c @@ -0,0 +1,16 @@ +#include <stdarg.h> +#include <syslog.h> + +#include "ax25ipd.h" + +void LOGLn(int level, const char *format, ...) +{ + va_list va; + + va_start(va, format); + + if (loglevel >= level) + vsyslog(LOG_DAEMON | LOG_WARNING, format, va); + + va_end(va); +} |