-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
278 lines (225 loc) · 6.29 KB
/
log.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
#include "log.h"
#include "set_handler.h"
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/timeb.h>
struct log_file *log_files = NULL;
struct timeb start = {0, 0, 0, 0};
char stamp[10];
void sig_handler(int signal);
loglevel_t priority_encoder(loglevel_t level);
struct timeb get_elapsed(void);
struct log_file *open_log(const char* filename, loglevel_t maxlevel) {
FILE *file = fopen(filename, "ab");
if ( file == NULL ) {
perror("Errore fopen()");
exit(EXIT_FAILURE);
}
return new_log(file, maxlevel, TRUE); /* wrapped */
}
struct log_file *new_log(FILE *file, loglevel_t maxlevel, bool wrap) {
time_t now = time(NULL);
struct log_file *new = malloc(sizeof(struct log_file));
assert(file != NULL);
check_alloc(new);
new->file = file;
new->maxlevel = maxlevel;
new->wrap = wrap;
new->prompted = FALSE;
new->auto_prompt = FALSE;
new->prompt = FALSE;
new->next = NULL;
if ( start.time == 0 ) ftime(&start);
/* We don't want log delimiters on the console */
if ( wrap && file != stdout && file != stderr ) {
struct timeb elapsed = get_elapsed();
fprintf(file, TIMESTAMP_FORMAT "======== Opening logfile at %s",
(int) elapsed.time, elapsed.millitm, ctime(&now));
/* ctime() terminates with \n\0 */
}
if ( log_files != NULL ) {
struct log_file *ptr = log_files;
while ( ptr->next != NULL ) ptr = ptr->next;
ptr->next = new;
} else {
log_files = new;
/* close logs on process termination */
atexit(close_logs);
if ( set_handler(SIGABRT, sig_handler) != 0 ||
set_handler(SIGINT, sig_handler) != 0 ||
set_handler(SIGTERM, sig_handler) != 0 )
log_error("Error sigaction()");
}
return new;
}
struct log_file *close_log(struct log_file *logfile) {
struct log_file *lf = NULL;
assert(logfile != NULL);
if ( logfile == log_files ) {
log_files = logfile->next;
} else {
lf = log_files;
while ( lf != NULL && lf->next != logfile ) lf = lf->next;
if ( lf != NULL ) lf->next = logfile->next;
}
lf = logfile->next;
if ( logfile->wrap && logfile->file != stdout && logfile->file != stderr ) {
time_t now = time(NULL);
struct timeb elapsed = get_elapsed();
fprintf(logfile->file,
TIMESTAMP_FORMAT "======== Closing logfile at %s\n\n",
(int) elapsed.time, elapsed.millitm, ctime(&now));
}
if ( logfile->prompt ) fputs("\n", logfile->file);
fflush(logfile->file);
/* prevent stdout/stderr from being fclose()d */
if ( logfile->file != stdout && logfile->file != stderr )
fclose(logfile->file);
free(logfile);
return lf;
}
void close_logs() {
struct log_file *lf;
for ( lf = log_files; lf != NULL; lf = close_log(lf) );
}
int log_message(loglevel_t level, const char *message) {
struct log_file *lf;
int count = 0;
assert(message != NULL);
for ( lf = log_files; lf != NULL; lf = lf->next ) {
if ( level & lf->maxlevel ) {
char *pre, *mark, *post = "";
count++;
/*if ( lf->file == stderr ) {
pre = "\033[31m";
post = "\033[0m";
}*/
if ( lf->prompt && lf->prompted && level != LOG_CONSOLE )
if ( lf->auto_prompt ) pre = "\r";
else pre = "\n";
else
pre = "";
if ( lf->wrap ) {
struct timeb elapsed = get_elapsed();
sprintf(stamp, TIMESTAMP_FORMAT, (int) elapsed.time,
elapsed.millitm);
level = priority_encoder(level & lf->maxlevel);
switch ( level ) {
case LOG_DEBUG:
mark = "((DEBUG)) ";
break;
case LOG_USERINPUT:
mark = "((USERINPUT)) ";
break;
case LOG_ERROR:
mark = "((ERROR)) ";
break;
case LOG_ERROR_VERBOSE:
mark = "((ERROR_VRB)) ";
break;
case LOG_WARNING:
mark = "((WARNING)) ";
break;
case LOG_INFO:
mark = "((INFO)) ";
break;
case LOG_INFO_VERBOSE:
mark = "((INFO_VRB)) ";
break;
case LOG_CONSOLE:
mark = "((CONSOLE)) ";
break;
default:
mark = "((UNDEFINED)) ";
}
} else {
mark = "";
strcpy(stamp, "");
}
/*FIXME togliere post se è inutile */
fprintf(lf->file, "%s%s%s%s%s\n", pre, stamp, mark, message, post);
if ( lf->auto_prompt ) log_prompt(lf);
else {
lf->prompted = FALSE;
fflush(lf->file); /*FIXME non flushare inutilmente */
}
}
}
return count;
}
int log_multiline(loglevel_t level, const char *message) {
char *split, *start, *end;
int count;
assert(message != NULL);
assert(strlen(message) > 0);
split = malloc(strlen(message));
check_alloc(split);
strcpy(split, message);
start = end = split;
while ( (end = strchr(start, '\n')) != NULL ) {
*end = '\0';
log_message(level, start);
start = end + 1;
}
count = log_message(level, start);
free(split);
return count;
}
int flog_message(loglevel_t level, const char *format, ...) {
va_list args;
int count;
char *message = malloc(BUFFER_SIZE);
assert(format != NULL);
check_alloc(message);
va_start(args, format);
vsprintf(message, format, args);
count = log_message(level, message);
free(message);
va_end(args);
return count;
}
int log_error(const char *message) {
return flog_message(LOG_ERROR, "%s: %s", message, strerror(errno));
}
int log_prompt(struct log_file *logfile) {
assert(logfile != NULL);
if ( logfile->prompt ) {
fprintf(logfile->file, "%c ", logfile->prompt);
fflush(logfile->file);
logfile->prompted = TRUE;
return 1;
}
return 0;
}
struct timeb get_elapsed() {
struct timeb now;
ftime(&now);
now.time -= start.time;
if ( now.millitm >= start.millitm ) {
now.millitm -= start.millitm;
} else {
now.time--;
now.millitm = start.millitm - now.millitm;
}
return now;
}
void sig_handler(int signal) {
switch ( signal ) {
case SIGABRT: log_message(LOG_INFO_VERBOSE, "Received SIGABRT"); break;
case SIGINT: log_message(LOG_INFO_VERBOSE, "Received SIGINT"); break;
case SIGTERM: log_message(LOG_INFO_VERBOSE, "Received SIGTERM"); break;
default: flog_message(LOG_INFO_VERBOSE, "Received signal %d", signal);
}
exit(EXIT_FAILURE);
}
loglevel_t priority_encoder(loglevel_t level) {
loglevel_t mask = 1 << (8 * sizeof(loglevel_t) - 1);
if ( level == 0 ) return level;
while ( (level & (~mask)) > 1 ) {
level &= ~mask;
mask >>= 1;
}
return level;
}