If the string specified by the message_tag option contains "{level}",
replace it with the log level of the message as a number.

This allows users to filter printed log messages by their level.

Signed-off-by: Miroslav Lichvar <mlich...@redhat.com>
---
 phc2sys.8 |  4 +++-
 print.c   | 27 ++++++++++++++++++---------
 ptp4l.8   |  3 ++-
 ts2phc.8  |  6 ++++--
 tz2alt.8  |  6 ++++--
 5 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/phc2sys.8 b/phc2sys.8
index 9a37778..df0608a 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -323,7 +323,9 @@ The default is 6 (LOG_INFO). Same as option
 .TP
 .B message_tag
 The tag which is added to all messages printed to the standard output
-or system log. The default is an empty string (which cannot be set in
+or system log. If the tag contains the string "{level}", it will be replaced
+with the log level of the message as a number.
+The default is an empty string (which cannot be set in
 the configuration file as the option requires an argument).
 Same as option
 .B \-t
diff --git a/print.c b/print.c
index 02bdcd0..7fd9d53 100644
--- a/print.c
+++ b/print.c
@@ -57,9 +57,10 @@ void print_set_verbose(int value)
 
 void print(int level, char const *format, ...)
 {
+       char buf[1024], tag[128], *s;
        struct timespec ts;
+       const char *v;
        va_list ap;
-       char buf[1024];
        FILE *f;
 
        if (level > print_level)
@@ -71,19 +72,27 @@ void print(int level, char const *format, ...)
        vsnprintf(buf, sizeof(buf), format, ap);
        va_end(ap);
 
+       if (message_tag) {
+               snprintf(tag, sizeof(tag), "%s ", message_tag);
+               v = "{level}";
+               s = strstr(tag, v);
+               if (s) {
+                       *s = '0' + level;
+                       memmove(s + 1, s + strlen(v), strlen(s + strlen(v)) + 
1);
+               }
+       } else {
+               tag[0] = '\0';
+       }
+
        if (verbose) {
                f = level >= LOG_NOTICE ? stdout : stderr;
-               fprintf(f, "%s[%lld.%03ld]: %s%s%s\n",
+               fprintf(f, "%s[%lld.%03ld]: %s%s\n",
                        progname ? progname : "",
-                       (long long)ts.tv_sec, ts.tv_nsec / 1000000,
-                       message_tag ? message_tag : "", message_tag ? " " : "",
-                       buf);
+                       (long long)ts.tv_sec, ts.tv_nsec / 1000000, tag, buf);
                fflush(f);
        }
        if (use_syslog) {
-               syslog(level, "[%lld.%03ld] %s%s%s",
-                      (long long)ts.tv_sec, ts.tv_nsec / 1000000,
-                      message_tag ? message_tag : "", message_tag ? " " : "",
-                      buf);
+               syslog(level, "[%lld.%03ld] %s%s",
+                      (long long)ts.tv_sec, ts.tv_nsec / 1000000, tag, buf);
        }
 }
diff --git a/ptp4l.8 b/ptp4l.8
index 40c66c2..504cc9f 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -705,7 +705,8 @@ The default value is 255.
 .TP
 .B message_tag
 The tag which is added to all messages printed to the standard output or system
-log.
+log. If the tag contains the string "{level}", it will be replaced with the log
+level of the message as a number.
 The default is an empty string (which cannot be set in the configuration file
 as the option requires an argument).
 
diff --git a/ts2phc.8 b/ts2phc.8
index 3c71d47..852a527 100644
--- a/ts2phc.8
+++ b/ts2phc.8
@@ -156,8 +156,10 @@ The default is 900000000 (90%).
 .TP
 .B message_tag
 The tag which is added to all messages printed to the standard output
-or system log.  The default is an empty string (which cannot be set in
-the configuration file as the option requires an argument).
+or system log.  If the tag contains the string "{level}", it will be replaced
+with the log level of the message as a number.  The default is an empty string
+(which cannot be set in the configuration file as the option requires an
+argument).
 
 .TP
 .B step_threshold
diff --git a/tz2alt.8 b/tz2alt.8
index 66a6605..8cf790b 100644
--- a/tz2alt.8
+++ b/tz2alt.8
@@ -94,8 +94,10 @@ The default is 6 (LOG_INFO).
 .TP
 .B message_tag
 The tag which is added to all messages printed to the standard output
-or system log.  The default is an empty string (which cannot be set in
-the configuration file as the option requires an argument).
+or system log.  If the tag contains the string "{level}", it will be replaced
+with the log level of the message as a number.  The default is an empty string
+(which cannot be set in the configuration file as the option requires an
+argument).
 
 .TP
 .B transportSpecific
-- 
2.41.0



_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to