-
Notifications
You must be signed in to change notification settings - Fork 36
/
error.c
157 lines (134 loc) · 2.99 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
/* error.c
* This file is part of kplex
* Copyright Keith Young 2012 - 2016
* For copying information see the file COPYING distributed with this software
*
* This files contains error handling and logging functions
*/
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#define IDENT "kplex"
/* This should not be changed once we start multiple threads */
static int facility = -1;
void initlog(int where)
{
if (facility >=0)
closelog();
if ((facility=where) >= 0)
openlog(IDENT,LOG_NOWAIT,facility);
}
void logdebug(int err, char *fmt, ...)
{
char *str;
char ebuf[128];
va_list ap;
va_start(ap,fmt);
if (facility >= 0) {
if (err && ((str = malloc(strlen(fmt) + 5)) != NULL)) {
strcpy(str,fmt);
strcat(str,": %m");
} else {
str = fmt;
}
vsyslog(LOG_DEBUG,str,ap);
if (str != fmt)
free(str);
} else {
fprintf(stderr,"%s DEBUG: ",IDENT);
vfprintf(stderr,fmt,ap);
if (err) {
if (strerror_r(err,ebuf,128) == 0 || errno == ERANGE)
fprintf(stderr,": %s",ebuf);
else
fprintf(stderr,": Unknown Error");
}
fputc('\n',stderr);
}
va_end(ap);
return;
}
void loginfo(char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
if (facility >= 0)
vsyslog(LOG_INFO,fmt,ap);
else {
vfprintf(stderr,fmt,ap);
fputc('\n',stderr);
}
va_end(ap);
return;
}
void logwarn(char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
if (facility >= 0) {
vsyslog(LOG_WARNING,fmt,ap);
} else {
vfprintf(stderr,fmt,ap);
fputc('\n',stderr);
}
va_end(ap);
return;
}
void logerr2(int err, char *fmt, va_list args)
{
char *str;
char ebuf[128];
if (facility >= 0) {
if (err && ((str = malloc(strlen(fmt) + 5)) != NULL)) {
strcpy(str,fmt);
strcat(str,": %m");
} else
str = fmt;
vsyslog(LOG_ERR,str,args);
if (str != fmt)
free(str);
} else {
vfprintf(stderr,fmt,args);
if (err) {
if (strerror_r(err,ebuf,128) == 0 || errno == ERANGE)
fprintf(stderr,": %s",ebuf);
else
fprintf(stderr,": Unknown Error");
}
fputc('\n',stderr);
}
return;
}
void logerr(int err, char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
logerr2(err,fmt,ap);
va_end(ap);
}
void logterm(int err, char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
logerr2(err,fmt,ap);
va_end(ap);
err=1;
pthread_exit(&err);
}
void logtermall(int err, char *fmt, ...)
{
va_list ap;
va_start(ap,fmt);
logerr2(err,fmt,ap);
va_end(ap);
kill(getpid(),SIGINT);
err=1;
pthread_exit(&err);
}