Browse Source

Fix starts for #11

Adds the option -P --permissive.

Starts implementation for logfiles.

Now -P flag make ttail handle invalid files. When invalid file given the
minmax function set the file pointer and the minmax array to NULL.

May introduce bugs if the binary search function does not handles "holes"
in fp array or in minmax array...
Yann Weber 7 years ago
parent
commit
bbaa6b84b4
5 changed files with 70 additions and 6 deletions
  1. 6
    0
      src/include/ttail.h
  2. 3
    1
      src/include/ttail_init.h
  3. 8
    0
      src/ttail_init.c
  4. 1
    1
      src/ttail_search.c
  5. 52
    4
      src/ttail_search_files.c

+ 6
- 0
src/include/ttail.h View File

41
 #define TTAIL_FLAG_FORMAT 8
41
 #define TTAIL_FLAG_FORMAT 8
42
 #define TTAIL_FLAG_EXTENDED_RE 16
42
 #define TTAIL_FLAG_EXTENDED_RE 16
43
 #define TTAIL_FLAG_CI_RE 32
43
 #define TTAIL_FLAG_CI_RE 32
44
+/**! When set indicate that ttail is in permissive mode */
45
+#define TTAIL_FLAG_PERMISSIVE 64
44
 
46
 
45
 #define TTAIL_DEFAULT_FORMATS {"%m",\
47
 #define TTAIL_DEFAULT_FORMATS {"%m",\
46
 "%A %B %d, %Y %H:%M:%S",\
48
 "%A %B %d, %Y %H:%M:%S",\
62
 "%y-%m-%d",\
64
 "%y-%m-%d",\
63
 "%Y/%m/%d:%H:%M",NULL}
65
 "%Y/%m/%d:%H:%M",NULL}
64
 
66
 
67
+#define ttail_permissive(t) ( t->flag & TTAIL_FLAG_PERMISSIVE )
68
+#define ttail_strict_msg() fprintf(stderr, "This error can be non-fatal using \
69
+-P --permissive flag\n")
70
+
65
 
71
 
66
 struct _ttail_s
72
 struct _ttail_s
67
 {
73
 {

+ 3
- 1
src/include/ttail_init.h View File

42
 	{"date-format", required_argument, 0, 'f'},\
42
 	{"date-format", required_argument, 0, 'f'},\
43
 	{"date-min", required_argument, 0, 'd'},\
43
 	{"date-min", required_argument, 0, 'd'},\
44
 	{"date-max", required_argument, 0, 'm'},\
44
 	{"date-max", required_argument, 0, 'm'},\
45
+	{"permissive", no_argument, 0, 'P'},\
45
 	{"help", no_argument, 0, 'h'},\
46
 	{"help", no_argument, 0, 'h'},\
46
 	{0,	0,	0,	0 }\
47
 	{0,	0,	0,	0 }\
47
 }
48
 }
48
-#define TTAIL_SHORT_OPT "vr:Eip:f:d:l:m:h"
49
+#define TTAIL_SHORT_OPT "vr:Eip:f:d:l:m:Ph"
49
 
50
 
50
 #define TTAIL_OPT_HELP {\
51
 #define TTAIL_OPT_HELP {\
51
 	{"Augment the verbosity level",NULL},\
52
 	{"Augment the verbosity level",NULL},\
58
 ,"FORMAT"},\
59
 ,"FORMAT"},\
59
 	{"Start to output loglines starting from this date","DATE"},\
60
 	{"Start to output loglines starting from this date","DATE"},\
60
 	{"Stop to output loglines before this date","DATE"},\
61
 	{"Stop to output loglines before this date","DATE"},\
62
+	{"Tell ttail to not stop on loglines not formatted as expected",NULL},\
61
 	{"Print this help and exit",NULL},\
63
 	{"Print this help and exit",NULL},\
62
 	{"",NULL}\
64
 	{"",NULL}\
63
 }
65
 }

+ 8
- 0
src/ttail_init.c View File

151
 				strcpy(date, optarg);
151
 				strcpy(date, optarg);
152
 				dates[c=='d'?0:1] = date;
152
 				dates[c=='d'?0:1] = date;
153
 				break;
153
 				break;
