From d5302613a00915076b945b25b50eb6b376121955 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Thu, 18 Feb 2016 08:12:39 +0200 Subject: Improve logging * src/diag.c: Rewrite. (vdiagmsg,diagmsg): New functions. * src/pies.c (main): Set DIAG_REOPEN_LOG for init process. * src/pies.h (DIAG_REOPEN_LOG): New flag (DIAG_TO_MASK,DIAG_ALL): New constants. (diagmsg): New proto. * src/sysvinit.c: Add log messages in important transition points (inittrans): Reinitialize logging to syslog upon transition to normal mode. --- src/diag.c | 152 ++++++++++++++++++++++++++++++++++++++++++--------------- src/pies.c | 7 +-- src/pies.h | 11 ++++- src/sysvinit.c | 10 +++- 4 files changed, 136 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/diag.c b/src/diag.c index eba08b5..f601daf 100644 --- a/src/diag.c +++ b/src/diag.c @@ -16,21 +16,15 @@ #include "pies.h" -unsigned debug_level; -int source_info_option; -int diag_output = DIAG_TO_STDERR; - -void -diag_setup (int flags) +typedef struct { - if (flags) - diag_output = flags; - if (diag_output & DIAG_TO_SYSLOG) - openlog (log_tag, LOG_PID, log_facility); -} + int open; + FILE *file; +} LOGSTREAM; -void -syslog_printer (int prio, const char *fmt, va_list ap) + +static int +syslog_printer (LOGSTREAM *str, int prio, const char *fmt, va_list ap) { #if HAVE_VSYSLOG vsyslog (prio, fmt, ap); @@ -39,48 +33,131 @@ syslog_printer (int prio, const char *fmt, va_list ap) vsnprintf (buf, sizeof buf, fmt, ap); syslog (prio, "%s", buf); #endif + return 0; } -static FILE * -stderr_open () +static int +syslog_open (int logf, LOGSTREAM *str) { - if (!init_process) - return stderr; + if (logf & DIAG_REOPEN_LOG) + { + openlog (log_tag, LOG_PID, log_facility); + str->open = 1; + } else + str->open = 0; + return 0; +} + +static void +syslog_close (LOGSTREAM *str) +{ + if (str->open) + closelog (); +} + +static int +stderr_printer (LOGSTREAM *str, int prio, const char *fmt, va_list ap) +{ + FILE *fp = str->file; + va_list aq; + + fprintf (fp, "%s: ", program_name); + va_copy (aq, ap); + vfprintf (fp, fmt, aq); + va_end (aq); + fprintf (fp, "\n"); + return 0; +} + +static int +stderr_open (int logf, LOGSTREAM *str) +{ + if (logf & DIAG_REOPEN_LOG) { int fd = console_open (O_WRONLY|O_NOCTTY|O_NDELAY); if (fd == -1) - return NULL; - return fdopen (fd, "w"); + return -1; + str->file = fdopen (fd, "w"); + if (!str->file) + { + close (fd); + return -1; + } + str->open = 1; } + else + { + str->file = stderr; + str->open = 0; + } + return 0; } static void -stderr_close (FILE *fp) +stderr_close (LOGSTREAM *str) { - if (init_process) - fclose (fp); + if (str->open) + fclose (str->file); } + +struct logger +{ + int mask; + int (*printer) (LOGSTREAM *, int prio, const char *fmt, va_list ap); + int (*open) (int flags, LOGSTREAM *); + void (*close) (LOGSTREAM *); +}; + +struct logger logger[] = { + { DIAG_TO_STDERR, stderr_printer, stderr_open, stderr_close }, + { DIAG_TO_SYSLOG, syslog_printer, syslog_open, syslog_close } +}; void -vlogmsg (int prio, const char *fmt, va_list ap) +vdiagmsg (int logf, int prio, const char *fmt, va_list ap) { - if (DIAG_OUTPUT (DIAG_TO_STDERR)) + int i; + for (i = 0; i < ARRAY_SIZE (logger); i++) { - va_list aq; - FILE *fp = stderr_open (); - if (!fp) - return; - fprintf (fp, "%s: ", program_name); - va_copy (aq, ap); - vfprintf (fp, fmt, aq); - va_end (aq); - fprintf (fp, "\n"); - stderr_close (fp); + if (logger[i].mask & (logf & DIAG_TO_MASK)) + { + LOGSTREAM stream; + if (logger[i].open (logf, &stream) == 0) + { + logger[i].printer (&stream, prio, fmt, ap); + logger[i].close (&stream); + } + } } - - if (DIAG_OUTPUT (DIAG_TO_SYSLOG)) - syslog_printer (prio, fmt, ap); +} + +void +diagmsg (int logf, int prio, const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + vdiagmsg (logf, prio, fmt, ap); + va_end (ap); +} + +unsigned debug_level; +int source_info_option; +int diag_output = DIAG_TO_STDERR; + +void +diag_setup (int flags) +{ + if (flags) + diag_output = flags; + if (diag_output & DIAG_TO_SYSLOG) + openlog (log_tag, LOG_PID, log_facility); +} + +void +vlogmsg (int prio, const char *fmt, va_list ap) +{ + vdiagmsg (diag_output, prio, fmt, ap); } void @@ -102,7 +179,6 @@ debug_msg (const char *fmt, ...) } - void logmsg_vprintf (int prio, const char *fmt, va_list ap) { diff --git a/src/pies.c b/src/pies.c index 4212b4e..046f681 100644 --- a/src/pies.c +++ b/src/pies.c @@ -1968,7 +1968,7 @@ main (int argc, char **argv) /* Set default logging */ if (init_process) - diag_flags = DIAG_TO_STDERR; + diag_flags = DIAG_TO_STDERR | DIAG_REOPEN_LOG; else diag_flags = DIAG_TO_SYSLOG | (stderr_closed_p () ? 0 : DIAG_TO_STDERR); @@ -2137,8 +2137,9 @@ main (int argc, char **argv) (unsigned long) pid); exit (EX_USAGE); } - - logmsg (LOG_INFO, _("%s %s starting"), proginfo.package, proginfo.version); + + logmsg (LOG_INFO, + _("%s %s starting"), proginfo.package, proginfo.version); if (!foreground) { diff --git a/src/pies.h b/src/pies.h index 4a6160f..9a3dfca 100644 --- a/src/pies.h +++ b/src/pies.h @@ -446,8 +446,12 @@ int meta1parse (void); /* diag.c */ -#define DIAG_TO_SYSLOG 0x1 -#define DIAG_TO_STDERR 0x2 +#define DIAG_TO_SYSLOG 0x01 +#define DIAG_TO_STDERR 0x02 +#define DIAG_TO_MASK 0x0f +#define DIAG_REOPEN_LOG 0x10 + +#define DIAG_ALL (DIAG_REOPEN_LOG|DIAG_TO_STDERR|DIAG_TO_SYSLOG) extern int diag_output; @@ -463,6 +467,9 @@ void diag_setup (int flags); # define PIES_PRINTFLIKE(fmt,narg) __attribute__ ((__format__ (__printf__, fmt, narg))) #endif +void diagmsg (int logf, int prio, const char *fmt, ...) + PIES_PRINTFLIKE(3,4); + void logmsg (int prio, const char *fmt, ...) PIES_PRINTFLIKE(2,3); void logmsg_printf (int prio, const char *fmt, ...) PIES_PRINTFLIKE(2,3); void logmsg_vprintf (int prio, const char *fmt, va_list ap); diff --git a/src/sysvinit.c b/src/sysvinit.c index 7fad00b..87a52d6 100644 --- a/src/sysvinit.c +++ b/src/sysvinit.c @@ -61,6 +61,8 @@ int prevlevel = 'N'; int initdefault; /* Default runlevel */ int dfl_level; int emergency_shell; + +#define DIAG_CON (DIAG_TO_STDERR|DIAG_REOPEN_LOG) int console_open (int mode) @@ -430,6 +432,8 @@ sysvinit_set_runlevel (int newlevel) case 'A': case 'B': case 'C': + diagmsg (DIAG_ALL, LOG_INFO, + "Activating on-demand level '%c'", newlevel); sysvinit_demand (newlevel); break; @@ -437,10 +441,11 @@ sysvinit_set_runlevel (int newlevel) if (newlevel == runlevel) { } - if (runlevel_index (newlevel) == -1) + else if (runlevel_index (newlevel) == -1) return -1; else { + diagmsg (DIAG_ALL, LOG_INFO, "Switching to runlevel: %c", newlevel); dfl_level = newlevel; raise (SIGALRM); } @@ -827,6 +832,7 @@ inittrans () /* boot -> normal */ create_fifo (); ctl_open (); + diag_setup (DIAG_TO_SYSLOG | DIAG_REOPEN_LOG); } } if (newlevel && newlevel != runlevel) @@ -849,10 +855,12 @@ inittrans () /* Stop disabled programs */ sig = SIGTERM; + diagmsg (DIAG_CON, LOG_INFO, "Sending processes the TERM signal"); progman_foreach (terminate_disabled, &sig); if (progman_wait_until (no_disabled_running, NULL)) { sig = SIGKILL; + diagmsg (DIAG_CON, LOG_INFO, "Sending processes the KILL signal"); progman_foreach (terminate_disabled, &sig); progman_wait_until (no_disabled_running, NULL); } -- cgit v1.2.1