-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.c
410 lines (333 loc) · 11.5 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "Log.h"
int main(void) {
LogList *logList = malloc(sizeof(LogList));
FILE *fp;
char fileName[MAX_FILE_LEN];
int option = 0;
int runFlag = TRUE;
initializeLogList(logList);
getFileName(fileName);
if((fp = fopen(fileName, "r")) == NULL) {
fprintf(stderr, "ERROR: FILE DOESN't EXIST\n");
return EXIT_FAILURE;
}
parseFile(fp, logList);
while(runFlag == TRUE) {
promptUsage();
scanf("%d", &option);
while(getchar() != '\n');
while(validateOption(option, 5) == FALSE) {
printf(">> SELECT OPTION[1 - 5]: ");
scanf("%d", &option);
while(getchar() != '\n');
}
executeOption(option, *logList, fileName);
}
freeLogList(logList);
free(logList);
return 0;
}
void getFileName(char *fileName) {
printf("ENTER THE LOG FILE NAME: ");
scanf("%s", fileName);
}
void parseFile(FILE *fp, LogList *logList) {
char line[MAX_LEN];
while(fgets(line, MAX_LEN, fp) != NULL) {
LogInfo *logInfo = malloc(sizeof(LogInfo));
char event[MAX_ARGS_LEN];
sscanf(line, "%*s %*s %s", event);
event[strlen(event)-1] = '\0';
if(strcmp(event, "LOGIN") == 0) {
sscanf(line, "%s %s %s %s %s %1024[^\n]",
(logInfo->logElements)[0], (logInfo->logElements)[1], (logInfo->logElements)[2],
(logInfo->logElements)[3], (logInfo->logElements)[4], (logInfo->logElements)[5]);
(logInfo->logElements)[0][strlen((logInfo->logElements)[0])-1] = '\0';
(logInfo->logElements)[1][strlen((logInfo->logElements)[1])-1] = '\0';
(logInfo->logElements)[2][strlen((logInfo->logElements)[2])-1] = '\0';
(logInfo->logElements)[3][strlen((logInfo->logElements)[3])-1] = '\0';
(logInfo->logElements)[4][strlen((logInfo->logElements)[4])-1] = '\0';
} else if(strcmp(event, "CMD") == 0) {
sscanf(line, "%s %s %s %s %s %1024[^\n]\n",
(logInfo->logElements)[0], (logInfo->logElements)[1], (logInfo->logElements)[2],
(logInfo->logElements)[3], (logInfo->logElements)[4], (logInfo->logElements)[5]);
(logInfo->logElements)[0][strlen((logInfo->logElements)[0])-1] = '\0';
(logInfo->logElements)[1][strlen((logInfo->logElements)[1])-1] = '\0';
(logInfo->logElements)[2][strlen((logInfo->logElements)[2])-1] = '\0';
(logInfo->logElements)[3][strlen((logInfo->logElements)[3])-1] = '\0';
(logInfo->logElements)[4][strlen((logInfo->logElements)[4])-1] = '\0';
} else if(strcmp(event, "MSG") == 0) {
sscanf(line, "%s %s %s %s %s %1024[^\n]",
(logInfo->logElements)[0], (logInfo->logElements)[1], (logInfo->logElements)[2],
(logInfo->logElements)[3], (logInfo->logElements)[4], (logInfo->logElements)[5]);
(logInfo->logElements)[0][strlen((logInfo->logElements)[0])-1] = '\0';
(logInfo->logElements)[1][strlen((logInfo->logElements)[1])-1] = '\0';
(logInfo->logElements)[2][strlen((logInfo->logElements)[2])-1] = '\0';
(logInfo->logElements)[3][strlen((logInfo->logElements)[3])-1] = '\0';
(logInfo->logElements)[4][strlen((logInfo->logElements)[4])-1] = '\0';
} else if(strcmp(event, "LOGOUT") == 0) {
sscanf(line, "%s %s %s %s",
(logInfo->logElements)[0], (logInfo->logElements)[1], (logInfo->logElements)[2], (logInfo->logElements)[3]);
(logInfo->logElements)[0][strlen((logInfo->logElements)[0])-1] = '\0';
(logInfo->logElements)[1][strlen((logInfo->logElements)[1])-1] = '\0';
(logInfo->logElements)[2][strlen((logInfo->logElements)[2])-1] = '\0';
} else if(strcmp(event, "ERR") == 0) {
sscanf(line, "%s %s %s %1024[^\n]",
(logInfo->logElements)[0], (logInfo->logElements)[1], (logInfo->logElements)[2], (logInfo->logElements)[3]);
(logInfo->logElements)[0][strlen((logInfo->logElements)[0])-1] = '\0';
(logInfo->logElements)[1][strlen((logInfo->logElements)[1])-1] = '\0';
(logInfo->logElements)[2][strlen((logInfo->logElements)[2])-1] = '\0';
}
insertLogInfo(logList, logInfo);
}
}
int validateOption(int option, int maxOption) {
if(option >= 1 && option <= maxOption) {
return TRUE;
} else {
printf("\nERROR: INVALID OPTION! PLEASE RETRY\n");
return FALSE;
}
}
void executeOption(int option, LogList logList, char *fileName) {
switch(option) {
case 1:
printEntireLogInfo(logList);
break;
case 2:
sortLogInfo(logList, fileName);
break;
case 3:
filterLogInfo(logList, fileName);
break;
case 4:
searchKeywords(logList);
break;
case 5:
exit(EXIT_SUCCESS);
break;
default:
break;
}
puts("");
}
void printEntireLogInfo(LogList logList) {
LogInfo *cur = logList.head;
LogInfo *next = NULL;
int i;
printf("%d\n", logList.count);
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%-30s\n\n",
"A", "B", "C", "D", "E", "F");
for(i=0; i<logList.count; i++) {
next = cur->next;
if(strcmp((cur->logElements)[2], "LOGIN") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%s\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2],
(cur->logElements)[3], (cur->logElements)[4], (cur->logElements)[5]);
} else if(strcmp((cur->logElements)[2], "CMD") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%s\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2],
(cur->logElements)[3], (cur->logElements)[4], (cur->logElements)[5]);
} else if(strcmp((cur->logElements)[2], "MSG") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%s\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2],
(cur->logElements)[3], (cur->logElements)[4], (cur->logElements)[5]);
} else if(strcmp((cur->logElements)[2], "LOGOUT") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[39m\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2], (cur->logElements)[3]);
} else if(strcmp((cur->logElements)[2], "ERR") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[39m\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2], (cur->logElements)[3]);
}
cur = next;
}
}
void sortLogInfo(LogList logList, char *fileName) {
int runFlag = TRUE;
int option = 0;
int order = 0;
char *cmd[16] = {"/usr/bin/sort"};
char buf[MAX_LEN];
pid_t pid;
while(runFlag == TRUE) {
printf("\n>> SELECT COLUMN [1 - 6]: ");
scanf("%d", &option);
while(getchar() != '\n');
while(validateOption(option, 6) == FALSE) {
printf(">> SELECT COLUMN[1 - 6]: ");
scanf("%d", &option);
while(getchar() != '\n');
}
promptSortOption();
scanf("%d", &order);
while(getchar() != '\n');
while(validateOption(order, 2) == FALSE) {
printf(">> SELECT ORDER[1 - 2]: ");
scanf("%d", &order);
while(getchar() != '\n');
}
break;
}
sprintf(buf, "-k%d", option);
cmd[1] = buf;
if(order == 2) {
cmd[2] = "-r";
cmd[3] = fileName;
cmd[4] = (void *)NULL;
} else {
cmd[2] = fileName;
cmd[3] = (void *)NULL;
}
if((pid = fork()) == 0) {
if(execv(cmd[0], cmd) < 0) {
fprintf(stderr, "%s: command not found\n", cmd[0]);
exit(EXIT_FAILURE);
}
}
waitpid(pid, 0, 0);
}
void filterLogInfo(LogList logList, char *fileName) {
int runFlag = TRUE;
int option = 0;
char *cmd[16] = {"grep", "--color=always", "-a"};
char buf[MAX_LEN];
pid_t pid;
while(runFlag == TRUE) {
promptFilterOption();
scanf("%d", &option);
while(getchar() != '\n');
while(validateOption(option, 6) == FALSE) {
printf("\nERROR: INVALID OPTION! PLEASE RETRY\n");
printf(">> SELECT COLUMN[1 - 6]: ");
scanf("%d", &option);
while(getchar() != '\n');
}
if(option == 1) {
promptKeyword();
scanf("%[^\n]%*c", buf);
} else {
filterLogByTime(logList);
return;
}
break;
}
cmd[3] = buf;
cmd[4] = fileName;
cmd[5] = (void *)NULL;
if((pid = fork()) == 0) {
if(execvp(cmd[0], cmd) < 0) {
fprintf(stderr, "%s: command not found\n", cmd[0]);
exit(EXIT_FAILURE);
}
}
waitpid(pid, 0, 0);
}
void filterLogByTime(LogList logList) {
int i;
int start;
int end;
char timeStart[MAX_LEN];
char timeEnd[MAX_LEN];
LogInfo *cur = logList.head;
LogInfo *next = NULL;
printf(">>ENTER START TIMESTAMP (MM/DD/YY-hour:minute[AM/PM]): ");
scanf("%[^\n]%*c", timeStart);
printf(">>ENTER END TIMESTAMP (MM/DD/YY-hour:minute[AM/PM]): ");
scanf("%[^\n]%*c", timeEnd);
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%-30s\n\n",
"A", "B", "C", "D", "E", "F");
for(i=0; i<logList.count; i++) {
next = cur->next;
start = strncmp(timeStart, cur->logElements[0], strlen(timeStart));
end = strncmp(timeEnd, cur->logElements[0], strlen(timeEnd));
if(start <= 0 && end >= 0) {
if(strcmp((cur->logElements)[2], "LOGIN") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%s\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2],
(cur->logElements)[3], (cur->logElements)[4], (cur->logElements)[5]);
} else if(strcmp((cur->logElements)[2], "CMD") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%s\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2],
(cur->logElements)[3], (cur->logElements)[4], (cur->logElements)[5]);
} else if(strcmp((cur->logElements)[2], "MSG") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[36m%10s \e[39m%s\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2],
(cur->logElements)[3], (cur->logElements)[4], (cur->logElements)[5]);
} else if(strcmp((cur->logElements)[2], "LOGOUT") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[39m\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2], (cur->logElements)[3]);
} else if(strcmp((cur->logElements)[2], "ERR") == 0) {
printf("\e[32m%20s \e[33m%10s \e[34m%10s \e[35m%20s \e[39m\n",
(cur->logElements)[0], (cur->logElements)[1], (cur->logElements)[2], (cur->logElements)[3]);
}
}
cur = next;
}
}
void searchKeywords(LogList logList) {
}
void initializeLogList(LogList *logList) {
logList->head = NULL;
logList->tail = NULL;
logList->count = 0;
}
void insertLogInfo(LogList *logList, LogInfo *logInfo) {
if(logList->count == 0) {
logInfo->prev = NULL;
logInfo->next = NULL;
logList->head = logInfo;
logList->tail = logInfo;
} else {
logInfo->prev = logList->tail;
logInfo->next = NULL;
logList->tail->next = logInfo;
logList->tail = logInfo;
}
(logList->count)++;
}
void resetLogList(LogList *logList) {
freeLogList(logList);
initializeLogList(logList);
}
void freeLogList(LogList *logList) {
LogInfo *cur = logList->head;
LogInfo *next = NULL;
int i;
for(i=0; i<logList->count; i++) {
next = cur->next;
free(cur);
cur = next;
}
}
void promptUsage() {
printf("OPTION [1 - 5]\n");
printf("1: Print entire log info\n");
printf("2: Sort the logs by column\n");
printf("3: Filter logs based on field\n");
printf("4: Search keywords\n");
printf("5: Exit program\n");
printf("\n>> SELECT OPTION [1 - 5]: ");
}
void promptSortOption() {
printf("OPTION [1 - 2]\n");
printf("1: Sort log in ascending order\n");
printf("2: Sort log in decending order\n");
printf("\n>> SELECT ORDER [1 - 2]: ");
}
void promptFilterOption() {
printf("OPTION [1 - 2]\n");
printf("1: Filter by keyword\n");
printf("2: Filter by time\n");
printf("\n>> SELECT ORDER [1 - 2]: ");
}
void promptKeyword() {
printf("Date-Timestamp: mm/dd/yy-hh:mm[am/pm]\n");
printf("Username\n");
printf("Event: LOGIN, CMD, MSG, LOGOUT, ERR\n");
printf("LOGIN: IP:port, success/fail, ERR # message or MOTD\n");
printf("CMD: thecommand, success/fail, chat/client\n");
printf("MSG: to/from, user, message\n");
printf("LOGOUT: intentional/error\n");
printf("ERR: ERR # message\n");
printf("\n>> SELECT KEYWORD: ");
}