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,6 +41,8 @@ typedef struct _ttail_s ttail_t;
41 41
 #define TTAIL_FLAG_FORMAT 8
42 42
 #define TTAIL_FLAG_EXTENDED_RE 16
43 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 47
 #define TTAIL_DEFAULT_FORMATS {"%m",\
46 48
 "%A %B %d, %Y %H:%M:%S",\
@@ -62,6 +64,10 @@ typedef struct _ttail_s ttail_t;
62 64
 "%y-%m-%d",\
63 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 72
 struct _ttail_s
67 73
 {

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

@@ -42,10 +42,11 @@
42 42
 	{"date-format", required_argument, 0, 'f'},\
43 43
 	{"date-min", required_argument, 0, 'd'},\
44 44
 	{"date-max", required_argument, 0, 'm'},\
45
+	{"permissive", no_argument, 0, 'P'},\
45 46
 	{"help", no_argument, 0, 'h'},\
46 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 51
 #define TTAIL_OPT_HELP {\
51 52
 	{"Augment the verbosity level",NULL},\
@@ -58,6 +59,7 @@ POSIX) ",NULL},\
58 59
 ,"FORMAT"},\
59 60
 	{"Start to output loglines starting from this date","DATE"},\
60 61
 	{"Stop to output loglines before this date","DATE"},\
62
+	{"Tell ttail to not stop on loglines not formatted as expected",NULL},\
61 63
 	{"Print this help and exit",NULL},\
62 64
 	{"",NULL}\
63 65
 }

+ 8
- 0
src/ttail_init.c View File

@@ -151,6 +151,14 @@ given\n");
151 151
 				strcpy(date, optarg);
152 152
 				dates[c=='d'?0:1] = date;
153 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 162
 			default: /* ? */
155 163
 				goto ttail_init_err;
156 164
 				

+ 1
- 1
src/ttail_search.c View File

@@ -117,7 +117,7 @@ const char* ttail_logline_subst(ttail_t* t, const char* logline)
117 117
 	if(ret)
118 118
 	{
119 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 121
 		return NULL;
122 122
 	}
123 123
 	return logline + pmatch[0].rm_eo;

+ 52
- 4
src/ttail_search_files.c View File

@@ -51,17 +51,21 @@ int _ttail_search_closest_files(ttail_t* t)
51 51
 		ret = _ttail_file_minmax(t, i, ftm[i]);
52 52
 		if(ret < 0)
53 53
 		{
54
-			fprintf(stderr, "Minmax error\n");
55 54
 			goto _ttail_search_closest_files_loop_err;
56 55
 		}
57 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 59
 in '%s'\n", t->logfile_name[i]);
61 60
 			free(ftm[i]);
62 61
 			ftm[i] = NULL;
63 62
 			fclose(t->logfile[i]);
64 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 69
 			continue;
66 70
 		}
67 71
 		if(i && prev_found &&
@@ -312,6 +316,11 @@ running binary search algorithm in '%s'\n", t->logfile_name[*id]);
312 316
 				}
313 317
 				cmpres=0;
314 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 324
 				if((min && cmpres < 0) || (!min && cmpres > 0))
316 325
 				{
317 326
 					break;
@@ -336,6 +345,10 @@ running binary search algorithm in '%s'\n", t->logfile_name[*id]);
336 345
 		fprintf(stderr, "No files to scan\n");
337 346
 		return 0;
338 347
 	}
348
+	if(*id == t->logfile_sz)
349
+	{
350
+		return 0;
351
+	}
339 352
 	/* the answer is somewhere in *id file */
340 353
 	*off = _ttail_file_search_from_end(t, *id, in);
341 354
 	return 0;
@@ -606,6 +619,7 @@ inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
606 619
 	fp = t->logfile[id];
607 620
 	if(!fp)
608 621
 	{
622
+		fprintf(stderr, "File pointer is null !\n");
609 623
 		return 1;
610 624
 	}
611 625
 	if(fseek(fp, 0, SEEK_SET) < 0)
@@ -623,6 +637,22 @@ inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
623 637
 		{
624 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 657
 	if(fseek(fp, -1, SEEK_END) < 0)
628 658
 	{
@@ -633,6 +663,8 @@ inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
633 663
 	{
634 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 668
 			return -1;
637 669
 		}
638 670
 		if(ttail_file_getline(t, id) < 0)
@@ -647,6 +679,22 @@ inline int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
647 679
 		{
648 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 698
 		if(fseek(fp, cur-1, SEEK_SET) < 0)
651 699
 		{
652 700
 			perror("Unable to manipulate fp");
@@ -872,11 +920,11 @@ inline int _ttail_file_cur_cmp(ttail_t* t, size_t id, const struct tm* tm ,
872 920
 		return -1;
873 921
 	}
874 922
 	ret = ttail_logline2date(t, ttail_file_getline_buf(t), &ctm);
875
-	if(ret)
923
+	if(ret < 0)
876 924
 	{
877 925
 		return -1;
878 926
 	}
879
-	else if(ret == 1)
927
+	else if(ret > 1)
880 928
 	{
881 929
 		return 1;
882 930
 	}

Loading…
Cancel
Save