Commit | Line | Data |
---|---|---|
970ed795 | 1 | /////////////////////////////////////////////////////////////////////////////// |
3abe9331 | 2 | // Copyright (c) 2000-2015 Ericsson Telecom AB |
970ed795 EL |
3 | // All rights reserved. This program and the accompanying materials |
4 | // are made available under the terms of the Eclipse Public License v1.0 | |
5 | // which accompanies this distribution, and is available at | |
6 | // http://www.eclipse.org/legal/epl-v10.html | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | /************************** | |
9 | Log-file merger | |
10 | written by Gabor Tatarka | |
11 | **************************/ | |
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
15 | #include <string.h> | |
16 | #include <errno.h> | |
17 | #include <time.h> | |
18 | #include <signal.h> | |
19 | #include "../common/memory.h" | |
20 | #include "../common/version_internal.h" | |
21 | ||
22 | #ifdef LICENSE | |
23 | #include "../common/license.h" | |
24 | #endif | |
25 | ||
26 | #ifdef MINGW | |
27 | /* On MinGW seeking is not working in files opened in text mode due to a | |
28 | * "feature" in the underlying MSVCRT. So we open all files in binary mode. */ | |
29 | # define FOPEN_READ "rb" | |
30 | # define FOPEN_WRITE "wb" | |
31 | #else | |
32 | # define FOPEN_READ "r" | |
33 | # define FOPEN_WRITE "w" | |
34 | #endif | |
35 | ||
36 | #define Fail -1 | |
37 | #define False 0 | |
38 | #define True 1 | |
39 | ||
40 | /* These lengths below represent the number of characters in the log file. | |
41 | * No NUL terminator is included in the length. */ | |
42 | #define TIMELENGTH 15 | |
43 | #define SECONDLENGTH 9 | |
44 | #define DATETIMELENGTH 27 | |
45 | #define MAXTIMESTAMPLENGTH 27 | |
46 | ||
47 | #define BUFFERSIZE 1024 | |
48 | #define YYYYMONDD 1 /* format of Date: year/month/day*/ | |
49 | ||
50 | static const char * const MON[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
51 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | |
52 | ||
53 | static const char *progname; | |
54 | static FILE *outfile = NULL; | |
55 | ||
56 | enum TimeStampFormats { TSF_Undefined = -1, TSF_Seconds, TSF_Time, | |
57 | TSF_DateTime }; | |
58 | static int TimeStampLength; | |
59 | static enum TimeStampFormats TimeStampUsed = TSF_Undefined; | |
60 | ||
61 | static int IsSecond(const char *str)/*Is timestamp format Seconds*/ | |
62 | { | |
63 | int i; | |
64 | if (*str >= '0' && *str <= '9') str++; /* first digit */ | |
65 | else return False; | |
66 | while (*str >= '0' && *str <= '9') str++; /* other digits */ | |
67 | if (*str == '.') str++; | |
68 | else return False; /* '.' */ | |
69 | for (i = 0; i < 6; i++, str++) | |
70 | if (*str < '0' || *str > '9') return False; /* microseconds(6 digits) */ | |
71 | return True; | |
72 | } | |
73 | ||
74 | static int IsSecond2_6(const char *str)/*does string contain sec(2).usec(6)*/ | |
75 | { | |
76 | int a; | |
77 | for(a=0;a<SECONDLENGTH;a++) { | |
78 | if(a==2) { | |
79 | if(*str=='.') { | |
80 | str++;continue; | |
81 | } else return False; | |
82 | } | |
83 | if(*str<'0'||*str>'9')return False; | |
84 | str++; | |
85 | } | |
86 | return True; | |
87 | } | |
88 | ||
89 | static int IsTime(const char *str)/*Is timestamp format Time*/ | |
90 | { | |
91 | int a; | |
92 | if(False==IsSecond2_6(str+6))return False; | |
93 | for(a=0;a<6;a++){ | |
94 | if(a==2||a==5) { | |
95 | if(*str==':') { | |
96 | str++;continue; | |
97 | } else return False; | |
98 | } | |
99 | if(*str<'0'||*str>'9')return False; | |
100 | str++; | |
101 | } | |
102 | return True; | |
103 | } | |
104 | ||
105 | #ifdef YYYYMONDD /*Date format: year/month/day*/ | |
106 | # define FIRST_LEN 4 | |
107 | # define THIRD_LEN 2 | |
108 | #else /*Date format: day/month/year*/ | |
109 | # define FIRST_LEN 2 | |
110 | # define THIRD_LEN 4 | |
111 | #endif | |
112 | ||
113 | static int IsDateTime(const char *str)/*is timestamp format Date/Time*/ | |
114 | { | |
115 | int a,b; | |
116 | if(False==IsTime(str+12))return False; | |
117 | for(a=0;a<FIRST_LEN;a++) {/*YYYY or DD*/ | |
118 | if(*str<'0'||*str>'9')return False; | |
119 | str++; | |
120 | } | |
121 | if(*str!='/')return False;/* '/' */ | |
122 | str++; | |
123 | for(a=0,b=0;a<12;a++)if(0==strncmp(str,MON[a],3)){b=1;break;}/*MON*/ | |
124 | if(!b)return False; | |
125 | str+=3; | |
126 | if(*str!='/')return False;/* '/' */ | |
127 | str++; | |
128 | for(a=0;a<THIRD_LEN;a++) {/*DD or YYYY*/ | |
129 | if(*str<'0'||*str>'9')return False; | |
130 | str++; | |
131 | } | |
132 | return True; | |
133 | } | |
134 | ||
135 | static int FormatMatch(const char *str,int format)/*does format of timestamp match format*/ | |
136 | { | |
137 | switch(format) | |
138 | { | |
139 | case TSF_Undefined:return False; | |
140 | case TSF_Seconds:if(False==IsSecond(str))return False;else return True; | |
141 | case TSF_Time:if(False==IsTime(str))return False;else return True; | |
142 | case TSF_DateTime:if(False==IsDateTime(str))return False; | |
143 | else return True; | |
144 | default:return False; | |
145 | } | |
146 | } | |
147 | ||
148 | /* | |
149 | formats: | |
150 | DateTime: yyyy/Mon/dd hh:mm:ss.us | |
151 | Time: hh:mm:ss.us | |
152 | Second: s.us | |
153 | */ | |
154 | static enum TimeStampFormats GetTimeStampFormat(const char *filename) | |
155 | { | |
156 | /*get timestamp format used in file*/ | |
157 | enum TimeStampFormats ret_val = TSF_Undefined; | |
158 | char str[MAXTIMESTAMPLENGTH + 1]; | |
159 | FILE *fp = fopen(filename, FOPEN_READ); | |
160 | if (fp == NULL) { | |
161 | fprintf(stderr, "%s: warning: cannot open %s: %s\n", progname, | |
162 | filename, strerror(errno)); | |
163 | return TSF_Undefined; | |
164 | } | |
165 | if (fgets(str, sizeof(str), fp) != NULL) { | |
166 | if (IsSecond(str)) ret_val = TSF_Seconds; | |
167 | else if (IsTime(str)) ret_val = TSF_Time; | |
168 | else if (IsDateTime(str)) ret_val = TSF_DateTime; | |
169 | } | |
170 | fclose(fp); | |
171 | return ret_val; | |
172 | } | |
173 | ||
174 | static char *GetComponentIdentifier(const char *path_name) | |
175 | { | |
176 | char *ret_val; | |
177 | size_t name_len = strlen(path_name); | |
178 | size_t filename_begin = 0, i; | |
179 | size_t compid_begin, compid_end; | |
180 | int dash_found = 0; | |
181 | /* find the first character of the file name */ | |
182 | for (i = 0; i < name_len; i++) | |
183 | if (path_name[i] == '/') filename_begin = i + 1; | |
184 | /* fallback values if neither '-' nor '.' is found */ | |
185 | compid_begin = filename_begin; | |
186 | compid_end = name_len; | |
187 | /* find the last '-' character in the file name */ | |
188 | for (i = name_len; i > filename_begin; i--) | |
189 | if (path_name[i - 1] == '-') { | |
190 | dash_found = 1; | |
191 | compid_begin = i; | |
192 | break; | |
193 | } | |
194 | if (dash_found) { | |
195 | /* find the first '.' character after the '-' */ | |
196 | for (i = compid_begin; i < name_len; i++) | |
197 | if (path_name[i] == '.') { | |
198 | compid_end = i; | |
199 | break; | |
200 | } | |
201 | } else { | |
202 | /* find the last '.' in the file name */ | |
203 | for (i = name_len; i > filename_begin; i--) | |
204 | if (path_name[i - 1] == '.') { | |
205 | compid_end = i - 1; | |
206 | break; | |
207 | } | |
208 | /* find the last but one '.' in the file name */ | |
209 | for (i = compid_end; i > filename_begin; i--) | |
210 | if (path_name[i - 1] == '.') { | |
211 | compid_begin = i; | |
212 | break; | |
213 | } | |
214 | } | |
215 | if (compid_end > compid_begin) { | |
216 | size_t compid_len = compid_end - compid_begin; | |
217 | ret_val = (char*)Malloc(compid_len + 1); | |
218 | memcpy(ret_val, path_name + compid_begin, compid_len); | |
219 | ret_val[compid_len] = '\0'; | |
220 | } else ret_val = NULL; | |
221 | return ret_val; | |
222 | } | |
223 | ||
224 | static FILE *OpenTempFile(char **filename) | |
225 | { | |
226 | FILE *fp; | |
227 | #ifdef MINGW | |
228 | /* Function mkstemp() is not supported on MinGW */ | |
229 | char *temp_name = tempnam(NULL, NULL); | |
230 | if (temp_name == NULL) { | |
231 | fprintf(stderr, "%s: creation of a temporary file failed: %s\n", progname, | |
232 | strerror(errno)); | |
233 | exit(EXIT_FAILURE); | |
234 | } | |
235 | fp = fopen(temp_name, FOPEN_WRITE); | |
236 | if (fp == NULL) { | |
237 | fprintf(stderr, "%s: opening of temporary file `%s' failed: %s\n", | |
238 | progname, temp_name, strerror(errno)); | |
239 | free(temp_name); | |
240 | exit(EXIT_FAILURE); | |
241 | } | |
242 | *filename = mcopystr(temp_name); | |
243 | free(temp_name); | |
244 | #else | |
245 | int fd; | |
246 | *filename = mcopystr("/tmp/logmerge_XXXXXX"); | |
247 | fd = mkstemp(*filename); | |
248 | if (fd < 0) { | |
249 | fprintf(stderr, "%s: creation of a temporary file based on template `%s' " | |
250 | "failed: %s\n", progname, *filename, strerror(errno)); | |
251 | Free(*filename); | |
252 | exit(EXIT_FAILURE); | |
253 | } | |
254 | fp = fdopen(fd, FOPEN_WRITE); | |
255 | if (fp == NULL) { | |
256 | fprintf(stderr, "%s: system call fdopen() failed on temporary file `%s' " | |
257 | "(file descriptor %d): %s\n", progname, *filename, fd, strerror(errno)); | |
258 | Free(*filename); | |
259 | exit(EXIT_FAILURE); | |
260 | } | |
261 | #endif | |
262 | return fp; | |
263 | } | |
264 | ||
265 | static FILE **fp_list_in = NULL, *fpout; | |
266 | static char **name_list_in = NULL; | |
267 | static int fpout_is_closeable = 0,must_use_temp = 0; | |
268 | static char **temp_file_list = NULL; | |
269 | static int num_tempfiles = 0, num_infiles = 0, num_allfiles = 0,start_file = 0; | |
270 | static int infiles_processed = False; | |
271 | typedef struct | |
272 | { | |
273 | char timestamp[MAXTIMESTAMPLENGTH+1];/*text of timestamp*/ | |
274 | time_t sec; /*seconds in timestamp (since 1970)*/ | |
275 | unsigned long usec; /*microseconds in timestamp (0L..1000000L)*/ | |
276 | int wrap;/*if current timestamp is smaller than prev. timestamp -> wrap++;*/ | |
277 | expstring_t data;/*text of logged event*/ | |
278 | char *str_to_add;/*part of original filename*/ | |
279 | int ignore; /* if true -> EOF */ | |
280 | long start_line,line_ctr;/*line of event start (timestamp), line counter*/ | |
281 | }LogEvent; | |
282 | ||
283 | static LogEvent **EventList; | |
284 | ||
285 | static int OpenMaxFiles(int argc,char *argv[]) | |
286 | { | |
287 | int a=0; | |
288 | while(argc) { | |
289 | fp_list_in=(FILE **)Realloc(fp_list_in,(a+1)*sizeof(FILE *)); | |
290 | errno = 0; | |
291 | fp_list_in[a]=fopen(argv[a], FOPEN_READ); | |
292 | if(fp_list_in[a]==NULL) { | |
293 | switch(errno) { | |
294 | case 0: | |
295 | /* Solaris may not set errno if libc cannot create a stdio | |
296 | stream because the underlying fd is greater than 255 */ | |
297 | case ENFILE: | |
298 | case EMFILE: | |
299 | /*more infiles than can be opened->close one and create a tempfile for output*/ | |
300 | if(argc>0) { | |
301 | Free(EventList[--a]->str_to_add); | |
302 | Free(EventList[ a]); | |
303 | fclose(fp_list_in[a]); | |
304 | temp_file_list = (char**)Realloc(temp_file_list, | |
305 | (num_tempfiles + 1) * sizeof(*temp_file_list)); | |
306 | fpout = OpenTempFile(temp_file_list + num_tempfiles); | |
307 | num_tempfiles++; | |
308 | fpout_is_closeable=1; | |
309 | } | |
310 | num_infiles=a; | |
311 | return a; | |
312 | default: | |
313 | fprintf(stderr,"%s: error opening input file %s: %s\n", | |
314 | progname, argv[a], strerror(errno)); | |
315 | exit(EXIT_FAILURE); | |
316 | } | |
317 | } else { | |
318 | EventList=(LogEvent **)Realloc(EventList, | |
319 | (a+1)*sizeof(LogEvent *)); | |
320 | EventList[a]=(LogEvent *)Malloc(sizeof(LogEvent)); | |
321 | if (infiles_processed) EventList[a]->str_to_add = NULL; | |
322 | else { | |
323 | /* cutting the component identifier portion out from the | |
324 | * file name */ | |
325 | EventList[a]->str_to_add = | |
326 | GetComponentIdentifier(name_list_in[a + start_file]); | |
327 | } | |
328 | EventList[a]->ignore=True; | |
329 | EventList[a]->data=NULL; | |
330 | EventList[a]->wrap=0; | |
331 | EventList[a]->sec=0; | |
332 | EventList[a]->usec=0L; | |
333 | EventList[a]->line_ctr=1L; | |
334 | EventList[a]->start_line=1L; | |
335 | ++a; | |
336 | } | |
337 | argc--; | |
338 | } | |
339 | if (must_use_temp) { | |
340 | temp_file_list = (char**)Realloc(temp_file_list, | |
341 | (num_tempfiles + 1) * sizeof(*temp_file_list)); | |
342 | fpout = OpenTempFile(temp_file_list + num_tempfiles); | |
343 | num_tempfiles++; | |
344 | fpout_is_closeable = 1; | |
345 | } else { | |
346 | fpout = outfile; | |
347 | if (outfile!=stdout) fpout_is_closeable = 1; | |
348 | else fpout_is_closeable = 0; | |
349 | } | |
350 | num_infiles=a; | |
351 | return a;/*nr. of opened files*/ | |
352 | } | |
353 | ||
354 | static void CloseAllFiles(void) | |
355 | { | |
356 | int i; | |
357 | if (fpout_is_closeable) fclose(fpout); | |
358 | for (i = 0; i < num_infiles; i++) { | |
359 | Free(EventList[i]->data); | |
360 | Free(EventList[i]->str_to_add); | |
361 | Free(EventList[i]); | |
362 | } | |
363 | Free(EventList); | |
364 | EventList = NULL; | |
365 | num_infiles = 0; | |
366 | } | |
367 | ||
368 | static int EventCmp(LogEvent *e1,LogEvent *e2) | |
369 | /*Returns: if(event1<event2)-1; | |
370 | if(event1==event2)0; | |
371 | if(event1>event2)1;*/ | |
372 | { | |
373 | time_t tmpsec1,tmpsec2; | |
374 | tmpsec1=e1->sec; | |
375 | tmpsec2=e2->sec; | |
376 | if(tmpsec1<tmpsec2)return -1; | |
377 | if(tmpsec1>tmpsec2)return 1; | |
378 | if(e1->usec<e2->usec)return -1; | |
379 | if(e1->usec>e2->usec)return 1; | |
380 | return 0; | |
381 | } | |
382 | ||
383 | #ifdef YYYYMONDD | |
384 | #define YearOffset 0 | |
385 | #define MonOffset 5 | |
386 | #define DayOffset 9 | |
387 | #else | |
388 | #define DayOffset 0 | |
389 | #define MonOffset 3 | |
390 | #define YearOffset 7 | |
391 | #endif | |
392 | ||
393 | static void TS2long(time_t *sec, unsigned long *usec, const char *TSstr) | |
394 | /*converts timestamp string to two long values*/ | |
395 | { | |
396 | struct tm TM; | |
397 | int a; | |
398 | char *ptr,str[MAXTIMESTAMPLENGTH+1]; | |
399 | strncpy(str,TSstr,MAXTIMESTAMPLENGTH); | |
400 | str[MAXTIMESTAMPLENGTH] = '\0'; | |
401 | /*->this way only a copy of the timestamp string will be modified*/ | |
402 | switch(TimeStampUsed) { | |
403 | case TSF_Seconds: | |
404 | ptr=strpbrk(str,"."); | |
405 | *ptr='\0';ptr++;*(ptr+6)='\0'; | |
406 | *sec=(time_t)atol(str); | |
407 | *usec=atol(ptr); | |
408 | return; | |
409 | case TSF_Time: | |
410 | TM.tm_year = 70; | |
411 | TM.tm_mon = 0; | |
412 | TM.tm_mday = 1; | |
413 | TM.tm_isdst = -1; | |
414 | *(str+2)='\0'; | |
415 | *(str+5)='\0'; | |
416 | *(str+8)='\0'; | |
417 | *(str+15)='\0'; | |
418 | TM.tm_hour = atoi(str); | |
419 | TM.tm_min = atoi(str+3); | |
420 | TM.tm_sec = atoi(str+6); | |
421 | *usec = atol(str+9); | |
422 | break; | |
423 | case TSF_DateTime: | |
424 | *(str+YearOffset+4)='\0';*(str+MonOffset+3)='\0'; | |
425 | *(str+DayOffset+2)='\0'; | |
426 | TM.tm_year=atoi(str+YearOffset)-1900; | |
427 | for(a=0;a<12;a++)if(!strcmp(MON[a],str+MonOffset)) { | |
428 | TM.tm_mon=a;break; | |
429 | } | |
430 | TM.tm_mday=atoi(str+DayOffset);TM.tm_isdst=-1; | |
431 | ptr=str+12; | |
432 | *(ptr+2)='\0';*(ptr+5)='\0';*(ptr+8)='\0'; | |
433 | *(ptr+15)='\0'; | |
434 | TM.tm_hour=atoi(ptr);TM.tm_min=atoi(ptr+3); | |
435 | TM.tm_sec=atoi(ptr+6);*usec=atol(ptr+9); | |
436 | break; | |
437 | default: | |
438 | *sec = 0; | |
439 | *usec = 0; | |
440 | return; | |
441 | } | |
442 | *sec = mktime(&TM); | |
443 | } | |
444 | ||
445 | static int GetEvent(FILE *fp, LogEvent *event) | |
446 | { | |
447 | time_t prev_sec; | |
448 | unsigned long prev_usec; | |
449 | for ( ; ; ) { | |
450 | /* find and read timestamp */ | |
451 | if (fgets(event->timestamp, TimeStampLength + 1, fp) == NULL) { | |
452 | event->ignore = True; | |
453 | return False; | |
454 | } | |
455 | event->start_line=event->line_ctr; | |
456 | if (FormatMatch(event->timestamp, TimeStampUsed)) break; | |
457 | } | |
458 | prev_sec = event->sec; | |
459 | prev_usec = event->usec; | |
460 | TS2long(&event->sec, &event->usec, event->timestamp); | |
461 | if (event->sec < prev_sec || | |
462 | (event->sec == prev_sec && event->usec < prev_usec)) { | |
463 | event->wrap = 1; | |
464 | } | |
465 | event->ignore = False; | |
466 | for ( ; ; ) { | |
467 | size_t a; | |
468 | char buf[BUFFERSIZE]; | |
469 | /* read the log-event */ | |
470 | if (fgets(buf, sizeof(buf), fp) == NULL) { | |
471 | /* EOF was detected */ | |
472 | if (event->data == NULL) event->data = mcopystr("\n"); | |
473 | else if (event->data[mstrlen(event->data) - 1] != '\n') | |
474 | event->data = mputc(event->data, '\n'); | |
475 | return False; | |
476 | } | |
477 | a = strlen(buf); | |
478 | if(FormatMatch(buf,TimeStampUsed)) {/*Did we read the next event's timestamp?*/ | |
479 | fseek(fp, -1L * a, SEEK_CUR);/*"unread" next event*/ | |
480 | break; | |
481 | } else if (buf[a - 1] == '\n') event->line_ctr++; | |
482 | event->data=mputstr(event->data, buf);/*append buffer to event-data*/ | |
483 | } | |
484 | return True; | |
485 | } | |
486 | ||
487 | static void WriteError(void) | |
488 | { | |
489 | fprintf(stderr, "%s: error: writing to %s file failed: %s\n", | |
490 | progname, fpout == outfile ? "output" : "temporary", strerror(errno)); | |
491 | exit(EXIT_FAILURE); | |
492 | } | |
493 | ||
494 | static void FlushEvent(LogEvent *event) | |
495 | { | |
496 | if (fputs(event->timestamp, fpout) == EOF) WriteError(); | |
497 | if (!infiles_processed && event->str_to_add != NULL) { | |
498 | if (putc(' ', fpout) == EOF) WriteError(); | |
499 | if (fputs(event->str_to_add, fpout) == EOF) WriteError(); | |
500 | } | |
501 | if (fputs(event->data, fpout) == EOF) WriteError(); | |
502 | Free(event->data); | |
503 | event->data = NULL; | |
504 | event->ignore = True; | |
505 | } | |
506 | ||
507 | static void ProcessOpenFiles(void) | |
508 | /*merge all opened files to fpout (that is stdout or outfile or a tempfile), | |
509 | and clean up*/ | |
510 | { | |
511 | int i; | |
512 | for (i = 0; i < num_infiles; i++) { | |
513 | /* read first logged event from all opened files */ | |
514 | if (!GetEvent(fp_list_in[i], EventList[i])) { | |
515 | /* EOF or read error (e.g. file contained only one log event) */ | |
516 | fclose(fp_list_in[i]); | |
517 | fp_list_in[i] = NULL; | |
518 | } | |
519 | } | |
520 | for ( ; ; ) { | |
521 | /* find the earliest timestamp */ | |
522 | int min_index = -1; | |
523 | for (i = 0; i < num_infiles; i++) { | |
524 | if (!EventList[i]->ignore && (min_index < 0 || | |
525 | EventCmp(EventList[min_index], EventList[i]) > 0)) | |
526 | min_index = i; | |
527 | } | |
528 | if (min_index < 0) break; /* no more events */ | |
529 | FlushEvent(EventList[min_index]); | |
530 | if (fp_list_in[min_index] != NULL) { | |
531 | /* read the next event from that file */ | |
532 | EventList[min_index]->wrap = 0; | |
533 | if (!GetEvent(fp_list_in[min_index], EventList[min_index])) { | |
534 | /*EOF or read error*/ | |
535 | fclose(fp_list_in[min_index]); | |
536 | fp_list_in[min_index] = NULL; | |
537 | } | |
538 | if (!infiles_processed && EventList[min_index]->wrap > 0) { | |
539 | fprintf(stderr,"%s: warning: timestamp is in wrong order " | |
540 | "in file %s line %ld\n", progname, | |
541 | name_list_in[min_index + start_file], | |
542 | EventList[min_index]->start_line); | |
543 | } | |
544 | } | |
545 | } | |
546 | for (i = 0; i < num_infiles; i++) { | |
547 | if (fp_list_in[i] != NULL) fclose(fp_list_in[i]); | |
548 | } | |
549 | Free(fp_list_in); | |
550 | fp_list_in = NULL; | |
551 | } | |
552 | ||
553 | static void DelTemp(void) | |
554 | { | |
555 | int a; | |
556 | for(a=0;a<num_tempfiles;a++) { | |
557 | fprintf(stderr, "%s: deleting temporary file %s\n", progname, | |
558 | temp_file_list[a]); | |
559 | remove(temp_file_list[a]); | |
560 | Free(temp_file_list[a]); | |
561 | } | |
562 | Free(temp_file_list); | |
563 | } | |
564 | ||
565 | static void Usage(void) | |
566 | { | |
567 | fprintf(stderr, | |
568 | "Usage: %s [-o outfile] file1.log [file2.log ...]\n" | |
569 | " or %s -v\n" | |
570 | "options:\n" | |
571 | " -o outfile: write merged logs into file outfile\n" | |
572 | " -v: print version\n" | |
573 | "If there is no outfile specified output is stdout.\n\n", | |
574 | progname,progname); | |
575 | } | |
576 | ||
577 | static void ControlChandler(int x) | |
578 | { | |
579 | (void)x; | |
580 | /* the temporary files will be deleted by exit() */ | |
581 | exit(EXIT_FAILURE); | |
582 | } | |
583 | ||
584 | int main(int argc,char *argv[]) | |
585 | { | |
586 | int a,b,c,processed_files=0,filename_count=0; | |
587 | char *outfile_name=NULL; | |
588 | int vflag=0,oflag=0; | |
589 | #ifdef LICENSE | |
590 | license_struct lstr; | |
591 | #endif | |
592 | progname=argv[0]; | |
593 | atexit(DelTemp); | |
594 | signal(SIGINT,ControlChandler); | |
595 | while ((c = getopt(argc, argv, "vo:")) != -1) { | |
596 | switch (c) { | |
597 | case 'o': | |
598 | outfile_name=optarg; | |
599 | oflag = 1; | |
600 | break; | |
601 | case 'v': | |
602 | vflag=1; | |
603 | break; | |
604 | default: Usage();return 0; | |
605 | } | |
606 | } | |
607 | if(oflag&&vflag){Usage();return 0;}/*both switches are used*/ | |
608 | if(vflag) { | |
609 | fputs("Log Merger for the TTCN-3 Test Executor\n" | |
610 | "Product number: " PRODUCT_NUMBER "\n" | |
611 | "Build date: " __DATE__ " " __TIME__ "\n" | |
612 | "Compiled with: " C_COMPILER_VERSION "\n\n" | |
613 | COPYRIGHT_STRING "\n\n", stderr); | |
614 | #ifdef LICENSE | |
615 | print_license_info(); | |
616 | #endif | |
617 | return 0; | |
618 | } | |
619 | #ifdef LICENSE | |
620 | init_openssl(); | |
621 | load_license(&lstr); | |
622 | if (!verify_license(&lstr)) { | |
623 | free_license(&lstr); | |
624 | free_openssl(); | |
625 | exit(EXIT_FAILURE); | |
626 | } | |
627 | if (!check_feature(&lstr, FEATURE_LOGFORMAT)) { | |
628 | fputs("The license key does not allow the merging of log files.\n", | |
629 | stderr); | |
630 | return 2; | |
631 | } | |
632 | free_license(&lstr); | |
633 | free_openssl(); | |
634 | #endif | |
635 | argc-=optind-1;argv+=optind-1; | |
636 | if(argc<2){Usage();return 0;}/*executed when no input file is given*/ | |
637 | for(a=1;a<argc;a++) {/*find first file with a valid timestamp*/ | |
638 | TimeStampUsed=GetTimeStampFormat(argv[a]); | |
639 | if(TimeStampUsed!=TSF_Undefined)break; | |
640 | } | |
641 | switch(TimeStampUsed) { | |
642 | case TSF_Seconds: fputs("Merging logs with timestamp " | |
643 | "format \"seconds\" has no sense.\n", stderr); return 0; | |
644 | case TSF_Time: TimeStampLength=TIMELENGTH;break; | |
645 | case TSF_DateTime: TimeStampLength=DATETIMELENGTH;break; | |
646 | default: fputs("Unsupported timestamp format.\n", stderr); return 1; | |
647 | } | |
648 | for(a=1,c=0;a<argc;a++) {/*get files with valid timestamp format*/ | |
649 | b=GetTimeStampFormat(argv[a]); | |
650 | if(TimeStampUsed==b) {/*file conains at least one valid timestamp*/ | |
651 | c++; | |
652 | name_list_in=(char **)Realloc(name_list_in,c*sizeof(char *)); | |
653 | name_list_in[c-1] = mcopystr(argv[a]); | |
654 | } else if(b==TSF_Undefined)/*file contains no timestamp or uses a | |
655 | different format than the first match*/ | |
656 | fprintf(stderr,"Warning: unknown format in %s\n",argv[a]); | |
657 | else fprintf(stderr,"Warning: format mismatch in %s\n",argv[a]); | |
658 | } | |
659 | num_allfiles=c; | |
660 | if(num_allfiles<1){Usage();return 0;}/*no valid log file found*/ | |
661 | if(oflag){/*switch [-o outfile] is used -> create outfile*/ | |
662 | outfile = fopen(outfile_name, FOPEN_WRITE); | |
663 | if(outfile==NULL) { | |
664 | fprintf(stderr,"Error creating %s %s\n",outfile_name,strerror(errno)); | |
665 | return 1; | |
666 | } | |
667 | } else { | |
668 | outfile = stdout; | |
669 | } | |
670 | while(1) { | |
671 | filename_count=num_allfiles;start_file=0; | |
672 | while(num_allfiles>0) {/*process files in name_list_in*/ | |
673 | processed_files=OpenMaxFiles(num_allfiles,name_list_in+start_file); | |
674 | must_use_temp=True;/*if there are infiles remaining use tempfiles | |
675 | for all*/ | |
676 | if((processed_files<2)&&(num_allfiles>1)){fprintf(stderr,"Error: " | |
677 | "can not open enough files.\nMore descriptors required " | |
678 | "(set with the command `limit descriptors\')\n");return 1;} | |
679 | if(infiles_processed==True) | |
680 | for(a=0;a<processed_files;a++) { | |
681 | Free(EventList[a]->str_to_add); | |
682 | EventList[a]->str_to_add = NULL; | |
683 | } | |
684 | num_allfiles-=processed_files; | |
685 | ProcessOpenFiles(); | |
686 | CloseAllFiles(); | |
687 | start_file+=processed_files; | |
688 | } | |
689 | must_use_temp=False;/*all infiles processed*/ | |
690 | /*remove temporary files used in previous step*/ | |
691 | if(infiles_processed==True) | |
692 | for(a=0;a<filename_count;a++)remove(name_list_in[a]); | |
693 | infiles_processed=True; | |
694 | for(a=0;a<filename_count;a++)Free(name_list_in[a]); | |
695 | Free(name_list_in); | |
696 | if(num_tempfiles==0)break;/*no more file to process*/ | |
697 | name_list_in=temp_file_list;/*process tempfiles*/ | |
698 | num_allfiles=num_tempfiles; | |
699 | num_tempfiles=0;temp_file_list=NULL; | |
700 | } | |
701 | check_mem_leak(progname); | |
702 | return 0; | |
703 | } |