linux-can.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
To: linux-can@vger.kernel.org
Cc: Oliver Hartkopp <socketcan@hartkopp.net>,
	Marc Kleine-Budde <mkl@pengutronix.de>,
	Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Subject: [PATCH can-utils-dev 2/5] lib: add pr_debug() macro
Date: Sun, 13 Nov 2022 17:53:18 +0900	[thread overview]
Message-ID: <20221113085321.87910-3-mailhol.vincent@wanadoo.fr> (raw)
In-Reply-To: <20221113085321.87910-1-mailhol.vincent@wanadoo.fr>

Add the pr_debug() macro so that replace:

  #ifdef DEBUG
  	printf("foo");
  #endif

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.

CC: Marc Kleine-Budde <mkl@pengutronix.de>
Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
---
@Marc, was there any reasons to print debug information to stderr and
not stdout in mcp251xfd-dev-coredump.c?
---
 canbusload.c                       |  9 +++------
 candump.c                          | 16 ++++------------
 canlogserver.c                     | 12 ++++--------
 lib.h                              |  6 ++++++
 mcp251xfd/mcp251xfd-dev-coredump.c | 10 +---------
 slcanpty.c                         | 10 +++-------
 6 files changed, 21 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..1cb1dd4 100644
--- a/lib.h
+++ b/lib.h
@@ -47,6 +47,12 @@
 
 #include <stdio.h>
 
+#ifdef DEBUG
+#define pr_debug(fmt, args...) printf(fmt, ##args)
+#else
+#define pr_debug(...)
+#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..3eba07e 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


  parent reply	other threads:[~2022-11-13  8:53 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-13  8:53 [PATCH can-utils-dev 0/5] can-utils-dev: a set of cleanup patches Vincent Mailhol
2022-11-13  8:53 ` [PATCH can-utils-dev 1/5] slcanpty: remove redundant asc2nibble() Vincent Mailhol
2022-11-13  8:53 ` Vincent Mailhol [this message]
2022-11-13 12:04   ` [PATCH can-utils-dev 2/5] lib: add pr_debug() macro Marc Kleine-Budde
2022-11-13 13:36     ` Vincent MAILHOL
2022-11-13 13:03   ` Marc Kleine-Budde
2022-11-13 13:19     ` Vincent MAILHOL
2022-11-13 13:34       ` Marc Kleine-Budde
2022-11-13 13:47         ` Vincent MAILHOL
2022-11-13 13:56           ` Marc Kleine-Budde
2022-11-13 14:17             ` Vincent MAILHOL
2022-11-13  8:53 ` [PATCH can-utils-dev 3/5] candump: add global variable progname Vincent Mailhol
2022-11-13  8:53 ` [PATCH can-utils-dev 4/5] candump: use linux/net_tstamp.h instead of redefining values ourselves Vincent Mailhol
2022-11-13 20:05   ` Oliver Hartkopp
2022-11-14  5:25     ` Vincent MAILHOL
2022-11-14  8:25       ` Oliver Hartkopp
2022-11-13  8:53 ` [PATCH can-utils-dev 5/5] lib: snprintf_can_error_frame: print counter errors if CAN_ERR_CNT is set Vincent Mailhol
2022-11-13 12:05 ` [PATCH can-utils-dev 0/5] can-utils-dev: a set of cleanup patches Marc Kleine-Budde
2022-11-13 16:18 ` [PATCH can-utils-dev v2 " Vincent Mailhol
2022-11-13 16:18   ` [PATCH can-utils-dev v2 1/5] slcanpty: remove redundant asc2nibble() Vincent Mailhol
2022-11-13 16:18   ` [PATCH can-utils-dev v2 2/5] lib: add pr_debug() macro Vincent Mailhol
2022-11-13 16:18   ` [PATCH can-utils-dev v2 3/5] lib: snprintf_can_error_frame: print counter errors if CAN_ERR_CNT is set Vincent Mailhol
2022-11-13 16:18   ` [PATCH can-utils-dev v2 4/5] candump: add global variable progname Vincent Mailhol
2022-11-13 16:18   ` [PATCH can-utils-dev v2 5/5] candump: use linux/net_tstamp.h instead of redefining values ourselves Vincent Mailhol
2022-11-14 16:38 ` [PATCH can-utils-dev v3 0/6] can-utils-dev: a set of update and cleanup patches Vincent Mailhol
2022-11-14 16:38   ` [PATCH can-utils-dev v3 1/6] slcanpty: remove redundant asc2nibble() Vincent Mailhol
2022-11-14 16:38   ` [PATCH can-utils-dev v3 2/6] include: update linux/can/error.h Vincent Mailhol
2022-11-14 16:38   ` [PATCH can-utils-dev v3 3/6] lib: snprintf_can_error_frame: print counter errors if CAN_ERR_CNT is set Vincent Mailhol
2022-11-14 16:38   ` [PATCH can-utils-dev v3 4/6] lib: add pr_debug() macro Vincent Mailhol
2022-11-14 16:38   ` [PATCH can-utils-dev v3 5/6] candump: add global variable progname Vincent Mailhol
2022-11-14 16:38   ` [PATCH can-utils-dev v3 6/6] candump: use linux/net_tstamp.h instead of redefining values ourselves Vincent Mailhol
2022-11-14 18:15   ` [PATCH can-utils-dev v3 0/6] can-utils-dev: a set of update and cleanup patches Oliver Hartkopp
2022-11-15  8:15   ` Marc Kleine-Budde
2022-11-15  8:57     ` Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221113085321.87910-3-mailhol.vincent@wanadoo.fr \
    --to=mailhol.vincent@wanadoo.fr \
    --cc=linux-can@vger.kernel.org \
    --cc=mkl@pengutronix.de \
    --cc=socketcan@hartkopp.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).