forked from sysprog21/lab0-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.c
315 lines (266 loc) · 7.1 KB
/
report.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include "report.h"
#include "web.h"
#define MAX(a, b) ((a) < (b) ? (b) : (a))
static FILE *errfile = NULL;
static FILE *verbfile = NULL;
static FILE *logfile = NULL;
int verblevel = 0;
static void init_files(FILE *efile, FILE *vfile)
{
errfile = efile;
verbfile = vfile;
}
static char fail_buf[1024] = "FATAL Error. Exiting\n";
static volatile int ret = 0;
/* Default fatal function */
static void default_fatal_fun()
{
ret = write(STDOUT_FILENO, fail_buf, strlen(fail_buf) + 1);
if (logfile)
fputs(fail_buf, logfile);
}
/* Optional function to call when fatal error encountered */
static void (*fatal_fun)() = default_fatal_fun;
void set_verblevel(int level)
{
verblevel = level;
}
bool set_logfile(const char *file_name)
{
logfile = fopen(file_name, "w");
return logfile != NULL;
}
void report_event(message_t msg, char *fmt, ...)
{
va_list ap;
bool fatal = msg == MSG_FATAL;
// cppcheck-suppress constVariable
static char *msg_name_text[N_MSG] = {
"WARNING",
"ERROR",
"FATAL ERROR",
};
char *msg_name = msg_name_text[2];
if (msg < N_MSG)
msg_name = msg_name_text[msg];
int level = N_MSG - msg - 1;
if (verblevel < level)
return;
if (!errfile)
init_files(stdout, stdout);
va_start(ap, fmt);
fprintf(errfile, "%s: ", msg_name);
vfprintf(errfile, fmt, ap);
fprintf(errfile, "\n");
fflush(errfile);
va_end(ap);
if (logfile) {
va_start(ap, fmt);
fprintf(logfile, "Error: ");
vfprintf(logfile, fmt, ap);
fprintf(logfile, "\n");
fflush(logfile);
va_end(ap);
fclose(logfile);
}
if (fatal) {
if (fatal_fun)
fatal_fun();
exit(1);
}
}
#define BUF_SIZE 4096
extern int web_connfd;
void report(int level, char *fmt, ...)
{
if (!verbfile)
init_files(stdout, stdout);
char buffer[BUF_SIZE];
if (level <= verblevel) {
va_list ap;
va_start(ap, fmt);
vfprintf(verbfile, fmt, ap);
fprintf(verbfile, "\n");
fflush(verbfile);
va_end(ap);
if (logfile) {
va_start(ap, fmt);
vfprintf(logfile, fmt, ap);
fprintf(logfile, "\n");
fflush(logfile);
va_end(ap);
}
va_start(ap, fmt);
vsnprintf(buffer, BUF_SIZE, fmt, ap);
va_end(ap);
}
if (web_connfd) {
int len = strlen(buffer);
buffer[len] = '\n';
buffer[len + 1] = '\0';
web_send(web_connfd, buffer);
}
}
void report_noreturn(int level, char *fmt, ...)
{
if (!verbfile)
init_files(stdout, stdout);
char buffer[BUF_SIZE];
if (level <= verblevel) {
va_list ap;
va_start(ap, fmt);
vfprintf(verbfile, fmt, ap);
fflush(verbfile);
va_end(ap);
if (logfile) {
va_start(ap, fmt);
vfprintf(logfile, fmt, ap);
fflush(logfile);
va_end(ap);
}
va_start(ap, fmt);
vsnprintf(buffer, BUF_SIZE, fmt, ap);
va_end(ap);
}
if (web_connfd)
web_send(web_connfd, buffer);
}
/* Functions denoting failures */
/* Need to be able to print without using malloc */
static void fail_fun(const char *format, const char *msg)
{
snprintf(fail_buf, sizeof(fail_buf), format, msg);
/* Tack on return */
fail_buf[strlen(fail_buf)] = '\n';
/* Use write to avoid any buffering issues */
ret = write(STDOUT_FILENO, fail_buf, strlen(fail_buf) + 1);
if (logfile) {
/* Don't know file descriptor for logfile */
fputs(fail_buf, logfile);
}
if (fatal_fun)
fatal_fun();
if (logfile)
fclose(logfile);
exit(1);
}
/* Maximum number of megabytes that application can use (0 = unlimited) */
static int mblimit = 0;
/* Keeping track of memory allocation */
static size_t allocate_cnt = 0;
static size_t allocate_bytes = 0;
static size_t free_cnt = 0;
static size_t free_bytes = 0;
/* Counters giving peak memory usage */
static size_t peak_bytes = 0;
static size_t last_peak_bytes = 0;
static size_t current_bytes = 0;
static void check_exceed(size_t new_bytes)
{
size_t limit_bytes = (size_t) mblimit << 20;
size_t request_bytes = new_bytes + current_bytes;
if (mblimit > 0 && request_bytes > limit_bytes) {
report_event(MSG_FATAL,
"Exceeded memory limit of %u megabytes with %lu bytes",
mblimit, request_bytes);
}
}
/* Call malloc & exit if fails */
void *malloc_or_fail(size_t bytes, const char *fun_name)
{
check_exceed(bytes);
void *p = malloc(bytes);
if (!p) {
fail_fun("Malloc returned NULL in %s", fun_name);
return NULL;
}
allocate_cnt++;
allocate_bytes += bytes;
current_bytes += bytes;
peak_bytes = MAX(peak_bytes, current_bytes);
last_peak_bytes = MAX(last_peak_bytes, current_bytes);
return p;
}
/* Call calloc returns NULL & exit if fails */
void *calloc_or_fail(size_t cnt, size_t bytes, const char *fun_name)
{
check_exceed(cnt * bytes);
void *p = calloc(cnt, bytes);
if (!p) {
fail_fun("Calloc returned NULL in %s", fun_name);
return NULL;
}
allocate_cnt++;
allocate_bytes += cnt * bytes;
current_bytes += cnt * bytes;
peak_bytes = MAX(peak_bytes, current_bytes);
last_peak_bytes = MAX(last_peak_bytes, current_bytes);
return p;
}
char *strsave_or_fail(const char *s, const char *fun_name)
{
if (!s)
return NULL;
size_t len = strlen(s);
check_exceed(len + 1);
char *ss = malloc(len + 1);
if (!ss)
fail_fun("strsave failed in %s", fun_name);
allocate_cnt++;
allocate_bytes += len + 1;
current_bytes += len + 1;
peak_bytes = MAX(peak_bytes, current_bytes);
last_peak_bytes = MAX(last_peak_bytes, current_bytes);
return strncpy(ss, s, len + 1);
}
/* Free block, as from malloc, realloc, or strsave */
void free_block(void *b, size_t bytes)
{
if (!b)
report_event(MSG_ERROR, "Attempting to free null block");
free(b);
free_cnt++;
free_bytes += bytes;
current_bytes -= bytes;
}
/* Free array, as from calloc */
void free_array(void *b, size_t cnt, size_t bytes)
{
if (!b)
report_event(MSG_ERROR, "Attempting to free null block");
free(b);
free_cnt++;
free_bytes += cnt * bytes;
current_bytes -= cnt * bytes;
}
/* Free string saved by strsave_or_fail */
void free_string(char *s)
{
if (!s)
report_event(MSG_ERROR, "Attempting to free null block");
free_block((void *) s, strlen(s) + 1);
}
/* Initialization of timers */
void init_time(double *timep)
{
(void) delta_time(timep);
}
double delta_time(double *timep)
{
struct timeval tv;
gettimeofday(&tv, NULL);
double current_time = tv.tv_sec + 1.0E-6 * tv.tv_usec;
double delta = current_time - *timep;
*timep = current_time;
return delta;
}