-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
323 lines (281 loc) · 6.16 KB
/
util.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
316
317
318
319
320
321
322
323
#include "inc.h"
#include "proto.h"
#include "logger.h"
#include <time.h>
#include <sys/types.h>
#include "../pm/mproc.h"
#define NLOGGERS 100
#define LOGGER_ALREADY_ACTIVE 1
#define LOGGER_DOES_NOT_EXIST 2
#define INTERNAL_ERROR 3
#define LOGGER_IS_OPEN 4
#define NO_RIGHTS_TO_LOGGER 5
#define LOGGER_NOT_ACTIVE 6
#define LOGGER_IS_ACTIVE 7
struct logger *loggers[NLOGGERS];
int LOGGERS_INITIALIZED = 0;
int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *info)
{
return(OK);
}
int init_loggers() {
if(LOGGERS_INITIALIZED)
return 1;
int ret = read_config(loggers);
if (ret) {
printf("Loggers initialized.\n");
} else {
printf("Failed to parse configuration file.\n");
return 0;
}
return LOGGERS_INITIALIZED = 1;
}
struct logger * get_logger(char* name) {
for (int i = 0; loggers[i] != NULL; i++) {
if (!strcmp(loggers[i]->name, name)) {
return loggers[i];
}
}
return NULL;
}
int start_logger(struct logger* l, int owner) {
if (l->type == FILE) {
int fd;
if (l->append_flag)
fd = open(l->file, O_WRONLY|O_APPEND|O_CREAT);
else
fd = open(l->file, O_WRONLY|O_CREAT);
if (fd == 0)
return INTERNAL_ERROR;
l->file_dest = fd;
}
l->active = 1;
l->owner = owner;
return(OK);
}
int do_hello(message *m_ptr) {
for (int i = 0; loggers[i] != NULL; i++) {
print_logger(loggers[i]);
}
return (OK);
}
int do_start_log(message *m) {
struct logger * l = get_logger(m->m_ls.name);
if(l == NULL)
return LOGGER_DOES_NOT_EXIST;
if(l->active)
return LOGGER_ALREADY_ACTIVE;
return start_logger(l, m->m_source);
}
int do_set_logger_level(message *m) {
struct logger * l = get_logger(m->m_ls.name);
if (l == NULL)
return LOGGER_DOES_NOT_EXIST;
if (l->active)
return LOGGER_IS_ACTIVE;
l->level = m->m_ls.level;
return (OK);
}
char *get_source_name(endpoint_t source) {
static struct mproc mproc[NR_PROCS];
if (getsysinfo(PM_PROC_NR, SI_PROC_TAB, mproc, sizeof(mproc)) != OK) {
return NULL;
}
int i;
struct mproc *mp;
static char mp_name[PROC_NAME_LEN];
for(i=0; i<NR_PROCS; i++) {
mp = &mproc[i];
if(mp->mp_endpoint == source) {
strcpy(mp_name, mp->mp_name);
return mp_name;
}
}
return NULL;
}
char *get_slevel(int level) {
char* lev;
switch (level) {
case 0:
lev = "TRACE";
return lev;
case 1:
lev = "DEBUG";
return lev;
case 2:
lev = "ERROR";
return lev;
}
return NULL;
}
int do_write_log(message *m) {
struct logger * l = get_logger(m->m_ls.name);
if (l == NULL)
return LOGGER_DOES_NOT_EXIST;
if (!l->active)
return LOGGER_NOT_ACTIVE;
if (l->owner != m->m_source)
return NO_RIGHTS_TO_LOGGER;
if(m->m_ls.level < l->level)
return (OK);
char time[9];
message mm;
memset(&mm, 0, sizeof(mm));
int ret = _syscall(PM_PROC_NR, PM_GETTIMEOFDAY, &mm);
if(ret != 0) {
return INTERNAL_ERROR;
}
int sec = mm.m_pm_lc_time.sec % (60*60*24);
snprintf(time, 9, "%02ld:%02ld:%02ld", ((long)(sec/(60*60)))%24,
(long)(sec%(60*60))/60, (long)(sec%(60*60))%60);
char* source = get_source_name(m->m_source);
if(source == NULL)
return INTERNAL_ERROR;
char * level = get_slevel(m->m_ls.level);
if(level == NULL)
return INTERNAL_ERROR;
char m_text[1024];
ret = sys_datacopy(m->m_source, m->m_ls.msg_loc, SELF,
(vir_bytes) m_text, m->m_ls.msg_len);
if(ret != 0) {
return INTERNAL_ERROR;
}
if(l->type == STDOUT)
printf("[STDOUT] ");
else if(l->type == STDERR)
printf("[STDERR] ");
for(int i = 0; l->format[i] != '\0'; i ++) {
char * sout;
if(l->format[i] == '%') {
switch(l->format[i+1]) {
case 't':
sout = time;
break;
case 'l':
sout = level;
break;
case 'n':
sout = source;
break;
case 'm':
sout = m_text;
break;
}
switch (l->type) {
case FILE:
write(l->file_dest, sout, strlen(sout));
break;
case STDOUT:
printf("%s", sout);
break;
case STDERR:
printf("%s", sout);
}
i++;
continue;
}
char car[2];
car[0] = l->format[i];
char ca = l->format[i];
switch (l->type) {
case FILE:
write(l->file_dest, car, 1);
break;
case STDOUT:
printf("%c", ca);
break;
case STDERR:
printf("%c", ca);
}
}
if (l->type == FILE) {
char car[2];
car[0] = '\n';
car[1] = '\0';
write(l->file_dest, car, 1);
} else {
printf("\n");
}
return (OK);
}
int close_logger(struct logger* l) {
if (l->type == FILE) {
int ret = close(l->file_dest);
if(ret)
return INTERNAL_ERROR;
l->file_dest = 0;
}
l->active = 0;
l->owner = 0;
return(OK);
}
int do_close_log(message *m) {
struct logger * l = get_logger(m->m_ls.name);
if(l == NULL)
return LOGGER_DOES_NOT_EXIST;
if(!l->active)
return LOGGER_NOT_ACTIVE;
if(l->owner != m->m_source)
return NO_RIGHTS_TO_LOGGER;
return close_logger(l);
}
int truncate_file(const char* filename) {
int fd = open(filename, O_WRONLY | O_TRUNC);
if (fd < 0) {
return INTERNAL_ERROR;
}
close(fd);
return OK;
}
int do_clear_logs(message *m) {
int global_ret_val = OK;
int ret;
if (!m->m_ls.name) {
for (int i = 0; i < LOGGERS_INITIALIZED; i++) {
if (loggers[i]->type != FILE) {
continue;
}
if (loggers[i]->active) {
global_ret_val = LOGGER_IS_OPEN;
}
if ((ret = truncate_file(loggers[i]->file)) != OK) {
global_ret_val = ret;
}
}
} else {
char buffer[64];
memcpy(buffer, m->m_ls.name, 32);
buffer[32] = '\0';
char* last_logger = buffer;
for (char* p = buffer; ; p++) {
if (*p == ',' || *p == '\0') {
int last = FALSE;
if (*p == '\0') {
last = TRUE;
}
*p = '\0';
struct logger* l = get_logger(last_logger);
last_logger = p + 1;
if (!l) {
global_ret_val = LOGGER_DOES_NOT_EXIST;
}
if (l->type != FILE) {
if (last) {
break;
}
continue;
}
if (l->active) {
global_ret_val = LOGGER_IS_OPEN;
}
if ((ret = truncate_file(l->file)) != OK) {
global_ret_val = ret;
}
if (last) {
break;
}
}
}
}
return global_ret_val;
}