-
Notifications
You must be signed in to change notification settings - Fork 22
/
error.cpp
182 lines (165 loc) · 4.38 KB
/
error.cpp
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
/* (c) William Edwards, 2011
Using the Simplified BSD License. See LICENSE file for details */
#include "error.hpp"
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include "valgrind/memcheck.h"
//#define ASSERT_ON_ERROR
void Error::dump_context(const ErrorContext* context,FILE* out) {
if(context) {
context->dump_context(out);
fprintf(out,"<%"PRIxPTR"> ",(intptr_t)context);
} else
fprintf(out,"<no context> ");
}
static char ErrorMessageBuf[100];
void ThrowClientError(const char* fmt,...) {
struct CE: public Error {
void release() {}
void dump(const ErrorContext* context,FILE* out) {
dump_context(context,out);
fprintf(out,"client error");
if(*ErrorMessageBuf)
fprintf(out,": %s",ErrorMessageBuf);
fputc('\n',out);
};
} static client_error;
va_list ap;
va_start(ap,fmt);
*ErrorMessageBuf = 0;
vsnprintf(ErrorMessageBuf,sizeof(ErrorMessageBuf),fmt,ap);
va_end(ap);
VALGRIND_PRINTF_BACKTRACE("Server Client Error: %s\n",ErrorMessageBuf);
throw &client_error;
}
void ThrowCError(const char* msg,const char* file,int line) {
struct CE: public Error {
void release() {}
void dump(const ErrorContext* context,FILE* out) {
dump_context(context,out);
fprintf(out,"c error: %d (%s) %s @ %s:%d\n",err,strerror(err),msg,file,line);
}
const char* msg;
const char* file;
int line;
int err;
} static c_error;
if(EINTR == errno)
ThrowShutdown("program interrupted");
c_error.msg = msg;
c_error.file = file;
c_error.line = line;
c_error.err = errno;
#ifdef ASSERT_ON_ERROR
assert(!"ThrowCError");
#endif
VALGRIND_PRINTF_BACKTRACE("Server C Error: %d (%s) %s\n",errno,strerror(errno),msg);
throw &c_error;
}
void ThrowEndOfStreamError() {
struct EOS: public virtual EndOfStreamError {
void release() {}
void dump(const ErrorContext* context,FILE* out) {
dump_context(context,out);
fprintf(out,"end of stream\n");
}
} static eos;
throw &eos;
}
void ThrowInternalError(const char* fmt,...) {
struct IE: public Error {
void release() {}
void dump(const ErrorContext* context,FILE* out) {
dump_context(context,out);
fprintf(out,"internal error");
if(*ErrorMessageBuf)
fprintf(out,": %s",ErrorMessageBuf);
fputc('\n',out);
};
} static internal_error;
va_list ap;
va_start(ap,fmt);
*ErrorMessageBuf = 0;
vsnprintf(ErrorMessageBuf,sizeof(ErrorMessageBuf),fmt,ap);
va_end(ap);
#ifdef ASSERT_ON_ERROR
assert(!"ThrowInternalError");
#endif
VALGRIND_PRINTF_BACKTRACE("Server Internal Error: %s\n",ErrorMessageBuf);
throw &internal_error;
}
void ThrowGracefulClose(const char* msg) {
struct GracefulClose: public virtual HalfClose {
void release() {}
void dump(const ErrorContext* context,FILE* out) {
if(msg && *msg) {
dump_context(context,out);
fprintf(out,"%s\n",msg);
}
}
} static graceful_close;
graceful_close.msg = msg;
VALGRIND_PRINTF_BACKTRACE("Server Graceful Close: %s\n",graceful_close.msg);
throw &graceful_close;
}
void ThrowShutdown(const char* msg) {
static Shutdown shutdown;
shutdown.msg = msg;
throw &shutdown;
}
static unsigned LogFlags = (LOG_CRITICAL); //|LOG_CONN|LOG_DEBUG);
bool Log(LogLevel level) { return (LogFlags&level); }
void SetLog(LogLevel level,bool enable) {
if(enable)
LogFlags |= level;
else
LogFlags &= ~level;
}
void InitLog(const char* fname) {
int pipe_fd[2];
check(pipe(pipe_fd));
const pid_t pid = fork();
check(pid);
if(!pid) { // our log child
close(pipe_fd[1]); // Close unused write end
FILE* logFile = fname? fopen(fname,"a"): NULL;
if(fname && !logFile)
fprintf(stderr,"cannot open log file \"%s\": %d (%s)\n",fname,errno,strerror(errno));
char ch;
bool timestamp = true;
while(read(pipe_fd[0],&ch,1) > 0) {
if(timestamp) {
char timebuf[64];
time_t t = time(NULL);
strftime(timebuf,sizeof(timebuf),"%y%m%d %T",localtime(&t));
printf("%s ",timebuf);
if(logFile)
fprintf(logFile,"%s ",timebuf);
timestamp = false;
}
putchar(ch);
if(logFile)
fputc(ch,logFile);
if('\n'==ch) {
timestamp = true;
fflush(stdout);
if(logFile)
fflush(logFile);
}
}
putchar('\n');
close(pipe_fd[0]);
if(logFile)
fclose(logFile);
exit(EXIT_SUCCESS);
} else { // Server main process
close(pipe_fd[0]); // Close unused read end
// redirect stdout and stderr
dup2(pipe_fd[1],STDOUT_FILENO);
dup2(pipe_fd[1],STDERR_FILENO);
close(pipe_fd[1]);
}
}