Преглед изворни кода

Fixing issue #4 for logfiles

Not done for stdin
Yann Weber пре 7 година
родитељ
комит
7dbc99e4a0

+ 0
- 7
src/include/ttail.h Прегледај датотеку

@@ -59,9 +59,6 @@ typedef struct _ttail_s ttail_t;
59 59
 "%Y/%m/%d:%H:%M",NULL}
60 60
 
61 61
 
62
-/**@brief Pointer on autodetect format provider */
63
-typedef int (*ttail_search_fmtdetect_provider)(ttail_t*, int*, size_t*, char***);
64
-
65 62
 struct _ttail_s
66 63
 {
67 64
 	char **logfile_name; /*!< logfiles name */
@@ -89,10 +86,6 @@ struct _ttail_s
89 86
 	int flag;
90 87
 
91 88
 	ttail_search_t *session;
92
-	
93
-	/**@brief Set by session init function to allow format autodetection
94
-	 * on loglines */
95
-	ttail_search_fmtdetect_provider fmtdetect_provider;
96 89
 };
97 90
 
98 91
 #endif

+ 1
- 1
src/include/ttail_init.h Прегледај датотеку

@@ -300,7 +300,7 @@ int _ttail_set_date_relative(ttail_t*, char*, int);
300 300
  *@param ttail_t* An initialized ttail_t
301 301
  *@return -1 if error 0 else
302 302
  */
303
-int _ttail_norm_dates(ttail_t*);
303
+int ttail_norm_dates(ttail_t*);
304 304
 
305 305
 /**@brief Attempt to guess a dateformat
306 306
  *@param const char* date as a dtring

+ 10
- 10
src/include/ttail_search_files.h Прегледај датотеку

@@ -29,7 +29,6 @@
29 29
 
30 30
 typedef struct _ttail_search_file_s ttail_search_file_t;
31 31
 
32
-
33 32
 struct _ttail_files_off_s
34 33
 {
35 34
 	size_t id;
@@ -73,6 +72,7 @@ struct _ttail_search_file_s
73 72
 
74 73
 #include "config.h"
75 74
 #include "ttail.h"
75
+#include "ttail_init.h"
76 76
 #include "ttail_search.h"
77 77
 
78 78
 /**@brief Convenient wrapper for getline
@@ -93,6 +93,15 @@ struct _ttail_search_file_s
93 93
  */
94 94
 int ttail_search_files_init(ttail_t*);
95 95
 
96
+/**@brief Date format initialisation using logfiles
97
+ *
98
+ *If no format set yet attempt to guess it from logfiles
99
+ *@param ttail_t*
100
+ *@return 0 on success -1 on failure 1 if format was allready set
101
+ *@see _ttail_search_files_fmt_guess()
102
+ */
103
+int _ttail_search_files_fmt_init(ttail_t*);
104
+
96 105
 /**@brief @ref ttail_search_closest() implementation for logfiles
97 106
  *
98 107
  *@warning Expect that ttail_search_closest_files_init() has been called
@@ -102,15 +111,6 @@ int ttail_search_files_init(ttail_t*);
102 111
  */
103 112
 int _ttail_search_closest_files(ttail_t*);
104 113
 
