Miroslav Lichvar
2016-10-17 14:33:30 UTC
When running multiple instances of ptp4l or phc2sys, it's difficult to
tell which log message belongs to which instance. Add new options to
ptp4l and phc2sys which can specify a prefix for all messages printed to
the standard output or system log, so messages from different instances
can have different prefixes.
Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
config.c | 1 +
phc2sys.8 | 4 ++++
phc2sys.c | 9 +++++++--
print.c | 18 ++++++++++++++----
print.h | 1 +
ptp4l.8 | 10 ++++++++++
ptp4l.c | 8 +++++++-
7 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/config.c b/config.c
index 5da5ecc..89e349e 100644
--- a/config.c
+++ b/config.c
@@ -199,6 +199,7 @@ struct config_item config_tab[] = {
PORT_ITEM_INT("logMinPdelayReqInterval", 0, INT8_MIN, INT8_MAX),
PORT_ITEM_INT("logSyncInterval", 0, INT8_MIN, INT8_MAX),
GLOB_ITEM_INT("logging_level", LOG_INFO, PRINT_LEVEL_MIN, PRINT_LEVEL_MAX),
+ GLOB_ITEM_STR("logging_prefix", NULL),
GLOB_ITEM_STR("manufacturerIdentity", "00:00:00"),
GLOB_ITEM_INT("max_frequency", 900000000, 0, INT_MAX),
PORT_ITEM_INT("min_neighbor_prop_delay", -20000000, INT_MIN, -1),
diff --git a/phc2sys.8 b/phc2sys.8
index 22d02c2..e515bd1 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -206,6 +206,10 @@ The default is /var/run/ptp4l.
Set the maximum syslog level of messages which should be printed or sent to
the system logger. The default is 6 (LOG_INFO).
.TP
+.BI \-o " print-prefix"
+Specifies a prefix for messages printed to the standard output or system log.
+The default is empty string.
+.TP
.B \-m
Print messages to the standard output.
.TP
diff --git a/phc2sys.c b/phc2sys.c
index 35cf6fa..a37e9bc 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -1209,6 +1209,7 @@ static void usage(char *progname)
" -x apply leap seconds by servo instead of kernel\n"
" -z [path] server address for UDS (/var/run/ptp4l)\n"
" -l [num] set the logging level to 'num' (6)\n"
+ " -o [prefix] set prefix for log messages\n"
" -m print messages to stdout\n"
" -q do not print messages to the syslog\n"
" -v prints the software version and exits\n"
@@ -1219,7 +1220,7 @@ static void usage(char *progname)
int main(int argc, char *argv[])
{
- char *progname;
+ char *progname, *prefix = NULL;
char *src_name = NULL, *dst_name = NULL;
struct clock *src, *dst;
struct config *cfg;
@@ -1251,7 +1252,7 @@ int main(int argc, char *argv[])
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
while (EOF != (c = getopt(argc, argv,
- "arc:d:s:E:P:I:S:F:R:N:O:L:M:i:u:wn:xz:l:mqvh"))) {
+ "arc:d:s:E:P:I:S:F:R:N:O:L:M:i:u:wn:xz:l:o:mqvh"))) {
switch (c) {
case 'a':
autocfg = 1;
@@ -1363,6 +1364,9 @@ int main(int argc, char *argv[])
PRINT_LEVEL_MIN, PRINT_LEVEL_MAX))
goto end;
break;
+ case 'o':
+ prefix = optarg;
+ break;
case 'm':
verbose = 1;
break;
@@ -1405,6 +1409,7 @@ int main(int argc, char *argv[])
}
print_set_progname(progname);
+ print_set_prefix(prefix);
print_set_verbose(verbose);
print_set_syslog(use_syslog);
print_set_level(print_level);
diff --git a/print.c b/print.c
index a82d0e7..c1dd867 100644
--- a/print.c
+++ b/print.c
@@ -28,12 +28,18 @@ static int verbose = 0;
static int print_level = LOG_INFO;
static int use_syslog = 1;
static const char *progname;
+static const char *prefix;
void print_set_progname(const char *name)
{
progname = name;
}
+void print_set_prefix(const char *string)
+{
+ prefix = string;
+}
+
void print_set_syslog(int value)
{
use_syslog = value ? 1 : 0;
@@ -67,13 +73,17 @@ void print(int level, char const *format, ...)
if (verbose) {
f = level >= LOG_NOTICE ? stdout : stderr;
- fprintf(f, "%s[%ld.%03ld]: %s\n",
+ fprintf(f, "%s[%ld.%03ld]: %s%s%s\n",
progname ? progname : "",
- ts.tv_sec, ts.tv_nsec / 1000000, buf);
+ ts.tv_sec, ts.tv_nsec / 1000000,
+ prefix ? prefix : "", prefix ? " " : "",
+ buf);
fflush(f);
}
if (use_syslog) {
- syslog(level, "[%ld.%03ld] %s",
- ts.tv_sec, ts.tv_nsec / 1000000, buf);
+ syslog(level, "[%ld.%03ld] %s%s%s",
+ ts.tv_sec, ts.tv_nsec / 1000000,
+ prefix ? prefix : "", prefix ? " " : "",
+ buf);
}
}
diff --git a/print.h b/print.h
index e8f2c8e..6236a14 100644
--- a/print.h
+++ b/print.h
@@ -33,6 +33,7 @@ __attribute__ ((format (printf, 2, 3)))
void print(int level, char const *format, ...);
void print_set_progname(const char *name);
+void print_set_prefix(const char *prefix);
void print_set_syslog(int value);
void print_set_level(int level);
void print_set_verbose(int value);
diff --git a/ptp4l.8 b/ptp4l.8
index 63e9abd..6755ad7 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -12,6 +12,8 @@ ptp4l - PTP Boundary/Ordinary Clock
.BI \-p " phc-device"
] [
.BI \-l " print-level"
+] [
+.BI \-o " print-prefix"
]
[
.BI \-i " interface"
@@ -82,6 +84,10 @@ Enable the slaveOnly mode.
Set the maximum syslog level of messages which should be printed or sent to
the system logger. The default is 6 (LOG_INFO).
.TP
+.BI \-o " print-prefix"
+Specifies a prefix for messages printed to the standard output or system log.
+The default is empty string.
+.TP
.B \-m
Print messages to the standard output.
.TP
@@ -466,6 +472,10 @@ is 0.
The maximum logging level of messages which should be printed.
The default is 6 (LOG_INFO).
.TP
+.B logging_prefix
+Prefix for messages printed to the standard output or system log.
+The default is empty string.
+.TP
.B verbose
Print messages to the standard output if enabled.
The default is 0 (disabled).
diff --git a/ptp4l.c b/ptp4l.c
index a87e7e6..c4813cb 100644
--- a/ptp4l.c
+++ b/ptp4l.c
@@ -62,6 +62,7 @@ static void usage(char *progname)
" (ignored for SOFTWARE/LEGACY HW time stamping)\n"
" -s slave only mode (overrides configuration file)\n"
" -l [num] set the logging level to 'num'\n"
+ " -o [str] set prefix for log messages\n"
" -m print messages to stdout\n"
" -q do not print messages to the syslog\n"
" -v prints the software version and exits\n"
@@ -88,7 +89,7 @@ int main(int argc, char *argv[])
/* Process the command line arguments. */
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
- while (EOF != (c = getopt(argc, argv, "AEP246HSLf:i:p:sl:mqvh"))) {
+ while (EOF != (c = getopt(argc, argv, "AEP246HSLf:i:p:sl:o:mqvh"))) {
switch (c) {
case 'A':
if (config_set_int(cfg, "delay_mechanism", DM_AUTO))
@@ -150,6 +151,10 @@ int main(int argc, char *argv[])
goto out;
config_set_int(cfg, "logging_level", print_level);
break;
+ case 'o':
+ if (config_set_string(cfg, "logging_prefix", optarg))
+ goto out;
+ break;
case 'm':
config_set_int(cfg, "verbose", 1);
break;
@@ -176,6 +181,7 @@ int main(int argc, char *argv[])
}
print_set_progname(progname);
+ print_set_prefix(config_get_string(cfg, NULL, "logging_prefix"));
print_set_verbose(config_get_int(cfg, NULL, "verbose"));
print_set_syslog(config_get_int(cfg, NULL, "use_syslog"));
print_set_level(config_get_int(cfg, NULL, "logging_level"));
tell which log message belongs to which instance. Add new options to
ptp4l and phc2sys which can specify a prefix for all messages printed to
the standard output or system log, so messages from different instances
can have different prefixes.
Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
config.c | 1 +
phc2sys.8 | 4 ++++
phc2sys.c | 9 +++++++--
print.c | 18 ++++++++++++++----
print.h | 1 +
ptp4l.8 | 10 ++++++++++
ptp4l.c | 8 +++++++-
7 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/config.c b/config.c
index 5da5ecc..89e349e 100644
--- a/config.c
+++ b/config.c
@@ -199,6 +199,7 @@ struct config_item config_tab[] = {
PORT_ITEM_INT("logMinPdelayReqInterval", 0, INT8_MIN, INT8_MAX),
PORT_ITEM_INT("logSyncInterval", 0, INT8_MIN, INT8_MAX),
GLOB_ITEM_INT("logging_level", LOG_INFO, PRINT_LEVEL_MIN, PRINT_LEVEL_MAX),
+ GLOB_ITEM_STR("logging_prefix", NULL),
GLOB_ITEM_STR("manufacturerIdentity", "00:00:00"),
GLOB_ITEM_INT("max_frequency", 900000000, 0, INT_MAX),
PORT_ITEM_INT("min_neighbor_prop_delay", -20000000, INT_MIN, -1),
diff --git a/phc2sys.8 b/phc2sys.8
index 22d02c2..e515bd1 100644
--- a/phc2sys.8
+++ b/phc2sys.8
@@ -206,6 +206,10 @@ The default is /var/run/ptp4l.
Set the maximum syslog level of messages which should be printed or sent to
the system logger. The default is 6 (LOG_INFO).
.TP
+.BI \-o " print-prefix"
+Specifies a prefix for messages printed to the standard output or system log.
+The default is empty string.
+.TP
.B \-m
Print messages to the standard output.
.TP
diff --git a/phc2sys.c b/phc2sys.c
index 35cf6fa..a37e9bc 100644
--- a/phc2sys.c
+++ b/phc2sys.c
@@ -1209,6 +1209,7 @@ static void usage(char *progname)
" -x apply leap seconds by servo instead of kernel\n"
" -z [path] server address for UDS (/var/run/ptp4l)\n"
" -l [num] set the logging level to 'num' (6)\n"
+ " -o [prefix] set prefix for log messages\n"
" -m print messages to stdout\n"
" -q do not print messages to the syslog\n"
" -v prints the software version and exits\n"
@@ -1219,7 +1220,7 @@ static void usage(char *progname)
int main(int argc, char *argv[])
{
- char *progname;
+ char *progname, *prefix = NULL;
char *src_name = NULL, *dst_name = NULL;
struct clock *src, *dst;
struct config *cfg;
@@ -1251,7 +1252,7 @@ int main(int argc, char *argv[])
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
while (EOF != (c = getopt(argc, argv,
- "arc:d:s:E:P:I:S:F:R:N:O:L:M:i:u:wn:xz:l:mqvh"))) {
+ "arc:d:s:E:P:I:S:F:R:N:O:L:M:i:u:wn:xz:l:o:mqvh"))) {
switch (c) {
case 'a':
autocfg = 1;
@@ -1363,6 +1364,9 @@ int main(int argc, char *argv[])
PRINT_LEVEL_MIN, PRINT_LEVEL_MAX))
goto end;
break;
+ case 'o':
+ prefix = optarg;
+ break;
case 'm':
verbose = 1;
break;
@@ -1405,6 +1409,7 @@ int main(int argc, char *argv[])
}
print_set_progname(progname);
+ print_set_prefix(prefix);
print_set_verbose(verbose);
print_set_syslog(use_syslog);
print_set_level(print_level);
diff --git a/print.c b/print.c
index a82d0e7..c1dd867 100644
--- a/print.c
+++ b/print.c
@@ -28,12 +28,18 @@ static int verbose = 0;
static int print_level = LOG_INFO;
static int use_syslog = 1;
static const char *progname;
+static const char *prefix;
void print_set_progname(const char *name)
{
progname = name;
}
+void print_set_prefix(const char *string)
+{
+ prefix = string;
+}
+
void print_set_syslog(int value)
{
use_syslog = value ? 1 : 0;
@@ -67,13 +73,17 @@ void print(int level, char const *format, ...)
if (verbose) {
f = level >= LOG_NOTICE ? stdout : stderr;
- fprintf(f, "%s[%ld.%03ld]: %s\n",
+ fprintf(f, "%s[%ld.%03ld]: %s%s%s\n",
progname ? progname : "",
- ts.tv_sec, ts.tv_nsec / 1000000, buf);
+ ts.tv_sec, ts.tv_nsec / 1000000,
+ prefix ? prefix : "", prefix ? " " : "",
+ buf);
fflush(f);
}
if (use_syslog) {
- syslog(level, "[%ld.%03ld] %s",
- ts.tv_sec, ts.tv_nsec / 1000000, buf);
+ syslog(level, "[%ld.%03ld] %s%s%s",
+ ts.tv_sec, ts.tv_nsec / 1000000,
+ prefix ? prefix : "", prefix ? " " : "",
+ buf);
}
}
diff --git a/print.h b/print.h
index e8f2c8e..6236a14 100644
--- a/print.h
+++ b/print.h
@@ -33,6 +33,7 @@ __attribute__ ((format (printf, 2, 3)))
void print(int level, char const *format, ...);
void print_set_progname(const char *name);
+void print_set_prefix(const char *prefix);
void print_set_syslog(int value);
void print_set_level(int level);
void print_set_verbose(int value);
diff --git a/ptp4l.8 b/ptp4l.8
index 63e9abd..6755ad7 100644
--- a/ptp4l.8
+++ b/ptp4l.8
@@ -12,6 +12,8 @@ ptp4l - PTP Boundary/Ordinary Clock
.BI \-p " phc-device"
] [
.BI \-l " print-level"
+] [
+.BI \-o " print-prefix"
]
[
.BI \-i " interface"
@@ -82,6 +84,10 @@ Enable the slaveOnly mode.
Set the maximum syslog level of messages which should be printed or sent to
the system logger. The default is 6 (LOG_INFO).
.TP
+.BI \-o " print-prefix"
+Specifies a prefix for messages printed to the standard output or system log.
+The default is empty string.
+.TP
.B \-m
Print messages to the standard output.
.TP
@@ -466,6 +472,10 @@ is 0.
The maximum logging level of messages which should be printed.
The default is 6 (LOG_INFO).
.TP
+.B logging_prefix
+Prefix for messages printed to the standard output or system log.
+The default is empty string.
+.TP
.B verbose
Print messages to the standard output if enabled.
The default is 0 (disabled).
diff --git a/ptp4l.c b/ptp4l.c
index a87e7e6..c4813cb 100644
--- a/ptp4l.c
+++ b/ptp4l.c
@@ -62,6 +62,7 @@ static void usage(char *progname)
" (ignored for SOFTWARE/LEGACY HW time stamping)\n"
" -s slave only mode (overrides configuration file)\n"
" -l [num] set the logging level to 'num'\n"
+ " -o [str] set prefix for log messages\n"
" -m print messages to stdout\n"
" -q do not print messages to the syslog\n"
" -v prints the software version and exits\n"
@@ -88,7 +89,7 @@ int main(int argc, char *argv[])
/* Process the command line arguments. */
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
- while (EOF != (c = getopt(argc, argv, "AEP246HSLf:i:p:sl:mqvh"))) {
+ while (EOF != (c = getopt(argc, argv, "AEP246HSLf:i:p:sl:o:mqvh"))) {
switch (c) {
case 'A':
if (config_set_int(cfg, "delay_mechanism", DM_AUTO))
@@ -150,6 +151,10 @@ int main(int argc, char *argv[])
goto out;
config_set_int(cfg, "logging_level", print_level);
break;
+ case 'o':
+ if (config_set_string(cfg, "logging_prefix", optarg))
+ goto out;
+ break;
case 'm':
config_set_int(cfg, "verbose", 1);
break;
@@ -176,6 +181,7 @@ int main(int argc, char *argv[])
}
print_set_progname(progname);
+ print_set_prefix(config_get_string(cfg, NULL, "logging_prefix"));
print_set_verbose(config_get_int(cfg, NULL, "verbose"));
print_set_syslog(config_get_int(cfg, NULL, "use_syslog"));
print_set_level(config_get_int(cfg, NULL, "logging_level"));
--
2.9.3
2.9.3