2418
|
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 |
#define MAXLINE 4095
|
|
14 |
|
2444
|
15 |
int daemon_proc = 0; /* set to 0 if stdout/stderr available, else set to 1 */
|
|
16 |
|
|
17 |
static void err_doit (int errnoflag, int level, const char *fmt, va_list ap)
|
|
18 |
{
|
|
19 |
int errno_save = errno, n;
|
|
20 |
char buf[MAXLINE+1];
|
2418
|
21 |
|
2444
|
22 |
vsnprintf (buf, MAXLINE, fmt, ap);
|
|
23 |
n = strlen(buf);
|
|
24 |
if (errnoflag)
|
|
25 |
snprintf (buf+n, MAXLINE-n, ": %s", strerror(errno_save));
|
|
26 |
strcat (buf, "\n");
|
2418
|
27 |
|
2444
|
28 |
if (daemon_proc)
|
|
29 |
syslog (level, buf);
|
|
30 |
else {
|
|
31 |
fflush (stdout);
|
|
32 |
fprintf (stderr, buf);
|
|
33 |
fflush (stderr);
|
2418
|
34 |
}
|
|
35 |
|
2444
|
36 |
return;
|
|
37 |
}
|
|
38 |
|
|
39 |
void err_ret (const char *fmt, ...)
|
|
40 |
{
|
|
41 |
va_list ap;
|
2418
|
42 |
|
2444
|
43 |
va_start (ap, fmt);
|
|
44 |
err_doit (1, LOG_INFO, fmt, ap);
|
|
45 |
va_end (ap);
|
|
46 |
return;
|
|
47 |
}
|
|
48 |
|
|
49 |
void err_sys (const char *fmt, ...)
|
|
50 |
{
|
|
51 |
va_list ap;
|
|
52 |
|
|
53 |
va_start (ap, fmt);
|
|
54 |
err_doit (1, LOG_ERR, fmt, ap);
|
|
55 |
va_end (ap);
|
|
56 |
exit (1);
|
|
57 |
}
|
|
58 |
|
|
59 |
void err_msg (const char *fmt, ...)
|
|
60 |
{
|
|
61 |
va_list ap;
|
2418
|
62 |
|
2444
|
63 |
va_start (ap, fmt);
|
|
64 |
err_doit (0, LOG_INFO, fmt, ap);
|
|
65 |
va_end (ap);
|
|
66 |
return;
|
|
67 |
}
|
|
68 |
|
|
69 |
void err_quit (const char *fmt, ...)
|
|
70 |
{
|
|
71 |
va_list ap;
|
2418
|
72 |
|
2444
|
73 |
va_start (ap, fmt);
|
|
74 |
err_doit (0, LOG_ERR, fmt, ap);
|
|
75 |
va_end (ap);
|
|
76 |
exit (1);
|
|
77 |
}
|
|
78 |
|
|
79 |
void err_dump (const char *fmt, ...)
|
|
80 |
{
|
|
81 |
va_list ap;
|
2418
|
82 |
|
2444
|
83 |
va_start (ap, fmt);
|
|
84 |
err_doit (1, LOG_ERR, fmt, ap);
|
|
85 |
va_end (ap);
|
|
86 |
abort();
|
2418
|
87 |
}
|
2444
|
88 |
|