Browse Source

Fix #4 Relative dates brakes format detection

Date detection on stdin enabled
Yann Weber 6 years ago
parent
commit
3b330e114b
2 changed files with 62 additions and 13 deletions
  1. 7
    7
      src/include/ttail_search_std.h
  2. 55
    6
      src/ttail_search_std.c

+ 7
- 7
src/include/ttail_search_std.h View File

@@ -56,14 +56,14 @@ struct _ttail_search_stdin_s
56 56
  */
57 57
 int ttail_search_std_init(ttail_t* t);
58 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
59
+/**@brief Date format initialisation using logfiles
60
+ *
61
+ *If no format set yet attempt to guess it from logfiles
62
+ *@param ttail_t*
63
+ *@return 0 on success -1 on failure 1 if format was allready set
64
+ *@see _ttail_search_files_fmt_guess()
65 65
  */
66
-int _ttail_search_fmtdetect_provider_std(ttail_t*, int*, size_t*, char***);
66
+int _ttail_search_std_fmt_init(ttail_t*);
67 67
 
68 68
 /**@brief @ref ttail_search_closest() implementation for stdin
69 69
  *@warning Expect that ttail_search_closest_std_init() has been called

+ 55
- 6
src/ttail_search_std.c View File

@@ -28,13 +28,58 @@ 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
+	if(_ttail_search_std_fmt_init(t) < 0)
32
+	{
33
+		return -1;
34
+	}
31 35
 	return 0;
32 36
 }
33 37
 
34
-int _ttail_search_fmtdetect_provider_std(ttail_t *t, int *try_left, 
35
-		size_t* line_sz, char*** lines_ptr)
38
+int _ttail_search_std_fmt_init(ttail_t* t)
36 39
 {
37
-	return 1;
40
+	ssize_t rd_sz;
41
+	const char *buff;
42
+	int fmt_id, max_lines, i;
43
+	const char *fmt[] = TTAIL_DEFAULT_FORMATS;
44
+
45
+	if(t->flag & TTAIL_FLAG_FORMAT)
46
+	{
47
+		return 1;
48
+	}
49
+
50
+	max_lines = 10;
51
+	
52
+	for(i=0; i<max_lines; i++)
53
+	{
54
+		if((rd_sz = ttail_std_getline(t)) < 0)
55
+		{
56
+			fprintf(stderr, "Unable to detect date from stdin\n");
57
+			return -1;
58
+		}
59
+
60
+		buff = ttail_logline_subst(t, ttail_std_getline_buff(t));
61
+		fmt_id = ttail_format_guess(buff, NULL);
62
+		if(fmt_id >= 0)
63
+		{
64
+			break;
65
+		}
66
+	}
67
+	if(fmt_id < 0)
68
+	{
69
+		fprintf(stderr, "Unable to detect date format from stdin\
70
+after %d lines were readed\n", max_lines);
71
+		return -1;
72
+	}
73
+	buff = fmt[fmt_id];
74
+	t->fmt = malloc(sizeof(char) *  (strlen(buff)+1));
75
+	if(!t->fmt)
76
+	{
77
+		perror("Unable to allocate memory for date format");
78
+		return -1;
79
+	}
80
+	strcpy(t->fmt, buff);
81
+	t->flag |= TTAIL_FLAG_FORMAT;
82
+	return 0;
38 83
 }
39 84
 
40 85
 int _ttail_search_closest_stdin(ttail_t* t)
@@ -48,16 +93,20 @@ int _ttail_search_closest_stdin(ttail_t* t)
48 93
 	}
49 94
 	if(! t->session->std.buff)
50 95
 	{
51
-		rd_sz = ttail_std_getline(t);
96
+		if((rd_sz = ttail_std_getline(t)) < 0)
97
+		{
98
+			perror("Unable to read stdin");
99
+			return -1;
100
+		}
52 101
 	}
53 102
 	else
54 103
 	{
104
+		/*first line allready in the buffer thank's to format
105
+		 *detection*/
55 106
 		rd_sz = strlen(ttail_std_getline_buff(t)) + 1;
56 107
 	}
57 108
 	while(rd_sz > 0)
58 109
 	{
59
-		/*first line allready in the buffer thank's to format
60
-		 *detection*/
61 110
 		ttail_tm_init(&tm);
62 111
 		ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
63 112
 		if(ret < 0)

Loading…
Cancel
Save