Browse Source

Preparing autoformat detection on loglines

Yann Weber 7 years ago
parent
commit
6734c8ad36

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

@@ -58,6 +58,10 @@ typedef struct _ttail_s ttail_t;
58 58
 "%y-%m-%d",\
59 59
 "%Y/%m/%d:%H:%M",NULL}
60 60
 
61
+
62
+/**@brief Pointer on autodetect format provider */
63
+typedef int (*ttail_search_fmtdetect_provider)(ttail_t*, int*, size_t*, char***);
64
+
61 65
 struct _ttail_s
62 66
 {
63 67
 	char **logfile_name; /*!< logfiles name */
@@ -85,6 +89,10 @@ struct _ttail_s
85 89
 	int flag;
86 90
 
87 91
 	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;
88 96
 };
89 97
 
90 98
 #endif

+ 13
- 0
src/include/ttail_search.h View File

@@ -28,8 +28,11 @@
28 28
 #include <unistd.h>
29 29
 
30 30
 typedef union _ttail_search_u ttail_search_t;
31
+
31 32
 #include "ttail.h"
33
+
32 34
 typedef char* (*ttail_search_subst_f)(ttail_t*, const char*);
35
+
33 36
 /**@brief Comparison results
34 37
  *
35 38
  * -1 if a < b, 1 if a > b, 0 if a == b, -2 if exception
@@ -52,6 +55,16 @@ union _ttail_search_u
52 55
 	ttail_search_stdin_t std;
53 56
 };
54 57
 
58
+
59
+/**@brief Init ttail search session
60
+ *
61
+ *If logfiles given as argument start a files session. Else a stdin session
62
+ *is started
63
+ *@param ttail_t* t
64
+ *@return 0 on success -1 on error
65
+ */
66
+int ttail_search_init_session(ttail_t*);
67
+
55 68
 /**@brief Begin a search session placing it in a ready to print situation
56 69
  *@param ttail_t*
57 70
  *@param struct tm* tmin

+ 16
- 1
src/include/ttail_search_files.h View File

@@ -87,21 +87,36 @@ struct _ttail_search_file_s
87 87
 /*<!Accessor to getline wrapper buffer */
88 88
 #define ttail_file_getline_buf(TTAIL) (TTAIL->session->file.buf)
89 89
 
90
+/**@brief Init the ttail->session
91
+ *@note Needed for format autodetection
92
+ *@return 0 on success -1 on failure
93
+ */
94
+int ttail_search_files_init(ttail_t*);
95
+
90 96
 /**@brief @ref ttail_search_closest() implementation for logfiles
91 97
  *
98
+ *@warning Expect that ttail_search_closest_files_init() has been called
92 99
  *Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
93 100
  *@param ttail_t*
94 101
  *@return 0 if ok -1 if fatal error 1 if not found
95 102
  */
96 103
 int _ttail_search_closest_files(ttail_t*);
97 104
 
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
+
98 114
 /**@brief Output result loglines to stdout
99 115
  *@param ttail_t*
100 116
  *@param int fd
101 117
  */
102 118
 void _ttail_search_print_files(ttail_t*, int);
103 119
 
104
-int _ttail_search_closest_files_init(ttail_t*);
105 120
 int _ttail_search_closest_files_set_fsizes(ttail_t*);
106 121
 
107 122
 /**@brief Binary search of the last logline with a date < tm

+ 16
- 1
src/include/ttail_search_std.h View File

@@ -27,7 +27,11 @@ typedef struct _ttail_search_stdin_s ttail_search_stdin_t;
27 27
 /*<! Private search session for stdin */
28 28
 struct _ttail_search_stdin_s
29 29
 {
30
+	/**@brief ttail_std_getline buffer
31
+	 *@note buffer is used by format detection
32
+	 */
30 33
 	char *buff;
34
+	/**@brief buffer size */
31 35
 	size_t buff_sz;
32 36
 };
33 37
 
@@ -50,9 +54,19 @@ struct _ttail_search_stdin_s
50 54
  *@return -1 on error else 0
51 55
  *@todo checks
52 56
  */
53
-int _ttail_search_closest_std_init(ttail_t* t);
57
+int ttail_search_std_init(ttail_t* t);
58
+
59
+/**@brief Provide datas for format autodetection
60
+ *@param ttail_t* t
61
+ *@param int* : retry left
62
+ *@param size_t* : line count
63
+ *@param char*** : pointer on lines buffer
64
+ *@return -1 on error 1 on autodetection impossible 0 on success
65
+ */
66
+int _ttail_search_fmtdetect_provider_std(ttail_t*, int*, size_t*, char***);
54 67
 
55 68
 /**@brief @ref ttail_search_closest() implementation for stdin
69
+ *@warning Expect that ttail_search_closest_std_init() has been called
56 70
  *@note when returns ttail->session->std.buff contains the first
57 71
  *line to print
58 72
  *@param ttail_t*
@@ -61,6 +75,7 @@ int _ttail_search_closest_std_init(ttail_t* t);
61 75
  *@todo checks
62 76
  */
