From 551dcb62f7151ab41292b89a7bf96dd84c7a1cc9 Mon Sep 17 00:00:00 2001
From: Lemmy Huang <huangliming5@huawei.com>
Date: Wed, 24 Jul 2024 10:30:24 +0800
Subject: [PATCH] cleancode: rename log level

  LWIPGZ_LOG_DEBUG
  LWIPGZ_LOG_INFO
  LWIPGZ_LOG_WARNING
  LWIPGZ_LOG_ERR
  LWIPGZ_LOG_FATAL

Signed-off-by: Lemmy Huang <huangliming5@huawei.com>
---
 src/api/lwipgz_sock.c    |  4 ++--
 src/api/sockets.c        |  2 +-
 src/api/sys_arch.c       | 10 +++++-----
 src/core/tcp.c           | 22 +++++++++++-----------
 src/core/tcp_in.c        |  6 +++---
 src/include/lwipgz_log.h | 26 +++++++++++++-------------
 src/include/lwipopts.h   | 25 +++++++++++++++++++++----
 7 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/src/api/lwipgz_sock.c b/src/api/lwipgz_sock.c
index 96f54bc..5419057 100644
--- a/src/api/lwipgz_sock.c
+++ b/src/api/lwipgz_sock.c
@@ -161,7 +161,7 @@ void netconn_set_sndbufsize(struct netconn *conn, tcpwnd_size_t sndbufsize)
   struct tcp_pcb *tcp = conn->pcb.tcp;
   
   if (sndbufsize > TCP_SND_BUF_MAX) {
-    LWIP_DEBUGF(GAZELLE_DEBUG_WARNING,
+    LWIP_DEBUGF(LWIPGZ_LOG_WARNING,
                 ("netconn_set_sndbufsize: setting sndbufsize exceed TCP_SND_BUF_MAX. "
                  "sndbufsize=%u, snd_buf_max=%u", sndbufsize, TCP_SND_BUF_MAX));
     return;
@@ -173,7 +173,7 @@ void netconn_set_sndbufsize(struct netconn *conn, tcpwnd_size_t sndbufsize)
   }
   /* remaining snd_buf less than the mount to be reduced */
   if (tcp->snd_buf < tcp->snd_buf_max - sndbufsize) {
-    LWIP_DEBUGF(GAZELLE_DEBUG_WARNING,
+    LWIP_DEBUGF(LWIPGZ_LOG_WARNING,
                 ("netconn_set_sndbufsize: setting sndbufsize too small. "
                  "snd_buf available is %u, need reduce is %u\n", tcp->snd_buf,
                  tcp->snd_buf_max - sndbufsize));
diff --git a/src/api/sockets.c b/src/api/sockets.c
index 7d3d131..69c3086 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -2695,7 +2695,7 @@ event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
       break;
     case NETCONN_EVT_ERROR:
       if ((conn->pending_err != ERR_OK) && (conn->pending_err != ERR_RST)) {
-          LWIP_DEBUGF(GAZELLE_DEBUG_WARNING, ("event_callback: have errevent, err=%d, fd=%d\n", conn->pending_err, conn->callback_arg.socket));
+          LWIP_DEBUGF(LWIPGZ_LOG_WARNING, ("event_callback: have errevent, err=%d, fd=%d\n", conn->pending_err, conn->callback_arg.socket));
       }
       sock->errevent = 1;
 #if GAZELLE_ENABLE
diff --git a/src/api/sys_arch.c b/src/api/sys_arch.c
index dcfd0a4..723ec9b 100644
--- a/src/api/sys_arch.c
+++ b/src/api/sys_arch.c
@@ -97,14 +97,14 @@ int thread_create(const char *name, unsigned id, thread_fn func, void *arg)
 
     ret = pthread_create(&tid, NULL, func, arg);
     if (ret != 0) {
-        LWIP_DEBUGF(SYS_DEBUG | GAZELLE_DEBUG_SERIOUS, ("thread_create: pthread_create failed\n"));
+        LWIP_DEBUGF(SYS_DEBUG | LWIPGZ_LOG_ERR, ("thread_create: pthread_create failed\n"));
         return ret;
     }
 
     SYS_FORMAT_NAME(thread_name, sizeof(thread_name), "%s_%02u", name, id);
     ret = pthread_setname_np(tid, thread_name);
     if (ret != 0) {
-        LWIP_DEBUGF(SYS_DEBUG | GAZELLE_DEBUG_WARNING, ("thread_create: pthread_setname_np %s failed\n", thread_name));
+        LWIP_DEBUGF(SYS_DEBUG | LWIPGZ_LOG_WARNING, ("thread_create: pthread_setname_np %s failed\n", thread_name));
     }
     return 0;
 }
@@ -116,7 +116,7 @@ sys_thread_t sys_thread_new(const char *name, lwip_thread_fn function, void *arg
 
     thread = (sys_thread_t)malloc(sizeof(*thread));
     if (thread == NULL) {
-        LWIP_DEBUGF(SYS_DEBUG | GAZELLE_DEBUG_SERIOUS, ("sys_thread_new: malloc sys_thread failed\n"));
+        LWIP_DEBUGF(SYS_DEBUG | LWIPGZ_LOG_ERR, ("sys_thread_new: malloc sys_thread failed\n"));
         return NULL;
     }
 
@@ -417,7 +417,7 @@ u8_t *sys_hugepage_malloc(const char *name, unsigned size)
     SYS_FORMAT_NAME(memname, sizeof(memname), "%s_%d", name, rte_gettid());
     mz = rte_memzone_reserve(memname, size, rte_socket_id(), 0);
     if (mz == NULL) {
-        LWIP_DEBUGF(SYS_DEBUG | GAZELLE_DEBUG_SERIOUS, ("sys_hugepage_malloc: failed to reserver memory for mempool[%s], errno %d\n", memname, errno));
+        LWIP_DEBUGF(SYS_DEBUG | LWIPGZ_LOG_ERR, ("sys_hugepage_malloc: failed to reserver memory for mempool[%s], errno %d\n", memname, errno));
         set_errno(ENOMEM);
         return NULL;
     } else {
@@ -433,7 +433,7 @@ u8_t *sys_hugepage_malloc(const char *name, unsigned size)
 void sys_mempool_var_init(struct memp_desc *memp, char *desc, u16_t size, u16_t num,
     u8_t *base, struct memp **tab, struct stats_mem *stats)
 {
-    LWIP_DEBUGF(SYS_DEBUG, ("[tid %u] %s: memp %p desc %s size %u num %u base %p\n",
+    LWIP_DEBUGF(SYS_DEBUG | LWIPGZ_LOG_DEBUG, ("[tid %u] %s: memp %p desc %s size %u num %u base %p\n",
         rte_gettid(), __FUNCTION__, memp, desc, size, num, base));
 
 #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY
diff --git a/src/core/tcp.c b/src/core/tcp.c
index 52d587d..c25f961 100644
--- a/src/core/tcp.c
+++ b/src/core/tcp.c
@@ -409,7 +409,7 @@ tcp_close_shutdown(struct tcp_pcb *pcb, u8_t rst_on_unacked_data)
 
       /* don't call tcp_abort here: we must not deallocate the pcb since
          that might not be expected when calling tcp_close */
-      LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS,
+      LWIP_DEBUGF(LWIPGZ_LOG_ERR,
                   ("tcp_close_shutdown: Not all data received by app, send RST, local_port=%d, remote_port=%d\n",
                   pcb->local_port, pcb->remote_port));
       tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
@@ -669,7 +669,7 @@ tcp_abandon(struct tcp_pcb *pcb, int reset)
 #endif /* TCP_QUEUE_OOSEQ */
     tcp_backlog_accepted(pcb);
     if (send_rst) {
-      LWIP_DEBUGF(TCP_RST_DEBUG | GAZELLE_DEBUG_SERIOUS,
+      LWIP_DEBUGF(TCP_RST_DEBUG | LWIPGZ_LOG_ERR,
                   ("tcp_abandon: send RST, local port=%d, remote port=%d\n", local_port, pcb->remote_port));
       tcp_rst(pcb, seqno, ackno, &pcb->local_ip, &pcb->remote_ip, local_port, pcb->remote_port);
     }
@@ -1411,7 +1411,7 @@ tcp_slowtmr_start:
         LWIP_ASSERT("tcp_slowtimr: persist ticking with empty send buffer", pcb->unsent != NULL);
         if (pcb->persist_probe >= TCP_MAXRTX) {
           ++pcb_remove; /* max probes reached */
-          LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS, ("tcp_slowtmr: persist_probe is greater TCP_MAXRTX local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
+          LWIP_DEBUGF(TCP_DEBUG | LWIPGZ_LOG_ERR, ("tcp_slowtmr: persist_probe is greater TCP_MAXRTX local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
         } else {
           u8_t backoff_cnt = tcp_persist_backoff[pcb->persist_backoff - 1];
           if (pcb->persist_cnt < backoff_cnt) {
@@ -1495,7 +1495,7 @@ tcp_slowtmr_start:
         if ((u32_t)(tcp_ticks - pcb->tmr) >
             TCP_FIN_WAIT_TIMEOUT / TCP_SLOW_INTERVAL) {
           ++pcb_remove;
-          LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2 local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
+          LWIP_DEBUGF(TCP_DEBUG | LWIPGZ_LOG_ERR, ("tcp_slowtmr: removing pcb stuck in FIN-WAIT-2 local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
         }
       }
     }
@@ -1539,7 +1539,7 @@ tcp_slowtmr_start:
       if ((u32_t)(tcp_ticks - pcb->tmr) >
           TCP_SYN_RCVD_TIMEOUT / TCP_SLOW_INTERVAL) {
         ++pcb_remove;
-        LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
+        LWIP_DEBUGF(TCP_DEBUG | LWIPGZ_LOG_ERR, ("tcp_slowtmr: removing pcb stuck in SYN-RCVD local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
       }
     }
 
@@ -1547,7 +1547,7 @@ tcp_slowtmr_start:
     if (pcb->state == LAST_ACK) {
       if ((u32_t)(tcp_ticks - pcb->tmr) > 2 * TCP_MSL / TCP_SLOW_INTERVAL) {
         ++pcb_remove;
-        LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS, ("tcp_slowtmr: removing pcb stuck in LAST-ACK local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
+        LWIP_DEBUGF(TCP_DEBUG | LWIPGZ_LOG_ERR, ("tcp_slowtmr: removing pcb stuck in LAST-ACK local_port=%u, remote_port=%u\n", pcb->local_port, pcb->remote_port));
       }
     }
 
@@ -1586,7 +1586,7 @@ tcp_slowtmr_start:
 #endif
 
       if (pcb_reset) {
-        LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS,
+        LWIP_DEBUGF(LWIPGZ_LOG_ERR,
                     ("tcp_slowtmr: KEEPALIVE timeout, send RST, local port=%d, remote port=%d\n",
                     pcb->local_port, pcb->remote_port));
         tcp_rst(pcb, pcb->snd_nxt, pcb->rcv_nxt, &pcb->local_ip, &pcb->remote_ip,
@@ -1960,7 +1960,7 @@ tcp_kill_prio(u8_t prio)
     }
   }
   if (inactive != NULL) {
-    LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS,
+    LWIP_DEBUGF(TCP_DEBUG | LWIPGZ_LOG_ERR,
                 ("tcp_kill_prio: killing oldest PCB (%"S32_F")\n", inactivity));
     tcp_abort(inactive);
   }
@@ -1991,7 +1991,7 @@ tcp_kill_state(enum tcp_state state)
     }
   }
   if (inactive != NULL) {
-    LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS,
+    LWIP_DEBUGF(TCP_DEBUG | LWIPGZ_LOG_ERR,
                 ("tcp_kill_closing: killing oldest %s PCB (%"S32_F")\n", tcp_state_str[state], inactivity));
     /* Don't send a RST, since no data is lost. */
     tcp_abandon(inactive, 0);
@@ -2018,7 +2018,7 @@ tcp_kill_timewait(void)
     }
   }
   if (inactive != NULL) {
-    LWIP_DEBUGF(TCP_DEBUG | GAZELLE_DEBUG_SERIOUS,
+    LWIP_DEBUGF(TCP_DEBUG | LWIPGZ_LOG_ERR,
                 ("tcp_kill_timewait: killing oldest TIME-WAIT PCB (%"S32_F")\n", inactivity));
     tcp_abort(inactive);
   }
@@ -2561,7 +2561,7 @@ tcp_netif_ip_addr_changed_pcblist(const ip_addr_t *old_addr, struct tcp_pcb *pcb
        ) {
       /* this connection must be aborted */
       struct tcp_pcb *next = pcb->next;
-      LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE | GAZELLE_DEBUG_SERIOUS,
+      LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE | LWIPGZ_LOG_ERR,
                   ("netif_set_ipaddr: aborting TCP\n"));
       tcp_abort(pcb);
       pcb = next;
diff --git a/src/core/tcp_in.c b/src/core/tcp_in.c
index 68b5e6e..d9db957 100644
--- a/src/core/tcp_in.c
+++ b/src/core/tcp_in.c
@@ -841,7 +841,7 @@ tcp_listen_input(struct tcp_pcb_listen *pcb)
     /* Send a SYN|ACK together with the MSS option. */
     rc = tcp_enqueue_flags(npcb, TCP_SYN | TCP_ACK);
     if (rc != ERR_OK) {
-      LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("tcp_listen_input: send SYN or ACK failed\n"));
+      LWIP_DEBUGF(LWIPGZ_LOG_ERR, ("tcp_listen_input: send SYN or ACK failed\n"));
       tcp_abandon(npcb, 0);
       return;
     }
@@ -1047,7 +1047,7 @@ tcp_process(struct tcp_pcb *pcb)
       /* received ACK? possibly a half-open connection */
       else if (flags & TCP_ACK) {
         /* send a RST to bring the other side in a non-synchronized state. */
-        LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("tcp_process: ACK in SYN_SENT, send RST, local_port=%d, remote_port=%d\n",
+        LWIP_DEBUGF(LWIPGZ_LOG_ERR, ("tcp_process: ACK in SYN_SENT, send RST, local_port=%d, remote_port=%d\n",
                     tcphdr->dest, tcphdr->src));
         tcp_rst(pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
                 ip_current_src_addr(), tcphdr->dest, tcphdr->src);
@@ -1090,7 +1090,7 @@ tcp_process(struct tcp_pcb *pcb)
              * the connection. */
             /* Already aborted? */
             if (err != ERR_ABRT) {
-              LWIP_DEBUGF(GAZELLE_DEBUG_SERIOUS, ("tcp_process: accept function returns with an error %d, send RST\n", err));
+              LWIP_DEBUGF(LWIPGZ_LOG_ERR, ("tcp_process: accept function returns with an error %d, send RST\n", err));
               tcp_abort(pcb);
             }
             return ERR_ABRT;
diff --git a/src/include/lwipgz_log.h b/src/include/lwipgz_log.h
index db844af..41ca95c 100644
--- a/src/include/lwipgz_log.h
+++ b/src/include/lwipgz_log.h
@@ -45,22 +45,22 @@
 
 #if GAZELLE_USE_DPDK_LOG
 
-#define LWIP_LOG_WARN    LWIP_DBG_LEVEL_WARNING
-#define LWIP_LOG_ERROR   LWIP_DBG_LEVEL_SERIOUS
-#define LWIP_LOG_FATAL   LWIP_DBG_LEVEL_SEVERE
 #define RTE_LOGTYPE_LWIP RTE_LOGTYPE_USER2
 
-#define LWIP_PLATFORM_LOG(level, fmt, ...) \
+/* Don't worry, compiler will optimize the constant branch. */
+#define LWIP_PLATFORM_LOG(level, fmt, ...)          \
 do { \
-    if ((level) & LWIP_LOG_FATAL) { \
-        RTE_LOG(ERR, LWIP, fmt, ##__VA_ARGS__); \
-        abort();                             \
-    } else if ((level) & LWIP_LOG_ERROR) { \
-        RTE_LOG(ERR, LWIP, fmt, ##__VA_ARGS__); \
-    } else if ((level) & LWIP_LOG_WARN) { \
+    if ((level) & LWIP_DBG_LEVEL_SEVERE) {          \
+        RTE_LOG(ERR, LWIP, fmt, ##__VA_ARGS__);     \
+        rte_exit(EXIT_FAILURE, "exit failure! ");   \
+    } else if ((level) & LWIP_DBG_LEVEL_SERIOUS) {  \
+        RTE_LOG(ERR, LWIP, fmt, ##__VA_ARGS__);     \
+    } else if ((level) & LWIP_DBG_LEVEL_WARNING) {  \
         RTE_LOG(WARNING, LWIP, fmt, ##__VA_ARGS__); \
-    } else { \
-        RTE_LOG(INFO, LWIP, fmt, ##__VA_ARGS__); \
+    } else if ((level) & (LWIP_DBG_TRACE | LWIP_DBG_STATE)) {   \
+        RTE_LOG(INFO, LWIP, fmt, ##__VA_ARGS__);    \
+    } else if ((level) & LWIP_DBG_ON) {             \
+        RTE_LOG(DEBUG, LWIP, fmt, ##__VA_ARGS__);   \
     } \
 } while(0)
 
@@ -71,7 +71,7 @@ do { \
 #define STRIP_BRACES(args) args
 
 #define LWIP_PLATFORM_ASSERT(x) \
-do { LWIP_PLATFORM_LOG(LWIP_LOG_FATAL, "Assertion \"%s\" failed at line %d in %s\n", \
+do { LWIP_PLATFORM_LOG(LWIPGZ_LOG_FATAL, "Assertion \"%s\" failed at line %d in %s\n", \
      x, __LINE__, __FILE__); abort();} while(0)
 
 #else /* GAZELLE_USE_DPDK_LOG */
diff --git a/src/include/lwipopts.h b/src/include/lwipopts.h
index ea6588a..b32e372 100644
--- a/src/include/lwipopts.h
+++ b/src/include/lwipopts.h
@@ -276,10 +276,27 @@
 */
 #define LWIP_DEBUG 1
 
-#define GAZELLE_DEBUG LWIP_DBG_ON
-#define GAZELLE_DEBUG_WARNING (LWIP_DBG_ON | LWIP_DBG_LEVEL_WARNING)
-#define GAZELLE_DEBUG_SERIOUS (LWIP_DBG_ON | LWIP_DBG_LEVEL_SERIOUS)
-#define GAZELLE_DEBUG_SEVERE  (LWIP_DBG_ON | LWIP_DBG_LEVEL_SEVERE)
+/* lwip log level */
+#define LWIPGZ_LOG_DEBUG    (LWIP_DBG_ON)
+#define LWIPGZ_LOG_INFO     (LWIP_DBG_ON | LWIP_DBG_TRACE)
+#define LWIPGZ_LOG_WARNING  (LWIP_DBG_ON | LWIP_DBG_LEVEL_WARNING)
+#define LWIPGZ_LOG_ERR      (LWIP_DBG_ON | LWIP_DBG_LEVEL_SERIOUS)
+#define LWIPGZ_LOG_FATAL    (LWIP_DBG_ON | LWIP_DBG_LEVEL_SEVERE)
+
+/* common debug options */
+#define NETIF_DEBUG         LWIP_DBG_OFF
+#define ETHARP_DEBUG        LWIP_DBG_OFF
+#define IP_DEBUG            LWIP_DBG_OFF
+#define IP6_DEBUG           LWIP_DBG_OFF
+
+#define TCP_DEBUG           LWIP_DBG_OFF
+#define TCP_INPUT_DEBUG     LWIP_DBG_OFF
+#define TCP_OUTPUT_DEBUG    LWIP_DBG_OFF
+#define UDP_DEBUG           LWIP_DBG_OFF
+
+#define SOCKETS_DEBUG       LWIP_DBG_OFF
+#define SYS_DEBUG           LWIP_DBG_OFF
+
 
 /*
    ------------------------------------
-- 
2.33.0

