OSDN Git Service

modified code to prevent DB inflation
[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;  /* daemon procedure or not */
13
14 static void     err_doit(int, int, const char *, va_list);
15
16 /**********************************************/
17 /* set flag of syslog usage                   */
18 /* i:(input) TRUE(1)=use syslog (daemon proc) */
19 /*           FALSE(0)=use stdout (shell proc) */
20 /**********************************************/
21 void errToSyslog(int i)
22 {
23   daemon_proc=i;
24 }
25
26 /*************************************************/
27 /* Non-fatal error related to a system call.     */
28 /* Print a message and return.                   */
29 /*************************************************/
30 void
31 err_ret(const char *fmt, ...)
32 {
33         va_list         ap;
34
35         va_start(ap, fmt);
36         err_doit(1, LOG_INFO, fmt, ap);
37         va_end(ap);
38         return;
39 }
40
41 /*************************************************/
42 /* Fatal error related to a system call.
43  * Print a message and terminate. */
44 /*************************************************/
45 void
46 err_sys(const char *fmt, ...)
47 {
48         va_list         ap;
49
50         va_start(ap, fmt);
51         err_doit(1, LOG_ERR, fmt, ap);
52         va_end(ap);
53         exit(1);
54 }
55
56 /*************************************************/
57 /* Fatal error related to a system call.
58  * Print a message, dump core, and terminate. */
59 /*************************************************/
60 void
61 err_dump(const char *fmt, ...)
62 {
63         va_list         ap;
64
65         va_start(ap, fmt);
66         err_doit(1, LOG_ERR, fmt, ap);
67         va_end(ap);
68         abort();                /* dump core and terminate */
69         exit(1);                /* shouldn't get here */
70 }
71
72 /*************************************************/
73 /* Nonfatal error unrelated to a system call.
74  * Print a message and return. */
75 /*************************************************/
76 void
77 err_msg(const char *fmt, ...)
78 {
79         va_list         ap;
80
81         va_start(ap, fmt);
82         err_doit(0, LOG_INFO, fmt, ap);
83         va_end(ap);
84         return;
85 }
86
87 /*************************************************/
88 /* Fatal error unrelated to a system call.
89  * Print a message and terminate. */
90 /*************************************************/
91 void
92 err_quit(const char *fmt, ...)
93 {
94         va_list         ap;
95
96         va_start(ap, fmt);
97         err_doit(0, LOG_ERR, fmt, ap);
98         va_end(ap);
99         exit(1);
100 }
101
102
103 /*************************************************/
104 /* Print a message and return to caller.
105  * Caller specifies "errnoflag" and "level". */
106 /*************************************************/
107 static void
108 err_doit(int errnoflag, int level, const char *fmt, va_list ap)
109 {
110         int             errno_save, n;
111         char    buf[BUFFMAXLN];
112
113         errno_save = errno;             /* value caller might want printed */
114 #ifdef  HAVE_VSNPRINTF
115         vsnprintf(buf, sizeof(buf), fmt, ap);   /* this is safe */
116 #else
117         vsprintf(buf, fmt, ap);                                 /* this is not safe */
118 #endif
119         n = strlen(buf);
120         if (errnoflag)
121                 snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
122         strcat(buf, "\n");
123
124         if (daemon_proc) {
125                 syslog(level, "%s", buf);
126         } else {
127                 fflush(stdout);         /* in case stdout and stderr are the same */
128                 fputs(buf, stderr);
129                 fflush(stderr);
130         }
131         return;
132 }
133
134
135 /*************************
136  terminate this program
137  used for debug purpose
138 *************************/
139 void terminateProg(int ret){
140   exit(ret);
141 }