3353
|
1 |
/*
|
|
2 |
|
|
3 |
module: errlib.c
|
|
4 |
|
|
5 |
purpose: library of error functions
|
|
6 |
|
|
7 |
reference: Stevens, Unix network programming (2ed), p.922
|
|
8 |
|
|
9 |
*/
|
|
10 |
|
|
11 |
#include "errlib.h"
|
|
12 |
|
|
13 |
|
|
14 |
#define MAXLINE 4095
|
|
15 |
|
|
16 |
#ifdef __CPLUSPLUS
|
|
17 |
extern "C" {
|
|
18 |
#endif
|
|
19 |
|
|
20 |
int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
|
|
21 |
|
|
22 |
static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
|
|
23 |
{
|
|
24 |
int errno_save = errno, n;
|
|
25 |
char buf[MAXLINE+1];
|
|
26 |
|
|
27 |
vsnprintf (buf, MAXLINE, fmt, ap);
|
|
28 |
n = strlen(buf);
|
|
29 |
if (errnoflag)
|
|
30 |
snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
|
|
31 |
strcat (buf, "\n");
|
|
32 |
|
|
33 |
if (daemon_proc)
|
|
34 |
syslog (level, buf);
|
|
35 |
else {
|
|
36 |
fflush (stdout);
|
|
37 |
fprintf (stderr, "%s", buf);
|
|
38 |
fflush (stderr);
|
|
39 |
}
|
|
40 |
|
|
41 |
return;
|
|
42 |
}
|
|
43 |
|
|
44 |
void err_ret (const char *fmt, ...)
|
|
45 |
{
|
|
46 |
va_list ap;
|
|
47 |
|
|
48 |
va_start (ap, fmt);
|
|
49 |
err_doit (1, LOG_INFO, fmt, ap);
|
|
50 |
va_end (ap);
|
|
51 |
return;
|
|
52 |
}
|
|
53 |
|
|
54 |
void err_sys (const char *fmt, ...)
|
|
55 |
{
|
|
56 |
va_list ap;
|
|
57 |
|
|
58 |
va_start (ap, fmt);
|
|
59 |
err_doit (1, LOG_ERR, fmt, ap);
|
|
60 |
va_end (ap);
|
|
61 |
exit (1);
|
|
62 |
}
|
|
63 |
|
|
64 |
void err_msg (const char *fmt, ...)
|
|
65 |
{
|
|
66 |
va_list ap;
|
|
67 |
|
|
68 |
va_start (ap, fmt);
|
|
69 |
err_doit (0, LOG_INFO, fmt, ap);
|
|
70 |
va_end (ap);
|
|
71 |
return;
|
|
72 |
}
|
|
73 |
|
|
74 |
void err_quit (const char *fmt, ...)
|
|
75 |
{
|
|
76 |
va_list ap;
|
|
77 |
|
|
78 |
va_start (ap, fmt);
|
|
79 |
err_doit (0, LOG_ERR, fmt, ap);
|
|
80 |
va_end (ap);
|
|
81 |
exit (1);
|
|
82 |
}
|
|
83 |
|
|
84 |
void err_dump (const char *fmt, ...)
|
|
85 |
{
|
|
86 |
va_list ap;
|
|
87 |
|
|
88 |
va_start (ap, fmt);
|
|
89 |
err_doit (1, LOG_ERR, fmt, ap);
|
|
90 |
va_end (ap);
|
|
91 |
abort();
|
|
92 |
}
|
|
93 |
|
|
94 |
#ifdef __CPLUSPLUS
|
|
95 |
}
|
|
96 |
#endif
|