aboutsummaryrefslogtreecommitdiff
path: root/src/diag.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2016-02-18 08:12:39 +0200
committerSergey Poznyakoff <gray@gnu.org>2016-02-18 08:17:35 +0200
commitd5302613a00915076b945b25b50eb6b376121955 (patch)
tree58c8ba0120c52632f008dff191532afc8decaf79 /src/diag.c
parente430f586ccf6621136089fb1d9d865ce1a351360 (diff)
downloadpies-d5302613a00915076b945b25b50eb6b376121955.tar.gz
pies-d5302613a00915076b945b25b50eb6b376121955.tar.bz2
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.
Diffstat (limited to 'src/diag.c')
-rw-r--r--src/diag.c152
1 files changed, 114 insertions, 38 deletions
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)
{

Return to:

Send suggestions and report system problems to the System administrator.