aboutsummaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/diag.c152
-rw-r--r--src/pies.c7
-rw-r--r--src/pies.h11
-rw-r--r--src/sysvinit.c10
4 files changed, 136 insertions, 44 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 @@
16 16
17#include "pies.h" 17#include "pies.h"
18 18
19unsigned debug_level; 19typedef struct
20int source_info_option;
21int diag_output = DIAG_TO_STDERR;
22
23void
24diag_setup (int flags)
25{ 20{
26 if (flags) 21 int open;
27 diag_output = flags; 22 FILE *file;
28 if (diag_output & DIAG_TO_SYSLOG) 23} LOGSTREAM;
29 openlog (log_tag, LOG_PID, log_facility);
30}
31 24
32void 25
33syslog_printer (int prio, const char *fmt, va_list ap) 26static int
27syslog_printer (LOGSTREAM *str, int prio, const char *fmt, va_list ap)
34{ 28{
35#if HAVE_VSYSLOG 29#if HAVE_VSYSLOG
36 vsyslog (prio, fmt, ap); 30 vsyslog (prio, fmt, ap);
@@ -39,48 +33,131 @@ syslog_printer (int prio, const char *fmt, va_list ap)
39 vsnprintf (buf, sizeof buf, fmt, ap); 33 vsnprintf (buf, sizeof buf, fmt, ap);
40 syslog (prio, "%s", buf); 34 syslog (prio, "%s", buf);
41#endif 35#endif
36 return 0;
42} 37}
43 38
44static FILE * 39static int
45stderr_open () 40syslog_open (int logf, LOGSTREAM *str)
46{ 41{
47 if (!init_process) 42 if (logf & DIAG_REOPEN_LOG)
48 return stderr; 43 {
44 openlog (log_tag, LOG_PID, log_facility);
45 str->open = 1;
46 }
49 else 47 else
48 str->open = 0;
49 return 0;
50}
51
52static void
53syslog_close (LOGSTREAM *str)
54{
55 if (str->open)
56 closelog ();
57}
58
59static int
60stderr_printer (LOGSTREAM *str, int prio, const char *fmt, va_list ap)
61{
62 FILE *fp = str->file;
63 va_list aq;
64
65 fprintf (fp, "%s: ", program_name);
66 va_copy (aq, ap);
67 vfprintf (fp, fmt, aq);
68 va_end (aq);
69 fprintf (fp, "\n");
70 return 0;
71}
72
73static int
74stderr_open (int logf, LOGSTREAM *str)
75{
76 if (logf & DIAG_REOPEN_LOG)
50 { 77 {
51 int fd = console_open (O_WRONLY|O_NOCTTY|O_NDELAY); 78 int fd = console_open (O_WRONLY|O_NOCTTY|O_NDELAY);
52 if (fd == -1) 79 if (fd == -1)
53 return NULL; 80 return -1;
54 return fdopen (fd, "w"); 81 str->file = fdopen (fd, "w");
82 if (!str->file)
83 {
84 close (fd);
85 return -1;
86 }
87 str->open = 1;
55 } 88 }
89 else
90 {
91 str->file = stderr;
92 str->open = 0;
93 }
94 return 0;
56} 95}
57 96
58static void 97static void
59stderr_close (FILE *fp) 98stderr_close (LOGSTREAM *str)
60{ 99{
61 if (init_process) 100 if (str->open)
62 fclose (fp); 101 fclose (str->file);
63} 102}
103
104struct logger
105{
106 int mask;
107 int (*printer) (LOGSTREAM *, int prio, const char *fmt, va_list ap);
108 int (*open) (int flags, LOGSTREAM *);
109 void (*close) (LOGSTREAM *);
110};
111
112struct logger logger[] = {
113 { DIAG_TO_STDERR, stderr_printer, stderr_open, stderr_close },
114 { DIAG_TO_SYSLOG, syslog_printer, syslog_open, syslog_close }
115};
64 116
65void 117void
66vlogmsg (int prio, const char *fmt, va_list ap) 118vdiagmsg (int logf, int prio, const char *fmt, va_list ap)
67{ 119{
68 if (DIAG_OUTPUT (DIAG_TO_STDERR)) 120 int i;
121 for (i = 0; i < ARRAY_SIZE (logger); i++)
69 { 122 {
70 va_list aq; 123 if (logger[i].mask & (logf & DIAG_TO_MASK))
71 FILE *fp = stderr_open (); 124 {
72 if (!fp) 125 LOGSTREAM stream;
73 return; 126 if (logger[i].open (logf, &stream) == 0)
74 fprintf (fp, "%s: ", program_name); 127 {
75 va_copy (aq, ap); 128 logger[i].printer (&stream, prio, fmt, ap);
76 vfprintf (fp, fmt, aq); 129 logger[i].close (&stream);
77 va_end (aq); 130 }
78 fprintf (fp, "\n"); 131 }
79 stderr_close (fp);
80 } 132 }
81 133}
82 if (DIAG_OUTPUT (DIAG_TO_SYSLOG)) 134
83 syslog_printer (prio, fmt, ap); 135void
136diagmsg (int logf, int prio, const char *fmt, ...)
137{
138 va_list ap;
139 va_start (ap, fmt);
140 vdiagmsg (logf, prio, fmt, ap);
141 va_end (ap);
142}
143
144unsigned debug_level;
145int source_info_option;
146int diag_output = DIAG_TO_STDERR;
147
148void
149diag_setup (int flags)
150{
151 if (flags)
152 diag_output = flags;
153 if (diag_output & DIAG_TO_SYSLOG)
154 openlog (log_tag, LOG_PID, log_facility);
155}
156
157void
158vlogmsg (int prio, const char *fmt, va_list ap)
159{
160 vdiagmsg (diag_output, prio, fmt, ap);
84} 161}
85 162
86void 163void
@@ -102,7 +179,6 @@ debug_msg (const char *fmt, ...)
102} 179}
103 180
104 181
105
106void 182void
107logmsg_vprintf (int prio, const char *fmt, va_list ap) 183logmsg_vprintf (int prio, const char *fmt, va_list ap)
108{ 184{
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)
1968 1968
1969 /* Set default logging */ 1969 /* Set default logging */
1970 if (init_process) 1970 if (init_process)
1971 diag_flags = DIAG_TO_STDERR; 1971 diag_flags = DIAG_TO_STDERR | DIAG_REOPEN_LOG;
1972 else 1972 else
1973 diag_flags = DIAG_TO_SYSLOG | (stderr_closed_p () ? 0 : DIAG_TO_STDERR); 1973 diag_flags = DIAG_TO_SYSLOG | (stderr_closed_p () ? 0 : DIAG_TO_STDERR);
1974 1974
@@ -2137,8 +2137,9 @@ main (int argc, char **argv)
2137 (unsigned long) pid); 2137 (unsigned long) pid);
2138 exit (EX_USAGE); 2138 exit (EX_USAGE);
2139 } 2139 }
2140 2140
2141 logmsg (LOG_INFO, _("%s %s starting"), proginfo.package, proginfo.version); 2141 logmsg (LOG_INFO,
2142 _("%s %s starting"), proginfo.package, proginfo.version);
2142 2143
2143 if (!foreground) 2144 if (!foreground)
2144 { 2145 {
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);
446 446
447 447
448/* diag.c */ 448/* diag.c */
449#define DIAG_TO_SYSLOG 0x1 449#define DIAG_TO_SYSLOG 0x01
450#define DIAG_TO_STDERR 0x2 450#define DIAG_TO_STDERR 0x02
451#define DIAG_TO_MASK 0x0f
452#define DIAG_REOPEN_LOG 0x10
453
454#define DIAG_ALL (DIAG_REOPEN_LOG|DIAG_TO_STDERR|DIAG_TO_SYSLOG)
451 455
452extern int diag_output; 456extern int diag_output;
453 457
@@ -463,6 +467,9 @@ void diag_setup (int flags);
463# define PIES_PRINTFLIKE(fmt,narg) __attribute__ ((__format__ (__printf__, fmt, narg))) 467# define PIES_PRINTFLIKE(fmt,narg) __attribute__ ((__format__ (__printf__, fmt, narg)))
464#endif 468#endif
465 469
470void diagmsg (int logf, int prio, const char *fmt, ...)
471 PIES_PRINTFLIKE(3,4);
472
466