154
+			case 'P':
155
+				if(res->flag & TTAIL_FLAG_PERMISSIVE)
156
+				{
157
+					fprintf(stderr, "Warning : looks like \
158
+-P --permissive flag was set more than once\n");
159
+				}
160
+				res->flag |= TTAIL_FLAG_PERMISSIVE;
161
+				break;
154
 			default: /* ? */
162
 			default: /* ? */
155
 				goto ttail_init_err;
163
 				goto ttail_init_err;
156
 				
164
 				

+ 1
- 1
src/ttail_search.c View File

117
 	if(ret)
117
 	if(ret)
118
 	{
118
 	{
119
 		regerror(ret, &(t->date_prefix), err,1024);
119
 		regerror(ret, &(t->date_prefix), err,1024);
120
-		fprintf(stderr, "Exec error : %s\n", err);
120
+		fprintf(stderr, "RegEx exec error : %s\n", err);
121
 		return NULL;
121
 		return NULL;
122
 	}
122
 	}
123
 	return logline + pmatch[0].rm_eo;
123
 	return logline + pmatch[0].rm_eo;

+ 52
- 4
src/ttail_search_files.c View File

51
 		ret = _ttail_file_minmax(t, i, ftm[i]);
51
 		ret = _ttail_file_minmax(t, i, ftm[i]);
52
 		if(ret < 0)
52
 		if(ret < 0)
53
 		{
53
 		{
54
-			fprintf(stderr, "Minmax error\n");
55
 			goto _ttail_search_closest_files_loop_err;
54
 			goto _ttail_search_closest_files_loop_err;
56
 		}
55
 		}
57
 		else if (ret == 1)
56
 		else if (ret == 1)
58
 		{
57
 		{
59
-			fprintf(stderr, "Warning : unable to find a valid date \
58
+			fprintf(stderr, "Error : unable to find a valid date \
60
 in '%s'\n", t->logfile_name[i]);
59
 in '%s'\n", t->logfile_name[i]);
61
 			free(ftm[i]);
60
 			free(ftm[i]);
62
 			ftm[i] = NULL;
61
 			ftm[i] = NULL;
63
 			fclose(t->logfile[i]);
62
 			fclose(t->logfile[i]);
64
 			t->logfile[i] = NULL;
63
 			t->logfile[i] = NULL;
64
+			if(!ttail_permissive(t))
65
+			{
66
+				ttail_strict_msg();
67
+				goto _ttail_search_closest_files_loop_err;
68
+			}
65
 			continue;
69
 			continue;
66
 		}
70
 		}
67
 		if(i && prev_found &&
71
 		if(i && prev_found &&
312
 				}
316
 				}
313
 				cmpres=0;
317
 				cmpres=0;
314
 				ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
318
 				ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
319
+				if(!ret && !ttail_permissive(t))
320
+				{
321
+					ttail_strict_msg();
322
+					return -1;
323
+				}
315
 				if((min && cmpres < 0) || (!min && cmpres > 0))
324
 				if((min && cmpres < 0) || (!min && cmpres > 0))
316
 				{
325
 				{
317
 					break;
326
 					break;
336
 		fprintf(stderr, "No files to scan\n");
345
 		fprintf(stderr, "No files to scan\n");
337
 		return 0;
346
 		return 0;
338
 	}
347
 	}
348
+	if(*id == t->logfile_sz)
349
+	{
350
+		return 0;
351
+	}
339
 	/* the answer is somewhere in *id file */
352
 	/* the answer is somewhere in *id file */
340
 	*off = _ttail_file_search_from_end(t, *id, in);
353
 	*off = _ttail_file_search_from_end(t, *id, in);
341
 	return 0;
354
 	return 0;
606
 	fp = t->logfile[id];
619
 	fp = t->logfile[id];
607
 	if(!fp)
620
 	if(!fp)
608
 	{
621
 	{
622
+		fprintf(stderr, "File pointer is null !\n");
609
 		return 1;
623
 		return 1;
610
 	}
624
 	}
611
 	if(fseek(fp, 0, SEEK_SET) < 0)
625
 	if(fseek(fp, 0, SEEK_SET) < 0)
623
 		{
637
 		{
624
 			break;
638
 			break;
625
 		}
639
 		}
640
+		if(!ttail_permissive(t))
641
+		{
642
+			if(t->verbose <= 0)
643
+			{
644
+				fprintf(stderr,
645
+					"Unable to find a date in logline\n");
646
+			}
647
+			else
648
+			{
649
+				fprintf(stderr,
650
+					"Unable to find a date in '%s'\n",
651
+					ttail_file_getline_buf(t));
652
+			}
653
+			ttail_strict_msg();
654
+			return -1;
655
+		}
626
 	}
656
 	}
627
 	if(fseek(fp, -1, SEEK_END) < 0)
657
 	if(fseek(fp, -1, SEEK_END) < 0)
628
 	{
658
 	{
633
 	{
663
 	{
634
 		if((cur = _ttail_file_start_line(t, id)) < 0)
664
 		if((cur = _ttail_file_start_line(t, id)) < 0)
635
 		{
665
 		{
666
+			fprintf(stderr, "Error will searching line starts in\
667
+%s\n", t->logfile_name[id]);
636
 			return -1;
668
 			return -1;
637
 		}
669
 		}
638
 		if(ttail_file_getline(t, id) < 0)
670
 		if(ttail_file_getline(t, id) < 0)
647
 		{
679
 		{
648
 			return 1;
680
 			return 1;
649
 		}
681
 		}
682
+		else if(!ttail_permissive(t))
683
+		{
684
+			if(t->verbose <= 0)
685
+			{
686
+				fprintf(stderr,
687
+					"Unable to find a date in logline\n");
688
+			}
689
+			else
690
+			{
691
+				fprintf(stderr,
692
+					"Unable to find a date in '%s'\n",
693
+					ttail_file_getline_buf(t));
694
+			}
695
+			ttail_strict_msg();
696
+			return -1;
697
+		}
650
 		if(fseek(fp, cur-1, SEEK_SET) < 0)
698
 		if(fseek(fp, cur-1, SEEK_SET) < 0)
651
 		{
699
 		{
652
 			perror("Unable to manipulate fp");
700
 			perror("Unable to manipulate fp");
872
 		return -1;
920
 		return -1;
873
 	}
921
 	}
874
 	ret = ttail_logline2date(t, ttail_file_getline_buf(t), &ctm);
922
 	ret = ttail_logline2date(t, ttail_file_getline_buf(t), &ctm);
875
-	if(ret)
923
+	if(ret < 0)
876
 	{
924
 	{
877
 		return -1;
925
 		return -1;
878
 	}
926
 	}
879
-	else if(ret == 1)
927
+	else if(ret > 1)
880
 	{
928
 	{
881
 		return 1;
929
 		return 1;
882
 	}
930
 	}

Loading…
Cancel
Save