OSDN Git Service

Fixed CLang warning messages and other
[opengatem/opengatem.git] / mdsrc / error.c
1 /**********************************************************/
2 /* output error message to syslog or stdout               */
3 /*  from UNIX NETWORK PROGRAMMING,Vol.1,Second Edition,   */
4 /*      By W. Richard Stevens, Published By Prentice Hall */
5 /*       ftp://ftp.kohala.com/pub/rstevens/unpv12e.tar.gz */
6 /**********************************************************/
7
8 #include "opengatemd.h"
9 #include        <stdarg.h>              /* ANSI C header file */
10 #include        <syslog.h>              /* for syslog() */
11
12 int             daemon_proc;
13
14 static void     err_doit(int, int, const char *, va_list);
15
16 /*************************************************/
17 /* Nonfatal error related to a system call.
18  * Print a message and return. */
19 /*************************************************/
20 void errToSyslog(int i)
21 {
22   daemon_proc=i;
23 }
24
25 void
26 err_ret(const char *fmt, ...)
27 {
28         va_list         ap;
29
30         va_start(ap, fmt);
31         err_doit(1, LOG_INFO, fmt, ap);
32         va_end(ap);
33         return;
34 }
35
36 /*************************************************/
37 /* Fatal error related to a system call.
38  * Print a message and terminate. */
39 /*************************************************/
40 void
41 err_sys(const char *fmt, ...)
42 {
43         va_list         ap;
44
45         va_start(ap, fmt);
46         err_doit(1, LOG_ERR, fmt, ap);
47         va_end(ap);
48         exit(1);
49 }
50
51 /*************************************************/
52 /* Fatal error related to a system call.
53  * Print a message, dump core, and terminate. */
54 /*************************************************/
55 void
56 err_dump(const char *fmt, ...)
57 {
58         va_list         ap;
59
60         va_start(ap, fmt);
61         err_doit(1, LOG_ERR, fmt, ap);
62         va_end(ap);
63         abort();                /* dump core and terminate */
64         exit(1);                /* shouldn't get here */
65 }
66
67 /*************************************************/
68 /* Nonfatal error unrelated to a system call.
69  * Print a message and return. */
70 /*************************************************/
71 void
72 err_msg(const char *fmt, ...)
73 {
74         va_list         ap;
75
76         va_start(ap, fmt);
77         err_doit(0, LOG_INFO, fmt, ap);
78         va_end(ap);
79         return;
80 }
81
82 /*************************************************/
83 /* Nonfatal error unrelated to a system call.
84  * Print a message and return. 
85  * Write with WARNING property */
86 /*************************************************/
87 void
88 err_msg_warn(const char *fmt, ...)
89 {
90         va_list         ap;
91
92         va_start(ap, fmt);
93         err_doit(0, LOG_WARNING, fmt, ap);
94         va_end(ap);
95         return;
96 }
97
98 /*************************************************/
99 /* Fatal error unrelated to a system call.
100  * Print a message and terminate. */
101 /*************************************************/
102 void
103 err_quit(const char *fmt, ...)
104 {
105         va_list         ap;
106
107         va_start(ap, fmt);
108         err_doit(0, LOG_ERR, fmt, ap);
109         va_end(ap);
110         exit(1);
111 }
112
113
114 /*************************************************/
115 /* Print a message and return to caller.
116  * Caller specifies "errnoflag" and "level". */
117 /*************************************************/
118 static void
119 err_doit(int errnoflag, int level, const char *fmt, va_list ap)
120 {
121         int             errno_save, n;
122         char    buf[BUFFMAXLN];
123
124         errno_save = errno;             /* value caller might want printed */
125 #ifdef  HAVE_VSNPRINTF
126         vsnprintf(buf, sizeof(buf), fmt, ap);   /* this is safe */
127 #else
128         vsprintf(buf, fmt, ap);                                 /* this is not safe */
129 #endif
130         n = strlen(buf);
131         if (errnoflag)
132                 snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
133         strcat(buf, "\n");
134
135         if (daemon_proc) {
136                 syslog(level, "%s", buf);
137         } else {
138                 fflush(stdout);         /* in case stdout and stderr are the same */
139                 fputs(buf, stderr);
140                 fflush(stderr);
141         }
142         return;
143 }
144
145
146