-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patherror.c
185 lines (154 loc) · 3.17 KB
/
error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*_
* Copyright 2009 Scyphus Solutions Co.,Ltd. All rights reserved.
*
* Authors:
* Hirochika Asai <asai@scyphus.co.jp>
*/
/* $Id: error.c,v 3fd7b0a2108d 2011/02/11 09:36:32 Hirochika $ */
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
int error_syslog = 0;
static void
error_printf(int, int, const char *, va_list);
/* Print error message. */
void
error_msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(0, LOG_INFO, fmt, ap);
va_end(ap);
}
/* Print system error message. */
void
error_sys_msg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(1, LOG_INFO, fmt, ap);
va_end(ap);
}
/* Print notice message. */
void
error_notice(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(0, LOG_NOTICE, fmt, ap);
va_end(ap);
}
/* Print system warning message. */
void
error_sys_notice(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(1, LOG_NOTICE, fmt, ap);
va_end(ap);
}
/* Print warning message. */
void
error_warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(0, LOG_WARNING, fmt, ap);
va_end(ap);
}
/* Print system warning message. */
void
error_sys_warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(1, LOG_WARNING, fmt, ap);
va_end(ap);
}
/* Print error message and quit the program. */
void
error_quit(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(0, LOG_ERR, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
/* Print system error message and quit the program. */
void
error_sys(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(1, LOG_ERR, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
/* Print error message and quit the program with a status. */
void
error_exit(int status, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(0, LOG_ERR, fmt, ap);
va_end(ap);
exit(status);
}
/* Print error message and abort. */
void
error_dump(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
error_printf(1, LOG_ERR, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(EXIT_FAILURE); /* shouldn't get here */
}
/**
* error_printf
*/
static void
error_printf(int errnoflag, int level, const char *fmt, va_list ap)
{
int errno_save, n;
char buf[MAXLINE];
errno_save = errno;
vsnprintf(buf, sizeof(buf), fmt, ap);
n = strlen(buf);
if ( errnoflag ) {
snprintf(buf+n, sizeof(buf)-n, ": %s", strerror(errno_save));
}
strcat(buf, "\n");
if ( error_syslog ) {
/* output to syslog */
syslog(level, buf);
} else {
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr);
}
}
void
error_enable_syslog(void)
{
error_syslog = 1;
}
void
error_disable_syslog(void)
{
error_syslog = 0;
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/