From 91ac79b7526364444ffce68d00e301987d445232 Mon Sep 17 00:00:00 2001
From: Grzegorz Sluja <grzegorz.sluja@iopsys.eu>
Date: Tue, 15 Sep 2020 13:12:00 +0200
Subject: [PATCH] Handle debug level by libs in easy_soc_libs

LIBWIFI_DEBUG_LEVEL env variable is created and can be set/get by the
ubus call. All libs in easy_soc_libs have been updated with new
functions
which are printed based on its level.

Debug_level is implemented with the following values
(the same as the linux kernel's log level):
ERR   = 3
WARN  = 4
INFO  = 6
DBG   = 7

Signed-off-by: Grzegorz Sluja <grzegorz.sluja@iopsys.eu>
---
 bcm/bcm.c  | 20 ++++++++++----------
 ethernet.c | 12 ++++++------
 ethernet.h |  5 +++++
 ethsw.c    |  2 +-
 4 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/bcm/bcm.c b/bcm/bcm.c
index 4133430..222db02 100644
--- a/bcm/bcm.c
+++ b/bcm/bcm.c
@@ -65,7 +65,7 @@ static int bcm_eth_get_unit_port(const char *ifname, int *unit, int *port)
 	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
 						sizeof(struct ethswctl_data));
 	if (ret != 0) {
-		fprintf(stderr, "ioctl failed! ret = %d\n", ret);
+		libethernet_err("ioctl failed! ret = %d\n", ret);
 		return -1;
 	}
 
@@ -83,7 +83,7 @@ static int bcm_eth_get_unit_port(const char *ifname, int *unit, int *port)
 		}
 	}
 
-	/* fprintf(stderr, "[%s] unit = %d port = %d portmap = 0x%x "
+	/* libethernet_dbg("[%s] unit = %d port = %d portmap = 0x%x "
 			"phyportmap = 0x%x\n",
 			ifname, *unit, *port, data.port_map,
 			data.phy_portmap); */
@@ -111,7 +111,7 @@ int bcm_eth_get_link_settings(const char *ifname, struct eth_link *link)
 	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
 						sizeof(struct ethswctl_data));
 	if (ret != 0) {
-		fprintf(stderr, "ioctl failed!\n");
+		libethernet_err("ioctl failed!\n");
 		return -1;
 	}
 
@@ -119,7 +119,7 @@ int bcm_eth_get_link_settings(const char *ifname, struct eth_link *link)
 	link->speed = data.speed;
 	link->fullduplex = data.duplex == 1 ? false : true;
 
-	/* fprintf(stderr, "port: %d  speed = %d  fullduplex = %d\n",
+	/* libethernet_dbg("port: %d  speed = %d  fullduplex = %d\n",
 			link->portid, link->speed, link->fullduplex); */
 
 	if (!!(data.phycfg & PHY_CFG_1000FD))
@@ -156,12 +156,12 @@ int bcm_eth_get_link_settings(const char *ifname, struct eth_link *link)
 	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
 						sizeof(struct ethswctl_data));
 	if (ret != 0) {
-		fprintf(stderr, "ioctl failed! ret = %d\n", ret);
+		libethernet_err("ioctl failed! ret = %d\n", ret);
 		return -1;
 	}
 
 	link->autoneg = data.autoneg_info == 0 ? false : true;
-	/* fprintf(stderr, "autoneg = %d\n", link->autoneg); */
+	/* libethernet_dbg("autoneg = %d\n", link->autoneg); */
 
 	memset(&data, 0, sizeof(struct ethswctl_data));
 	data.op = ETHSWLINKSTATUS;
@@ -172,7 +172,7 @@ int bcm_eth_get_link_settings(const char *ifname, struct eth_link *link)
 	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
 						sizeof(struct ethswctl_data));
 	if (ret != 0) {
-		fprintf(stderr, "ioctl failed!\n");
+		libethernet_err("ioctl failed!\n");
 		return -1;
 	}
 
@@ -183,7 +183,7 @@ int bcm_eth_get_link_settings(const char *ifname, struct eth_link *link)
 
 int bcm_eth_set_link_settings(const char *name, struct eth_link link)
 {
-	fprintf(stderr, "%s(): TODO\n", __func__);
+	libethernet_err("%s(): TODO\n", __func__);
 	return 0;
 }
 
@@ -300,7 +300,7 @@ int bcm_eth_get_stats(const char *ifname, struct eth_stats *s)
 	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
 						sizeof(struct ethswctl_data));
 	if (ret != 0) {
-		fprintf(stderr, "ioctl failed! ret = %d\n", ret);
+		libethernet_err("ioctl failed! ret = %d\n", ret);
 		return ret_proc == 0 ? 0 : -1;
 	}
 
