DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [dpdk-dev] [PATCH v2 2/3] log: add ability to match dynamic log based on shell pattern
Date: Fri, 23 Feb 2018 13:17:51 -0800	[thread overview]
Message-ID: <20180223211752.28651-3-stephen@networkplumber.org> (raw)
In-Reply-To: <20180223211752.28651-1-stephen@networkplumber.org>

Regular expressions are not the best way to match a hierarchical
pattern like dynamic log levels. And the separator for dynamic
log levels is period which is the regex wildcard character.

A better solution is to use filename matching 'globbing' so
that log levels match like file paths. For compatibility,
use colon to separate pattern match style arguments. For
example:
	--log-level 'pmd.net.virtio.*:debug'

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/librte_eal/common/eal_common_log.c     | 22 +++++++++++++++++-
 lib/librte_eal/common/eal_common_options.c | 36 ++++++++++++++++++++----------
 lib/librte_eal/common/include/rte_log.h    | 16 +++++++++++--
 3 files changed, 59 insertions(+), 15 deletions(-)

diff --git a/lib/librte_eal/common/eal_common_log.c b/lib/librte_eal/common/eal_common_log.c
index 37b2e20e539b..2601c7cd4e4f 100644
--- a/lib/librte_eal/common/eal_common_log.c
+++ b/lib/librte_eal/common/eal_common_log.c
@@ -9,6 +9,7 @@
 #include <string.h>
 #include <errno.h>
 #include <regex.h>
+#include <fnmatch.h>
 
 #include <rte_eal.h>
 #include <rte_log.h>
@@ -89,7 +90,26 @@ rte_log_set_level(uint32_t type, uint32_t level)
 	return 0;
 }
 