105
-/**@brief Provide datas for format autodetection
106
- *@param ttail_t* t
107
- *@param int* : retry left
108
- *@param size_t* : line count
109
- *@param char*** : pointer on lines buffer
110
- *@return -1 on error 1 on autodetection impossible 0 on success
111
- */
112
-int _ttail_search_fmtdetect_provider_files(ttail_t*, int*, size_t*, char***);
113
-
114 114
 /**@brief Output result loglines to stdout
115 115
  *@param ttail_t*
116 116
  *@param int fd

+ 20
- 0
src/main.c Прегледај датотеку

@@ -23,6 +23,7 @@ int main(int argc, char **argv)
23 23
 {
24 24
 	int res;
25 25
 	ttail_t *ttail;
26
+
26 27
 	if(!(ttail = ttail_init(argc, argv)))
27 28
 	{
28 29
 		usage();
@@ -40,6 +41,25 @@ int main(int argc, char **argv)
40 41
 		ttail_free(ttail);
41 42
 		return 1;
42 43
 	}
44
+
45
+	//Check that a format was detected
46
+	if(!(ttail->flag & TTAIL_FLAG_FORMAT))
47
+	{
48
+		fprintf(stderr, "No date format set nor detected. Abording.\n");
49
+		return -1;
50
+	}
51
+	else if(!ttail->fmt)
52
+	{
53
+		fprintf(stderr, "Date format flag set but no format found\n");
54
+		return -1;
55
+	}
56
+
57
+	if(ttail_norm_dates(ttail) < 0)
58
+	{
59
+		fprintf(stderr, "Unable to normalize dates\n");
60
+		return -1;
61
+	}
62
+
43 63
 	res = ttail_search_closest(ttail);
44 64
 	if(res < 0)
45 65
 	{

+ 10
- 14
src/ttail_init.c Прегледај датотеку

@@ -73,7 +73,7 @@ ttail_t *ttail_init(int argc, char **argv)
73 73
 	res->logfile = NULL;
74 74
 	res->logfile_name = NULL;
75 75
 	res->logfile_sz = 0;
76
-	res->prefix_sz = -1;
76
+	res->prefix_sz = 0;
77 77
 	res->session = NULL;
78 78
 	ttail_tm_init(&(res->date_min));
79 79
 	ttail_tm_init(&(res->date_max));
@@ -222,16 +222,6 @@ mandatory\n");
222 222
 			return -1;
223 223
 		}
224 224
 	}
225
-	if(!(t->flag & TTAIL_FLAG_FORMAT))
226
-	{
227
-		fprintf(stderr, "No date format set nor detected. Abording.\n");
228
-		return -1;
229
-	}
230
-	else if(!t->fmt)
231
-	{
232
-		fprintf(stderr, "Date format flag set but no format found\n");
233
-		return -1;
234
-	}
235 225
 	return 0;
236 226
 }
237 227
 
@@ -324,6 +314,7 @@ int ttail_set_prefix(ttail_t* res, const char* regex)
324 314
 	ret = regcomp(&res->date_prefix, regex, cflags);
325 315
 	if(!ret)
326 316
 	{
317
+		res->prefix_sz = -1;
327 318
 		return 0;
328 319
 	}
329 320
 	/*compilation error */
@@ -488,7 +479,7 @@ is not matched in '%s'\n",\
488 479
 int _ttail_set_date_relative(ttail_t* res, char* date, int c)
489 480
 {
490 481
 	time_t now;
491
-	char *unit;
482
+	char *unit, str_date[64];
492 483
 	int value;
493 484
 	struct tm *tm, *tm_p;
494 485
 	short mod;
@@ -613,11 +604,17 @@ int _ttail_set_date_relative(ttail_t* res, char* date, int c)
613 604
 			fprintf(stderr,"Invalid relative date '%s'\n", date);
614 605
 			return -1;
615 606
 	}
607
+	if(res->verbose > 0)
608
+	{
609
+		strftime(str_date, 64, "%c", tm);
610
+		fprintf(stderr, "%s date set to %s\n", 
611
+			c?"stop":"start", str_date);
612
+	}
616 613
 	return 0;
617 614
 
618 615
 }
619 616
 
