Browse Source

Fixing issue #4 for logfiles

Not done for stdin
Yann Weber 7 years ago
parent
commit
7dbc99e4a0

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

59
 "%Y/%m/%d:%H:%M",NULL}
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
 struct _ttail_s
62
 struct _ttail_s
66
 {
63
 {
67
 	char **logfile_name; /*!< logfiles name */
64
 	char **logfile_name; /*!< logfiles name */
89
 	int flag;
86
 	int flag;
90
 
87
 
91
 	ttail_search_t *session;
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
 #endif
91
 #endif

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

300
  *@param ttail_t* An initialized ttail_t
300
  *@param ttail_t* An initialized ttail_t
301
  *@return -1 if error 0 else
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
 /**@brief Attempt to guess a dateformat
305
 /**@brief Attempt to guess a dateformat
306
  *@param const char* date as a dtring
306
  *@param const char* date as a dtring

+ 10
- 10
src/include/ttail_search_files.h View File

29
 
29
 
30
 typedef struct _ttail_search_file_s ttail_search_file_t;
30
 typedef struct _ttail_search_file_s ttail_search_file_t;
31
 
31
 
32
-
33
 struct _ttail_files_off_s
32
 struct _ttail_files_off_s
34
 {
33
 {
35
 	size_t id;
34
 	size_t id;
73
 
72
 
74
 #include "config.h"
73
 #include "config.h"
75
 #include "ttail.h"
74
 #include "ttail.h"
75
+#include "ttail_init.h"
76
 #include "ttail_search.h"
76
 #include "ttail_search.h"
77
 
77
 
78
 /**@brief Convenient wrapper for getline
78
 /**@brief Convenient wrapper for getline
93
  */
93
  */
94
 int ttail_search_files_init(ttail_t*);
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
 /**@brief @ref ttail_search_closest() implementation for logfiles
105
 /**@brief @ref ttail_search_closest() implementation for logfiles
97
  *
106
  *
98
  *@warning Expect that ttail_search_closest_files_init() has been called
107
  *@warning Expect that ttail_search_closest_files_init() has been called
102
  */
111
  */
103
 int _ttail_search_closest_files(ttail_t*);
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
 /**@brief Output result loglines to stdout
114
 /**@brief Output result loglines to stdout
115
  *@param ttail_t*
115
  *@param ttail_t*
116
  *@param int fd
116
  *@param int fd

+ 20
- 0
src/main.c View File

23
 {
23
 {
24
 	int res;
24
 	int res;
25
 	ttail_t *ttail;
25
 	ttail_t *ttail;
26
+
26
 	if(!(ttail = ttail_init(argc, argv)))
27
 	if(!(ttail = ttail_init(argc, argv)))
27
 	{
28
 	{
28
 		usage();
29
 		usage();
40
 		ttail_free(ttail);
41
 		ttail_free(ttail);
41
 		return 1;
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
 	res = ttail_search_closest(ttail);
63
 	res = ttail_search_closest(ttail);
44
 	if(res < 0)
64
 	if(res < 0)
45
 	{
65
 	{

+ 10
- 14
src/ttail_init.c View File

73
 	res->logfile = NULL;
73
 	res->logfile = NULL;
74
 	res->logfile_name = NULL;
74
 	res->logfile_name = NULL;
75
 	res->logfile_sz = 0;
75
 	res->logfile_sz = 0;
76
-	res->prefix_sz = -1;
76
+	res->prefix_sz = 0;
77
 	res->session = NULL;
77
 	res->session = NULL;
78
 	ttail_tm_init(&(res->date_min));
78
 	ttail_tm_init(&(res->date_min));
79
 	ttail_tm_init(&(res->date_max));
79
 	ttail_tm_init(&(res->date_max));
222
 			return -1;
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
 	return 0;
225
 	return 0;
236
 }
226
 }
237
 
227
 
324
 	ret = regcomp(&res->date_prefix, regex, cflags);
314
 	ret = regcomp(&res->date_prefix, regex, cflags);
325
 	if(!ret)
315
 	if(!ret)
326
 	{
316
 	{
317
+		res->prefix_sz = -1;
327
 		return 0;
318
 		return 0;
328
 	}
319
 	}
329
 	/*compilation error */
320
 	/*compilation error */
488
 int _ttail_set_date_relative(ttail_t* res, char* date, int c)
479
 int _ttail_set_date_relative(ttail_t* res, char* date, int c)
489
 {
480
 {
490
 	time_t now;
481
 	time_t now;
491
-	char *unit;
482
+	char *unit, str_date[64];
492
 	int value;
483
 	int value;
493
 	struct tm *tm, *tm_p;
484
 	struct tm *tm, *tm_p;
494
 	short mod;
485
 	short mod;
613
 			fprintf(stderr,"Invalid relative date '%s'\n", date);
604
 			fprintf(stderr,"Invalid relative date '%s'\n", date);
614
 			return -1;
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
 	return 0;
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
 	struct tm *tm_p, tm;
619
 	struct tm *tm_p, tm;
623
 	/* Huge buffer but no way to make a difference between error and
620
 	/* Huge buffer but no way to make a difference between error and
697
 		}
694
 		}
698
 		return -1;
695
 		return -1;
699
 	}
696
 	}
700
-
701
 	return res;
697
 	return res;
702
 }
698
 }
703
 
699
 

+ 66
- 7
src/ttail_search_files.c View File

141
 	return -1;
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
 void _ttail_search_print_files(ttail_t* t, int out)
144
 void _ttail_search_print_files(ttail_t* t, int out)
151
 {
145
 {
152
 	size_t i;
146
 	size_t i;
500
 	{
494
 	{
501
 		goto _ttail_search_closest_files_err;
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
 	return 0;
503
 	return 0;
506
 
504
 
513
 	return -1;
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
 int _ttail_search_closest_files_set_fsizes(ttail_t* t)
575
 int _ttail_search_closest_files_set_fsizes(ttail_t* t)
517
 {
576
 {
518
 	size_t i;
577
 	size_t i;

+ 0
- 1
src/ttail_search_std.c View File

28
 	}
28
 	}
29
 	t->session->std.buff = NULL;
29
 	t->session->std.buff = NULL;
30
 	t->session->std.buff_sz = 0;
30
 	t->session->std.buff_sz = 0;
31
-	t->fmtdetect_provider = _ttail_search_fmtdetect_provider_std;
32
 	return 0;
31
 	return 0;
33
 }
32
 }
34
 
33
 

+ 2
- 2
tests/ttail_argparse_empty.c View File

48
 END_TEST
48
 END_TEST
49
 START_TEST (test_argparse_empty_prefixsz)
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
 END_TEST
54
 END_TEST
55
 START_TEST (test_argparse_empty_session)
55
 START_TEST (test_argparse_empty_session)

+ 0
- 8
tests/ttail_init_check.c View File

13
 }
13
 }
14
 END_TEST
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
 START_TEST (test_init_check1)
16
 START_TEST (test_init_check1)
24
 {
17
 {
25
 	char *arg[] = {"88/10/22", NULL};
18
 	char *arg[] = {"88/10/22", NULL};
45
 TTAIL_CHECK_START("ttail init checks", "ttail_init_check() checks")
38
 TTAIL_CHECK_START("ttail init checks", "ttail_init_check() checks")
46
 	TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail);
39
 	TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail);
47
 	TTAIL_ADD_TEST(test_init_check_bad1);
40
 	TTAIL_ADD_TEST(test_init_check_bad1);
48
-	TTAIL_ADD_TEST(test_init_check_bad2);
49
 	TTAIL_ADD_TEST(test_init_check1);
41
 	TTAIL_ADD_TEST(test_init_check1);
50
 TTAIL_CHECK_END
42
 TTAIL_CHECK_END
51
 
43
 

+ 5
- 5
tests/ttail_init_norm_dates.c View File

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

Loading…
Cancel
Save