-/* set level */
+int
+rte_log_set_level_match(const char *pattern, uint32_t level)
+{
+	size_t i;
+
+	if (level > RTE_LOG_DEBUG)
+		return -1;
+
+	for (i = 0; i < rte_logs.dynamic_types_len; i++) {
+		if (rte_logs.dynamic_types[i].name == NULL)
+			continue;
+
+		if (fnmatch(pattern, rte_logs.dynamic_types[i].name, 0))
+			rte_logs.dynamic_types[i].loglevel = level;
+	}
+
+	return 0;
+}
+
+/* set level by regular expression (using pattern match is preferred) */
 int
 rte_log_set_level_regexp(const char *pattern, uint32_t level)
 {
diff --git a/lib/librte_eal/common/eal_common_options.c b/lib/librte_eal/common/eal_common_options.c
index 19069638ea05..45a75f75ae94 100644
--- a/lib/librte_eal/common/eal_common_options.c
+++ b/lib/librte_eal/common/eal_common_options.c
@@ -955,31 +955,43 @@ eal_parse_log_priority(const char *level)
 static int
 eal_parse_log_level(const char *arg)
 {
-	char *str, *type, *level;
+	char *str, *level;
+	const char *regex = NULL;
+	const char *pattern = NULL;
 	int priority;
 
 	str = strdup(arg);
 	if (str == NULL)
 		return -1;
 
-	if (strchr(str, ',') == NULL) {
-		type = NULL;
-		level = str;
+	if ((level = strchr(str, ','))) {
+		regex = str;
+		*level++ = '\0';
+	} else if ((level = strchr(str, ':'))) {
+		pattern = str;
+		*level++ = '\0';
 	} else {
-		type = strsep(&str, ",");
-		level = strsep(&str, ",");
+		level = str;
 	}
 
 	priority = eal_parse_log_priority(level);
 	if (priority < 0)
 		goto fail;
 
-	if (type == NULL) {
+	if (regex) {
+		if (rte_log_set_level_regexp(regex, priority) < 0) {
+			fprintf(stderr, "cannot set log level %s,%d\n",
+				pattern, priority);
+			goto fail;
+		}
+	} else if (pattern) {
+		if (rte_log_set_level_match(pattern, priority) < 0) {
+			fprintf(stderr, "cannot set log level %s:%d\n",
+				pattern, priority);
+			goto fail;
+		}
+	} else {
 		rte_log_set_global_level(priority);
-	} else if (rte_log_set_level_regexp(type, priority) < 0) {
-		fprintf(stderr, "cannot set log level %s,%d\n",
-			type, priority);
-		goto fail;
 	}
 
 	free(str);
@@ -1336,7 +1348,7 @@ eal_common_usage(void)
 	       "  --"OPT_PROC_TYPE"         Type of this process (primary|secondary|auto)\n"
 	       "  --"OPT_SYSLOG"            Set syslog facility\n"
 	       "  --"OPT_LOG_LEVEL"=<int>   Set global log level\n"
-	       "  --"OPT_LOG_LEVEL"=<type-regexp>,<int>\n"
+	       "  --"OPT_LOG_LEVEL"=<type-match>:<int>\n"
 	       "                      Set specific log level\n"
 	       "  -v                  Display version information on startup\n"
 	       "  -h, --help          This help\n"
diff --git a/lib/librte_eal/common/include/rte_log.h b/lib/librte_eal/common/include/rte_log.h
index 9029c7856d31..6d0ff9fe4623 100644
--- a/lib/librte_eal/common/include/rte_log.h
+++ b/lib/librte_eal/common/include/rte_log.h
@@ -132,13 +132,25 @@ int rte_log_get_level(uint32_t logtype);
  * Set the log level for a given type.
  *
  * @param pattern
- *   The regexp identifying the log type.
+ *   The match pattern identifying the log type.
  * @param level
  *   The level to be set.
  * @return
  *   0 on success, a negative value if level is invalid.
  */
-int rte_log_set_level_regexp(const char *pattern, uint32_t level);
+int rte_log_set_level_match(const char *pattern, uint32_t level);
+
+/**
+ * Set the log level for a given type.
+ *
+ * @param regex
+ *   The regular expression identifying the log type.
+ * @param level
+ *   The level to be set.
+ * @return
+ *   0 on success, a negative value if level is invalid.
+ */
+int rte_log_set_level_regexp(const char *regex, uint32_t level);
 
 /**
  * Set the log level for a given type.
-- 
2.16.1

  parent reply	other threads:[~2018-02-23 21:18 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-23 20:56 [dpdk-dev] [PATCH 0/3] logging enhancments Stephen Hemminger
2018-02-23 20:56 ` [dpdk-dev] [PATCH 1/3] eal: allow symbolic log levels Stephen Hemminger
2018-02-23 20:56 ` [dpdk-dev] [PATCH 2/3] log: add ability to match dynamic log based on shell pattern Stephen Hemminger
2018-02-23 20:56 ` [dpdk-dev] [PATCH 3/3] doc: update log level matching in documentation Stephen Hemminger
2018-02-23 21:17 ` [dpdk-dev] [PATCH v2 0/3] logging enhancements Stephen Hemminger
2018-02-23 21:17   ` [dpdk-dev] [PATCH v2 1/3] eal: allow symbolic log levels Stephen Hemminger
2018-02-23 21:17   ` Stephen Hemminger [this message]
2018-04-04 11:34     ` [dpdk-dev] [PATCH v2 2/3] log: add ability to match dynamic log based on shell pattern Thomas Monjalon
2018-04-23 21:08       ` Thomas Monjalon
2018-04-23 23:59         ` Stephen Hemminger
2018-02-23 21:17   ` [dpdk-dev] [PATCH v2 3/3] doc: update log level matching in documentation Stephen Hemminger
2018-04-04 11:32     ` Thomas Monjalon
2018-04-04 11:37   ` [dpdk-dev] [PATCH v2 0/3] logging enhancements Thomas Monjalon
2018-04-23  8:15     ` Olivier Matz

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=20180223211752.28651-3-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    /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).