63 77
 int _ttail_search_closest_stdin(ttail_t*);
78
+
64 79
 /**@brief Output result loglines to stdout
65 80
  *@param ttail_t*
66 81
  *@param int fd

+ 6
- 0
src/main.c View File

@@ -34,6 +34,12 @@ int main(int argc, char **argv)
34 34
 		ttail_free(ttail);
35 35
 		return 1;
36 36
 	}
37
+	if(ttail_search_init_session(ttail) < 0)
38
+	{
39
+		fprintf(stderr, "Unable to initialise search session\n");
40
+		ttail_free(ttail);
41
+		return 1;
42
+	}
37 43
 	res = ttail_search_closest(ttail);
38 44
 	if(res < 0)
39 45
 	{

+ 14
- 2
src/ttail_search.c View File

@@ -18,14 +18,26 @@
18 18
  */
19 19
 #include "ttail_search.h"
20 20
 
21
-int ttail_search_closest(ttail_t* ttail)
21
+int ttail_search_init_session(ttail_t* ttail)
22 22
 {
23
-	int ret;
24 23
 	if(ttail->session != NULL)
25 24
 	{
26 25
 		fprintf(stderr, "A session is allready started\n");
27 26
 		return -1;
28 27
 	}
28
+	return ttail->logfile_sz?\
29
+		ttail_search_files_init(ttail):\
30
+		ttail_search_std_init(ttail);
31
+}
32
+
33
+int ttail_search_closest(ttail_t* ttail)
34
+{
35
+	int ret;
36
+	if(ttail->session == NULL)
37
+	{
38
+		fprintf(stderr, "No session started yet\n");
39
+		return -1;
40
+	}
29 41
 	ret = ttail->logfile_sz?\
30 42
 		_ttail_search_closest_files(ttail):\
31 43
 		_ttail_search_closest_stdin(ttail);

+ 9
- 6
src/ttail_search_files.c View File

@@ -24,11 +24,6 @@ int _ttail_search_closest_files(ttail_t* t)
24 24
 	size_t i, prev;
25 25
 	struct tm **ftm; /* t->logfile_sz len of struct tm[2] */
26 26
 	char prev_found;
27
-	ret = _ttail_search_closest_files_init(t);
28
-	if(ret)
29
-	{
30
-		return ret;
31
-	}
32 27
 	/* Storing min & max of each files in ftm and checking that files are 
33 28
 	 * sorted well */
34 29
 	ftm = malloc(sizeof(struct tm*) * t->logfile_sz);
@@ -37,6 +32,7 @@ int _ttail_search_closest_files(ttail_t* t)
37 32
 		perror("Unable to allocate memory");
38 33
 		goto _ttail_search_closest_files_alloc_err;
39 34
 	}
35
+	ret = 0; /* to avoid may be used uninitialized error */
40 36
 	prev_found = 0;
41 37
 	prev = 0;
42 38
 	for(i=0; i<t->logfile_sz; i++)
@@ -145,6 +141,12 @@ date-max\n");
145 141
 	return -1;
146 142
 }
147 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
+
148 150
 void _ttail_search_print_files(ttail_t* t, int out)
149 151
 {
150 152
 	size_t i;
@@ -436,7 +438,7 @@ to a date directly from a file\n");
436 438
 	return 0;
437 439
 }
438 440
 
