Add the pr_debug() macro so that: #ifdef DEBUG printf("foo"); #endif can be replaced by: pr_debug("foo"); Apply the pr_debug() macro wherever relevant. Currently, there is no consensus whether debug messages should be printed on stdout or stderr. Most of the modules: canbusload.c, candump.c and canlogserver.c use stdout but mcp251xfd/mcp251xfd-dev-coredump.c uses stderr. Harmonize the behavior by following the major trend and make mcp251xfd/mcp251xfd-dev-coredump.c also output to stdout. slcanpty.c does a #define DEBUG, meaning that debug is always turned on for this file. Remove this and make debug an option like every other files. Signed-off-by: Vincent Mailhol <mailhol.vincent@xxxxxxxxxx> --- canbusload.c | 9 +++------ candump.c | 16 ++++------------ canlogserver.c | 12 ++++-------- lib.h | 7 +++++++ mcp251xfd/mcp251xfd-dev-coredump.c | 10 +--------- slcanpty.c | 10 +++------- 6 files changed, 22 insertions(+), 42 deletions(-) diff --git a/canbusload.c b/canbusload.c index e4dfc02..47b62fd 100644 --- a/canbusload.c +++ b/canbusload.c @@ -61,6 +61,7 @@ #include <linux/can.h> #include <linux/can/raw.h> +#include "lib.h" #include "terminal.h" #include "canframelen.h" @@ -310,9 +311,7 @@ int main(int argc, char **argv) return 1; } -#ifdef DEBUG - printf("open %d '%s'.\n", i, ptr); -#endif + pr_debug("open %d '%s'.\n", i, ptr); s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (s[i] < 0) { @@ -358,10 +357,8 @@ int main(int argc, char **argv) if (nbytes > max_bitrate_len) max_bitrate_len = nbytes; /* for nice printing */ + pr_debug("using interface name '%s'.\n", ifr.ifr_name); -#ifdef DEBUG - printf("using interface name '%s'.\n", ifr.ifr_name); -#endif /* try to switch the socket into CAN FD mode */ const int canfd_on = 1; setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on)); diff --git a/candump.c b/candump.c index 6fe30bb..4a239e7 100644 --- a/candump.c +++ b/candump.c @@ -216,9 +216,7 @@ static int idx2dindex(int ifidx, int socket) strcpy(devname[i], ifr.ifr_name); -#ifdef DEBUG - printf("new index %d (%s)\n", i, devname[i]); -#endif + pr_debug("new index %d (%s)\n", i, devname[i]); return i; } @@ -474,9 +472,7 @@ int main(int argc, char **argv) ptr = argv[optind+i]; nptr = strchr(ptr, ','); -#ifdef DEBUG - printf("open %d '%s'.\n", i, ptr); -#endif + pr_debug("open %d '%s'.\n", i, ptr); obj->s = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (obj->s < 0) { @@ -510,9 +506,7 @@ int main(int argc, char **argv) memset(&ifr.ifr_name, 0, sizeof(ifr.ifr_name)); strncpy(ifr.ifr_name, ptr, nbytes); -#ifdef DEBUG - printf("using interface name '%s'.\n", ifr.ifr_name); -#endif + pr_debug("using interface name '%s'.\n", ifr.ifr_name); if (strcmp(ANYDEV, ifr.ifr_name) != 0) { if (ioctl(obj->s, SIOCGIFINDEX, &ifr) < 0) { @@ -602,9 +596,7 @@ int main(int argc, char **argv) /* try SO_RCVBUFFORCE first, if we run with CAP_NET_ADMIN */ if (setsockopt(obj->s, SOL_SOCKET, SO_RCVBUFFORCE, &rcvbuf_size, sizeof(rcvbuf_size)) < 0) { -#ifdef DEBUG - printf("SO_RCVBUFFORCE failed so try SO_RCVBUF ...\n"); -#endif + pr_debug("SO_RCVBUFFORCE failed so try SO_RCVBUF ...\n"); if (setsockopt(obj->s, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(rcvbuf_size)) < 0) { perror("setsockopt SO_RCVBUF"); diff --git a/canlogserver.c b/canlogserver.c index 6425ca4..51d548f 100644 --- a/canlogserver.c +++ b/canlogserver.c @@ -145,9 +145,7 @@ int idx2dindex(int ifidx, int socket) strcpy(devname[i], ifr.ifr_name); -#ifdef DEBUG - printf("new index %d (%s)\n", i, devname[i]); -#endif + pr_debug("new index %d (%s)\n", i, devname[i]); return i; } @@ -310,11 +308,9 @@ int main(int argc, char **argv) for (i=0; i<currmax; i++) { -#ifdef DEBUG - printf("open %d '%s' m%08X v%08X i%d e%d.\n", - i, argv[optind+i], mask[i], value[i], - inv_filter[i], err_mask[i]); -#endif + pr_debug("open %d '%s' m%08X v%08X i%d e%d.\n", + i, argv[optind+i], mask[i], value[i], + inv_filter[i], err_mask[i]); if ((s[i] = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { perror("socket"); diff --git a/lib.h b/lib.h index a4d3ce5..6cc58a2 100644 --- a/lib.h +++ b/lib.h @@ -47,6 +47,13 @@ #include <stdio.h> +#ifdef DEBUG +#define pr_debug(fmt, args...) printf(fmt, ##args) +#else +__attribute__((format (printf, 1, 2))) +static inline int pr_debug(const char* fmt, ...) {return 0;} +#endif + /* buffer sizes for CAN frame string representations */ #define CL_ID (sizeof("12345678##1")) diff --git a/mcp251xfd/mcp251xfd-dev-coredump.c b/mcp251xfd/mcp251xfd-dev-coredump.c index 5874d24..422900f 100644 --- a/mcp251xfd/mcp251xfd-dev-coredump.c +++ b/mcp251xfd/mcp251xfd-dev-coredump.c @@ -17,18 +17,10 @@ #include <linux/kernel.h> +#include "../lib.h" #include "mcp251xfd.h" #include "mcp251xfd-dump-userspace.h" -#define pr_err(fmt, args...) fprintf(stderr, fmt, ##args) -#define pr_no(fmt, args...) while (0) { fprintf(stdout, fmt, ##args); } - -#ifdef DEBUG -#define pr_debug(fmt, args...) pr_err(fmt, ##args) -#else -#define pr_debug(fmt, args...) pr_no(fmt, ##args) -#endif - struct mcp251xfd_dump_iter { const void *start; diff --git a/slcanpty.c b/slcanpty.c index 4ac9e8a..fa97cd6 100644 --- a/slcanpty.c +++ b/slcanpty.c @@ -49,8 +49,6 @@ #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1) #define DEVICE_NAME_PTMX "/dev/ptmx" -#define DEBUG - /* read data from pty, send CAN frames to CAN socket and answer commands */ int pty2can(int pty, int socket, struct can_filter *fi, int *is_open, int *tstamp) @@ -106,14 +104,12 @@ rx_restart: cmd = buf[0]; buf[nbytes] = 0; -#ifdef DEBUG for (tmp = 0; tmp < nbytes; tmp++) if (buf[tmp] == '\r') - putchar('@'); + pr_debug("@"); else - putchar(buf[tmp]); - printf("\n"); -#endif + pr_debug("%c", buf[tmp]); + pr_debug("\n"); /* check for filter configuration commands */ if (cmd == 'm' || cmd == 'M') { -- 2.37.4