@@ -354,7 +354,7 @@ int bcm_eth_get_rmon_stats(const char *ifname, struct eth_rmon_stats *rmon)
 	ret = eth_ioctl(ifname, SIOCETHSWCTLOPS, &data,
 						sizeof(struct ethswctl_data));
 	if (ret != 0) {
-		fprintf(stderr, "ioctl failed! ret = %d\n", ret);
+		libethernet_err("ioctl failed! ret = %d\n", ret);
 		return -1;
 	}
 
diff --git a/ethernet.c b/ethernet.c
index 8e151f7..ea30513 100644
--- a/ethernet.c
+++ b/ethernet.c
@@ -186,7 +186,7 @@ int eth_ioctl(const char *ifname, int cmd, void *in, int len)
 		return -1;
 
 	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
-		fprintf(stderr, "SIOCGIFINDEX failed!\n");
+		libethernet_err("SIOCGIFINDEX failed!\n");
 		close(s);
 		return -1;
 	}
@@ -216,7 +216,7 @@ int eth_mii_get_phy_id(const char *ifname, int port, int *phy_id)
 	memset(&ifr, 0, sizeof(struct ifreq));
 	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
 	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
-		fprintf(stderr, "SIOCGIFINDEX failed!\n");
+		libethernet_err("SIOCGIFINDEX failed!\n");
 		close(s);
 		return -1;
 	}
@@ -226,7 +226,7 @@ int eth_mii_get_phy_id(const char *ifname, int port, int *phy_id)
 	mii->val_in = port;
 
 	if (ioctl(s, SIOCGMIIPHY, &ifr) < 0) {
-		fprintf(stderr, "SIOCGMIIPHY failed!\n");
+		libethernet_err("SIOCGMIIPHY failed!\n");
 		close(s);
 		return -1;
 	}
@@ -234,7 +234,7 @@ int eth_mii_get_phy_id(const char *ifname, int port, int *phy_id)
 	if (phy_id)
 		*phy_id = mii->phy_id;
 
-	//fprintf(stderr, "%s: phy_id = %d\n", ifname, *phy_id);
+	//libethernet_dbg("%s: phy_id = %d\n", ifname, *phy_id);
 	close(s);
 	return 0;
 }
@@ -253,7 +253,7 @@ static int eth_mii_ioctl(const char *ifname, int cmd, int phy_id, int reg,
 	memset(&ifr, 0, sizeof(struct ifreq));
 	strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
 	if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
-		fprintf(stderr, "SIOCGIFINDEX failed!\n");
+		libethernet_err("SIOCGIFINDEX failed!\n");
 		close(s);
 		return -1;
 	}
@@ -267,7 +267,7 @@ static int eth_mii_ioctl(const char *ifname, int cmd, int phy_id, int reg,
 	mii->val_in = in;
 
 	if (ioctl(s, cmd, &ifr) < 0) {
-		fprintf(stderr, "MII cmd on %s failed\n", ifr.ifr_name);
+		libethernet_err("MII cmd on %s failed\n", ifr.ifr_name);
 		close(s);
 		return -1;
 	}
diff --git a/ethernet.h b/ethernet.h
index 3cb186d..8121dd8 100644
--- a/ethernet.h
+++ b/ethernet.h
@@ -30,6 +30,11 @@
 extern "C" {
 #endif
 
+#define libethernet_err(...)	err("libethernet: " __VA_ARGS__)
+#define libethernet_warn(...)	warn("libethernet: " __VA_ARGS__)
+#define libethernet_info(...)	info("libethernet: " __VA_ARGS__)
+#define libethernet_dbg(...)	dbg("libethernet: " __VA_ARGS__)
+
 /* enum eth_duplex - duplex modes */
 enum eth_duplex {
 	AUTO_DUPLEX,
diff --git a/ethsw.c b/ethsw.c
index ef0f9c4..16eb43d 100644
--- a/ethsw.c
+++ b/ethsw.c
@@ -96,7 +96,7 @@ int ethsw_set_link_settings(char *name, struct eth_link link)
 	struct switch_port_link l;
 	int port = ETH_PORT_UNDEFINED;
 
-	fprintf(stderr, "%s(): name = %s\n", __func__, name);
+	libethernet_dbg("%s(): name = %s\n", __func__, name);
 	if (link.portid != ETH_PORT_UNDEFINED)
 		port = link.portid;
 
-- 
GitLab