439
-int _ttail_search_closest_files_init(ttail_t* t)
441
+int ttail_search_files_init(ttail_t* t)
440 442
 {
441 443
 	struct stat stat;
442 444
 	FILE *fp;
@@ -498,6 +500,7 @@ int _ttail_search_closest_files_init(ttail_t* t)
498 500
 	{
499 501
 		goto _ttail_search_closest_files_err;
500 502
 	}
503
+	t->fmtdetect_provider = _ttail_search_fmtdetect_provider_files;
501 504
 
502 505
 	return 0;
503 506
 

+ 20
- 6
src/ttail_search_std.c View File

@@ -18,7 +18,7 @@
18 18
  */
19 19
 #include "ttail_search_std.h"
20 20
 
21
-int _ttail_search_closest_std_init(ttail_t* t)
21
+int ttail_search_std_init(ttail_t* t)
22 22
 {
23 23
 	t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
24 24
 	if(!t->session)
@@ -28,24 +28,37 @@ int _ttail_search_closest_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;
31 32
 	return 0;
32 33
 }
33 34
 
35
+int _ttail_search_fmtdetect_provider_std(ttail_t *t, int *try_left, 
36
+		size_t* line_sz, char*** lines_ptr)
37
+{
38
+	return 1;
39
+}
40
+
34 41
 int _ttail_search_closest_stdin(ttail_t* t)
35 42
 {
36 43
 	struct tm tm;
37 44
 	ssize_t rd_sz;
38 45
 	int ret, tmp;
39
-	if(_ttail_search_closest_std_init(t) < 0)
40
-	{
41
-		return -1;
42
-	}
43 46
 	if(!(t->flag & TTAIL_FLAG_DATE_MIN))
44 47
 	{
45 48
 		return 0;
46 49
 	}
47
-	while((rd_sz = ttail_std_getline(t)) > 0)
50
+	if(! t->session->std.buff)
51
+	{
52
+		rd_sz = ttail_std_getline(t);
53
+	}
54
+	else
55
+	{
56
+		rd_sz = strlen(ttail_std_getline_buff(t)) + 1;
57
+	}
58
+	while(rd_sz > 0)
48 59
 	{
60
+		/*first line allready in the buffer thank's to format
61
+		 *detection*/
49 62
 		ttail_tm_init(&tm);
50 63
 		ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
51 64
 		if(ret < 0)
@@ -62,6 +75,7 @@ int _ttail_search_closest_stdin(ttail_t* t)
62 75
 			//buffer contains the first line to print
63 76
 			return 0;
64 77
 		}
78
+		rd_sz = ttail_std_getline(t);
65 79
 	}
66 80
 	if(rd_sz < 0)
67 81
 	{

+ 13
- 4
tests/ttail_search_check.c View File

@@ -59,7 +59,7 @@ START_TEST (test_search_closest_init)
59 59
 {
60 60
 	int ret;
61 61
 
62
-	ret = _ttail_search_closest_files_init(ttail);
62
+	ret = ttail_search_files_init(ttail);
63 63
 	ck_assert_int_eq(ret,0);
64 64
 	ck_assert(ttail->session->file.vpos == 0);
65 65
 	#ifdef HUGEFILE
@@ -71,7 +71,7 @@ END_TEST
71 71
 START_TEST (test_search_closest_init_filesz)
72 72
 {
73 73
 	size_t i;
74
-	_ttail_search_closest_files_init(ttail);
74
+	ttail_search_files_init(ttail);
75 75
 	for(i=0;i<6;i++)
76 76
 	{
77 77
 		ck_assert(samples_sz[i] == ttail->session->file.file_sz[i]);
@@ -85,7 +85,7 @@ START_TEST (test_search_closest_init_vfile)
85 85
 	size_t i;
86 86
 	off_t full_sz;
87 87
 
88
-	_ttail_search_closest_files_init(ttail);
88
+	ttail_search_files_init(ttail);
89 89
 
90 90
 	full_sz = 0;
91 91
 	for(i=0;i<6;i++)
@@ -157,7 +157,7 @@ START_TEST (test_file_minmax1)
157 157
 	ttail->flag |= TTAIL_FLAG_PREFIX;
158 158
 	ttail->prefix_sz = 0;
159 159
 	ttail_set_fmt(ttail, "%b%n%d %H:%M");
160
-	r = _ttail_search_closest_files_init(ttail);
160
+	r = ttail_search_files_init(ttail);
161 161
 	ck_assert_int_eq(r, 0);
162 162
 	r = _ttail_file_minmax(ttail, 0, tm);
163 163
 	ck_assert_int_eq(r, 0);
@@ -322,6 +322,9 @@ START_TEST (test_search_files1)
322 322
 	memcpy(&(ttail->date_min), &tm, sizeof(tm));
323 323
 	ttail->flag |= TTAIL_FLAG_DATE_MIN;
324 324
 
325
+	ret = ttail_search_files_init(ttail);
326
+	ck_assert_int_eq(ret,0);
327
+
325 328
 	ret = _ttail_search_closest_files(ttail);
326 329
 	ck_assert_int_eq(ret, 0);
327 330
 	ck_assert(ttail->session->file.off_min.id == 0);
@@ -353,6 +356,9 @@ START_TEST (test_search_files2)
353 356
 	memcpy(&(ttail->date_min), &tm, sizeof(tm));
354 357
 	ttail->flag |= TTAIL_FLAG_DATE_MIN;
355 358
 	
359
+	ret = ttail_search_files_init(ttail);
360
+	ck_assert_int_eq(ret,0);
361
+
356 362
 	ret = _ttail_search_closest_files(ttail);
357 363
 	ck_assert_int_eq(ret, 0);
358 364
 	ck_assert(ttail->session->file.off_min.id == 0);
@@ -384,6 +390,9 @@ START_TEST (test_search_files3)
384 390
 	memcpy(&(ttail->date_min), &tm, sizeof(tm));
385 391
 	ttail->flag |= TTAIL_FLAG_DATE_MIN;
386 392
 
393
+	ret = ttail_search_files_init(ttail);
394
+	ck_assert_int_eq(ret,0);
395
+
387 396
 	ret = _ttail_search_closest_files(ttail);
388 397
 	ck_assert_int_eq(ret, 0);
389 398
 	ck_assert_int_eq(ttail->session->file.off_min.id, 5);

Loading…
Cancel
Save