OSDN Git Service

Ver.0.8.1
[opengatem/opengatem.git] / mngsrc / 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 "opengatemmng.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 /* Fatal error unrelated to a system call.
84  * Print a message and terminate. */
85 /*************************************************/
86 void
87 err_quit(const char *fmt, ...)
88 {
89         va_list         ap;
90
91         va_start(ap, fmt);
92         err_doit(0, LOG_ERR, fmt, ap);
93         va_end(ap);
94         exit(1);
95 }
96
97
98 /*************************************************/
99 /* Print a message and return to caller.
100  * Caller specifies "errnoflag" and "level". */
101 /*************************************************/
102 static void
103 err_doit(int errnoflag, int level, const char *fmt, va_list ap)
104 {
105         int             errno_save, n;
106         char    buf[BUFFMAXLN];
107
108         errno_save = errno;             /* value caller might want printed */
109 #ifdef  HAVE_VSNPRINTF
110         vsnprintf(buf, sizeof(buf), fmt, ap);   /* this is safe */
111 #else
112         vsprintf(buf, fmt, ap);                                 /* this is not safe */
113 #endif
114         n = strlen(buf);
115         if (errnoflag)
116                 snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
117         strcat(buf, "\n");
118
119         if (daemon_proc) {
120                 syslog(level, buf);
121         } else {
122                 fflush(stdout);         /* in case stdout and stderr are the same */
123                 fputs(buf, stderr);
124                 fflush(stderr);
125         }
126         return;
127 }
128
129
130 /*************************
131  terminate this program
132  used for debug purpose
133 *************************/
134 void terminateProg(int ret){
135   exit(ret);
136 }