620
-int _ttail_norm_dates(ttail_t* ttail)
617
+int ttail_norm_dates(ttail_t* ttail)
621 618
 {
622 619
 	struct tm *tm_p, tm;
623 620
 	/* Huge buffer but no way to make a difference between error and
@@ -697,7 +694,6 @@ int ttail_format_guess(const char* date_str, struct tm* tm)
697 694
 		}
698 695
 		return -1;
699 696
 	}
700
-
701 697
 	return res;
702 698
 }
703 699
 

+ 66
- 7
src/ttail_search_files.c Прегледај датотеку

@@ -141,12 +141,6 @@ date-max\n");
141 141
 	return -1;
142 142
 }
143 143
 
144
-int _ttail_search_fmtdetect_provider_files(ttail_t *t, int *try_left, 
145
-		size_t* line_sz, char*** lines_ptr)
146
-{
147
-	return 1;
148
-}
149
-
150 144
 void _ttail_search_print_files(ttail_t* t, int out)
151 145
 {
152 146
 	size_t i;
@@ -500,7 +494,11 @@ int ttail_search_files_init(ttail_t* t)
500 494
 	{
501 495
 		goto _ttail_search_closest_files_err;
502 496
 	}
503
-	t->fmtdetect_provider = _ttail_search_fmtdetect_provider_files;
497
+
498
+	if(_ttail_search_files_fmt_init(t) < 0)
499
+	{
500
+		goto _ttail_search_closest_files_err;
501
+	}
504 502
 
505 503
 	return 0;
506 504
 
@@ -513,6 +511,67 @@ int ttail_search_files_init(ttail_t* t)
513 511
 	return -1;
514 512
 }
515 513
 
514
+int _ttail_search_files_fmt_init(ttail_t* t)
515
+{
516
+	int fmt_id;
517
+	size_t i,j;
518
+	const char *buff;
519
+	const char *fmt[] = TTAIL_DEFAULT_FORMATS;
520
+
521
+	if(t->flag & TTAIL_FLAG_FORMAT)
522
+	{
523
+		return 1;
524
+	}
525
+
526
+	fmt_id = -1;
527
+	for(i=0; i<t->logfile_sz; i++)
528
+	{
529
+		if(!t->logfile[i]) {
530
+			continue;
531
+		}
532
+		for(j=0; j<10; j++)
533
+		{
534
+			//try to guess fmt on the 10 first lines
535
+			if(ttail_file_getline(t, i) < 0) {
536
+				break;
537
+			}
538
+			buff = ttail_logline_subst(t, ttail_file_getline_buf(t));
539
+			//buff contains the lines starting by the date
540
+			fmt_id = ttail_format_guess(buff, NULL);
541
+			if(fmt_id >= 0)
542
+			{
543
+				break;
544
+			}
545
+		}
546
+		rewind(t->logfile[i]);
547
+		if(fmt_id >= 0)
548
+		{
549
+			if(t->verbose > 0)
550
+			{
551
+				fprintf(stderr,
552
+					"Detected format %s in file %s\n",
553
+					fmt[fmt_id], t->logfile_name[i]);
554
+			}
555
+			break;
556
+		}
557
+	}
558
+	if(fmt_id >= 0)
559
+	{
560
+		buff = fmt[fmt_id];
561
+		t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
562
+		if(!t->fmt)
563
+		{
564
+			perror("Unable to allocate memory for date format");
565
+			return -1;
566
+		}
567
+		strcpy(t->fmt, buff);
568
+		t->flag |= TTAIL_FLAG_FORMAT;
569
+		return 0;
570
+	}
571
+	fprintf(stderr, "Unable to detect date format from logfiles\n");
572
+	return -1;
573
+}
574
+
516 575
 int _ttail_search_closest_files_set_fsizes(ttail_t* t)
517 576
 {
518 577
 	size_t i;

+ 0
- 1
src/ttail_search_std.c Прегледај датотеку

@@ -28,7 +28,6 @@ int ttail_search_std_init(ttail_t* t)
28 28
 	}
29 29
 	t->session->std.buff = NULL;
30 30
 	t->session->std.buff_sz = 0;
31
-	t->fmtdetect_provider = _ttail_search_fmtdetect_provider_std;
32 31
 	return 0;
33 32
 }
34 33
 

+ 2
- 2
tests/ttail_argparse_empty.c Прегледај датотеку

@@ -48,8 +48,8 @@ START_TEST (test_argparse_empty_verbose)
48 48
 END_TEST
49 49
 START_TEST (test_argparse_empty_prefixsz)
50 50
 {
51
-	ck_assert_msg(ttail->prefix_sz == -1,
52
-		"ttail_t.prefix_sz should be NULL");
51
+	ck_assert_msg(ttail->prefix_sz == 0,
52
+		"ttail_t.prefix_sz should be 0");
53 53
 }
54 54
 END_TEST
55 55
 START_TEST (test_argparse_empty_session)

+ 0
- 8
tests/ttail_init_check.c Прегледај датотеку

@@ -13,13 +13,6 @@ START_TEST (test_init_check_bad1)
13 13
 }
14 14
 END_TEST
15 15
 
16
-START_TEST (test_init_check_bad2)
17
-{
18
-	ttail->flag |= TTAIL_FLAG_DATE_MAX;
19
-	ck_assert_int_eq(ttail_init_check(ttail), -1);
20
-}
21
-END_TEST
22
-
23 16
 START_TEST (test_init_check1)
24 17
 {
25 18
 	char *arg[] = {"88/10/22", NULL};
@@ -45,7 +38,6 @@ END_TEST
45 38
 TTAIL_CHECK_START("ttail init checks", "ttail_init_check() checks")
46 39
 	TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail);
47 40
 	TTAIL_ADD_TEST(test_init_check_bad1);
48
-	TTAIL_ADD_TEST(test_init_check_bad2);
49 41
 	TTAIL_ADD_TEST(test_init_check1);
50 42
 TTAIL_CHECK_END
51 43
 

+ 5
- 5
tests/ttail_init_norm_dates.c Прегледај датотеку

@@ -19,7 +19,7 @@ START_TEST (test_norm_dates_flags_none)
19 19
 	ttail->date_min = tm;
20 20
 	ttail->date_max = tm;
21 21
 
22
-	ret = _ttail_norm_dates(ttail);
22
+	ret = ttail_norm_dates(ttail);
23 23
 	ck_assert_int_eq(ret, 0);
24 24
 	
25 25
 	tm = ttail->date_min;
@@ -52,7 +52,7 @@ START_TEST (test_norm_dates_flags_min)
52 52
 	ttail->date_min = tm;
53 53
 	ttail->date_max = tm;
54 54
 
55
-	ret = _ttail_norm_dates(ttail);
55
+	ret = ttail_norm_dates(ttail);
56 56
 	ck_assert_int_eq(ret, 0);
57 57
 	
58 58
 	tm = ttail->date_min;
@@ -85,7 +85,7 @@ START_TEST (test_norm_dates_flags_max)
85 85
 	ttail->date_min = tm;
86 86
 	ttail->date_max = tm;
87 87
 
88
-	ret = _ttail_norm_dates(ttail);
88
+	ret = ttail_norm_dates(ttail);
89 89
 	ck_assert_int_eq(ret, 0);
90 90
 	
91 91
 	tm = ttail->date_min;
@@ -119,7 +119,7 @@ START_TEST (test_norm_dates_flags_both)
119 119
 	ttail->date_min = tm;
120 120
 	ttail->date_max = tm;
121 121
 
122
-	ret = _ttail_norm_dates(ttail);
122
+	ret = ttail_norm_dates(ttail);
123 123
 	ck_assert_int_eq(ret, 0);
124 124
 	
125 125
 	tm = ttail->date_min;
@@ -153,7 +153,7 @@ START_TEST (test_norm_dates_flags_full)
153 153
 	ttail->date_min = tm;
154 154
 	ttail->date_max = tm;
155 155
 
156
-	ret = _ttail_norm_dates(ttail);
156
+	ret = ttail_norm_dates(ttail);
157 157
 	ck_assert_int_eq(ret, 0);
158 158
 	
159 159
 	tm = ttail->date_min;

Loading…
Откажи
Сачувај