From 781dee0835fbea52f57389893d50b2cf9f60e41f Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 18 Jul 2012 10:30:47 -0700 Subject: [PATCH] util: Introduce "subprogram_name" to identify subprocesses and threads. This will be more useful later when we introduces "worker" subprocesses. I don't have any current plans to introduce threading, but I can't think of a disadvantage to wording this in a general manner. Signed-off-by: Ben Pfaff --- NEWS | 3 +++ lib/daemon.c | 9 +++------ lib/util.c | 14 +++++++++++++- lib/util.h | 1 + lib/vlog.c | 8 ++++++++ lib/vlog.h | 8 ++++---- utilities/ovs-appctl.8.in | 10 ++++++++++ 7 files changed, 42 insertions(+), 11 deletions(-) diff --git a/NEWS b/NEWS index c74f5922..89554fd6 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ post-v1.8.0 ------------------------ + - New %t and %T log escapes to identify the subprogram within a + cooperating group of processes or threads that emitted a log message. + The default log patterns now include this information. v1.8.0 - xx xxx xxxx diff --git a/lib/daemon.c b/lib/daemon.c index 67256834..ecca6061 100644 --- a/lib/daemon.c +++ b/lib/daemon.c @@ -351,13 +351,11 @@ static void monitor_daemon(pid_t daemon_pid) { /* XXX Should log daemon's stderr output at startup time. */ - const char *saved_program_name; time_t last_restart; char *status_msg; int crashes; - saved_program_name = program_name; - program_name = xasprintf("monitor(%s)", program_name); + subprogram_name = "monitor"; status_msg = xstrdup("healthy"); last_restart = TIME_MIN; crashes = 0; @@ -366,7 +364,7 @@ monitor_daemon(pid_t daemon_pid) int status; proctitle_set("%s: monitoring pid %lu (%s)", - saved_program_name, (unsigned long int) daemon_pid, + program_name, (unsigned long int) daemon_pid, status_msg); do { @@ -428,8 +426,7 @@ monitor_daemon(pid_t daemon_pid) /* Running in new daemon process. */ proctitle_restore(); - free((char *) program_name); - program_name = saved_program_name; + subprogram_name = ""; } /* Close standard file descriptors (except any that the client has requested we diff --git a/lib/util.c b/lib/util.c index de3cf3cb..bc5fa988 100644 --- a/lib/util.c +++ b/lib/util.c @@ -34,7 +34,14 @@ VLOG_DEFINE_THIS_MODULE(util); COVERAGE_DEFINE(util_xalloc); +/* argv[0] without directory names. */ const char *program_name; + +/* Ordinarily "" but set to "monitor" for a monitor process or "worker" for a + * worker process. */ +const char *subprogram_name = ""; + +/* --version option output. */ static char *program_version; void @@ -243,7 +250,12 @@ ovs_error_valist(int err_no, const char *format, va_list args) { int save_errno = errno; - fprintf(stderr, "%s: ", program_name); + if (subprogram_name[0]) { + fprintf(stderr, "%s(%s): ", program_name, subprogram_name); + } else { + fprintf(stderr, "%s: ", program_name); + } + vfprintf(stderr, format, args); if (err_no != 0) { fprintf(stderr, " (%s)", ovs_retval_to_string(err_no)); diff --git a/lib/util.h b/lib/util.h index 1bdaeee5..dd86de26 100644 --- a/lib/util.h +++ b/lib/util.h @@ -62,6 +62,7 @@ #endif extern const char *program_name; +extern const char *subprogram_name; /* Returns the number of elements in ARRAY. */ #define ARRAY_SIZE(ARRAY) (sizeof ARRAY / sizeof *ARRAY) diff --git a/lib/vlog.c b/lib/vlog.c index d26e6139..128ed45d 100644 --- a/lib/vlog.c +++ b/lib/vlog.c @@ -653,6 +653,14 @@ format_log_message(const struct vlog_module *module, enum vlog_level level, case 'r': ds_put_format(s, "%lld", time_msec() - time_boot_msec()); break; + case 't': + ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main"); + break; + case 'T': + if (subprogram_name[0]) { + ds_put_format(s, "(%s)", subprogram_name); + } + break; default: ds_put_char(s, p[-1]); break; diff --git a/lib/vlog.h b/lib/vlog.h index e15e441d..9050634e 100644 --- a/lib/vlog.h +++ b/lib/vlog.h @@ -51,10 +51,10 @@ const char *vlog_get_level_name(enum vlog_level); enum vlog_level vlog_get_level_val(const char *name); /* Facilities that we can log to. */ -#define VLOG_FACILITIES \ - VLOG_FACILITY(SYSLOG, "%05N|%c|%p|%m") \ - VLOG_FACILITY(CONSOLE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c|%p|%m") \ - VLOG_FACILITY(FILE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c|%p|%m") +#define VLOG_FACILITIES \ + VLOG_FACILITY(SYSLOG, "%05N|%c%T|%p|%m") \ + VLOG_FACILITY(CONSOLE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m") \ + VLOG_FACILITY(FILE, "%D{%Y-%m-%dT%H:%M:%SZ}|%05N|%c%T|%p|%m") enum vlog_facility { #define VLOG_FACILITY(NAME, PATTERN) VLF_##NAME, VLOG_FACILITIES diff --git a/utilities/ovs-appctl.8.in b/utilities/ovs-appctl.8.in index c2ed26b5..7edd1823 100644 --- a/utilities/ovs-appctl.8.in +++ b/utilities/ovs-appctl.8.in @@ -187,6 +187,16 @@ The program's process ID (pid), as a decimal number. The number of milliseconds elapsed from the start of the application to the time the message was logged. . +.IP \fB%t\fR +The subprogram name, that is, an identifying name for the process or +thread that emitted the log message, such as \fBmonitor\fR for the +process used for \fB\-\-monitor\fR or \fBmain\fR for the primary +process or thread in a program. +. +.IP \fB%T\fR +The subprogram name enclosed in parentheses, e.g. \fB(monitor)\fR, or +the empty string for the primary process or thread in a program. +. .IP \fB%%\fR A literal \fB%\fR. .RE -- 2.30.2