Browse Source

**temporary commit**

Yann Weber 7 years ago
parent
commit
89739fc1e6

+ 15
- 4
src/include/ttail.h View File

@@ -21,10 +21,21 @@ typedef struct _ttail_s ttail_t;
21 21
 #define TTAIL_FLAG_DATE_MIN 2
22 22
 #define TTAIL_FLAG_DATE_MAX 4
23 23
 #define TTAIL_FLAG_FORMAT 8
24
-#define TTAIL_DEFAULT_FORMATS {"%m", "%A %B %d, %Y %H:%M:%S", "%A", "%B", \
25
-"%m/%d/%y %I %p", "%d,%m,%Y %H:%M", "at %A the %dst of %B in %Y",\
26
-"run job at %I %p,%B %dnd", "%A den %d. %B %Y %H.%M Uhr",\
27
-"%c", "%y/%m/%d", "%Y/%m/%d", "%y-%m-%d", "%Y/%m/%d:%H:%M",NULL}
24
+#define TTAIL_DEFAULT_FORMATS {"%m",\
25
+"%A %B %d, %Y %H:%M:%S",\
26
+"%A",\
27
+"%B%n%d %H:%M:%S",\
28
+"%B%n%d %H:%M",\
29
+"%m/%d/%y %I %p",\
30
+"%d,%m,%Y %H:%M",\
31
+"at %A the %dst of %B in %Y",\
32
+"run job at %I %p,%B %dnd",\
33
+"%A den %d. %B %Y %H.%M Uhr",\
34
+"%c",\
35
+"%y/%m/%d",\
36
+"%Y/%m/%d",\
37
+"%y-%m-%d",\
38
+"%Y/%m/%d:%H:%M",NULL}
28 39
 
29 40
 struct _ttail_s
30 41
 {

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

@@ -71,6 +71,14 @@ int _ttail_search_file_sorted(ttail_t*);
71 71
  */
72 72
 int _ttail_file_reopen(ttail_t*, size_t);
73 73
 
74
+/**@brief Set min & max date of file
75
+ *@param ttail_t* ttail
76
+ *@param size_t id file id in ttail
77
+ *@param struct tm[2] will be set to min & max
78
+ *@return 0 on success 1 if no date found or no fp -1 if error
79
+ */
80
+int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
81
+
74 82
 /**@brief Search next line
75 83
  *
76 84
  *Set f pos to next line begining and return the position

+ 69
- 0
src/ttail_search_files.c View File

@@ -112,9 +112,78 @@ int _ttail_search_closest_files_set_fsizes(ttail_t* t)
112 112
 
113 113
 int _ttail_search_file_sorted(ttail_t* t)
114 114
 {
115
+	/* files start & stop log tm, file start id = i*2 file stop = i*2+1 */
116
+	struct tm *ftm; 
117
+
118
+	ftm = malloc(sizeof(struct tm)*t->logfile_sz*2);
119
+	if(!ftm)
120
+	{
121
+		perror("Unable to allocate memory");
122
+		return -1;
123
+	}
124
+
125
+	free(ftm);
115 126
 	return -1;
116 127
 }
117 128
 
129
+int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2])
130
+{
131
+	FILE *fp;
132
+	long cur;
133
+	memset(tm, 0, sizeof(struct tm)*2);
134
+	fp = t->logfile[id];
135
+	if(!fp)
136
+	{
137
+		return 1;
138
+	}
139
+	if(!fseek(fp, 0, SEEK_SET))
140
+	{
141
+		perror("Unable to manipulate fp");
142
+		return -1;
143
+	}
144
+	while(1)
145
+	{
146
+		if(ttail_getline(t, id) < 0)
147
+		{
148
+			return 1;
149
+		}
150
+		if(!ttail_logline2date(t, ttail_getline_buf(t), tm))
151
+		{
152
+			break;
153
+		}
154
+	}
155
+	if(!fseek(fp, -1, SEEK_END))
156
+	{
157
+		perror("Unable to manipulate fp");
158
+		return -1;
159
+	}
160
+	while(1)
161
+	{
162
+		if((cur = _ttail_file_start_line(fp)) < 0)
163
+		{
164
+			return -1;
165
+		}
166
+		if(ttail_getline(t, id) < 0)
167
+		{
168
+			return 1;
169
+		}
170
+		if(!ttail_logline2date(t, ttail_getline_buf(t), tm+1))
171
+		{
172
+			break;
173
+		}
174
+		if(!cur)
175
+		{
176
+			return 1;
177
+		}
178
+		if(!fseek(fp, cur-1, SEEK_SET))
179
+		{
180
+			perror("Unable to manipulate fp");
181
+			return -1;
182
+		}
183
+	}
184
+	return 0;
185
+}
186
+
118 187
 int _ttail_file_reopen(ttail_t* t, size_t id)
119 188
 {
120 189
 	if(t->logfile[id])

+ 22
- 1
tests/ttail_init_check.c View File

@@ -244,6 +244,26 @@ START_TEST (test_init_guess_datetime)
244 244
 }
245 245
 END_TEST
246 246
 
247
+START_TEST (test_init_guess_datetime2)
248
+{
249
+	int ret;
250
+	struct tm tm;
251
+	char res[] = "%B%n%d %H:%M:%S";
252
+	ret = ttail_format_guess(ttail, "Mar  6 00:01:39 pilgrim dhclient", &tm);
253
+	ck_assert_int_ne(ret, -1);
254
+	ck_assert_str_eq(fmt[ret], res);
255
+	ck_assert(ttail->fmt != NULL);
256
+	ck_assert_str_eq(ttail->fmt, res);
257
+	ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT);
258
+	ck_assert_int_eq(tm.tm_year, 0);
259
+	ck_assert_int_eq(tm.tm_mon, 2);
260
+	ck_assert_int_eq(tm.tm_mday, 6);
261
+	ck_assert_int_eq(tm.tm_hour, 0);
262
+	ck_assert_int_eq(tm.tm_min, 1);
263
+	ck_assert_int_eq(tm.tm_sec, 39);
264
+}
265
+END_TEST
266
+
247 267
 START_TEST (test_init_guess_noguess)
248 268
 {
249 269
 	int ret;
@@ -290,7 +310,7 @@ START_TEST (test_init_guess_fmt_set)
290 310
 	int ret;
291 311
 	char res[] = "%Y/%m/%d";
292 312
 	ret = ttail_format_guess(ttail, "1988/10/22", NULL);
293
-	ck_assert_int_eq(ret, 11);
313
+	ck_assert_str_eq(fmt[ret], res);
294 314
 	ret = ttail_format_guess(ttail, "1988/10/22", NULL);
295 315
 	ck_assert_int_eq(ret, -2);
296 316
 	ck_assert_str_eq(ttail->fmt, res);
@@ -500,6 +520,7 @@ Suite * ttail_init_suite(void)
500 520
 		setup_ttail_empty, teardown_ttail);
501 521
 	tcase_add_test(tc_init_fmt_guess, test_init_guess_date);
502 522
 	tcase_add_test(tc_init_fmt_guess, test_init_guess_datetime);
523
+	tcase_add_test(tc_init_fmt_guess, test_init_guess_datetime2);
503 524
 	tcase_add_test(tc_init_fmt_guess, test_init_guess_noguess);
504 525
 	tcase_add_test(tc_init_fmt_guess, test_init_guess_date_notm);
505 526
 	tcase_add_test(tc_init_fmt_guess, test_init_guess_noguess_notm);

+ 23
- 1
tests/ttail_search_check.c View File

@@ -146,10 +146,26 @@ START_TEST (test_file_line_start)
146 146
 }
147 147
 END_TEST
148 148
 
149
+START_TEST (test_file_minmax1)
150
+{
151
+	int r;
152
+	struct tm tm[2];
153
+	printf("%d\n", ttail->logfile_sz);
154
+	ttail->flag |= TTAIL_FLAG_FORMAT;
155
+	//ttail->fmt = "%b%n%d %H:%M";
156
+	r = _ttail_search_closest_files_init(ttail);
157
+	ck_assert_int_eq(r, 0);
158
+	/*
159
+	r = _ttail_file_minmax(ttail, 0, tm);
160
+	*/
161
+}
162
+END_TEST
163
+
149 164
 Suite * ttail_search_suite(void)
150 165
 {
151 166
 	Suite *s;
152
-	TCase *tc_search_closest_fileinit, *tc_file_line;
167
+	TCase *tc_search_closest_fileinit, *tc_file_line,
168
+		*tc_file_minmax;
153 169
 
154 170
 	s = suite_create("ttail search checks");
155 171
 	tc_search_closest_fileinit = tcase_create("\
@@ -168,8 +184,14 @@ ttail_logline_closest_files_init() checks");
168 184
 	tcase_add_test(tc_file_line, test_file_line_next);
169 185
 	tcase_add_test(tc_file_line, test_file_line_start);
170 186
 
187
+	tc_file_minmax = tcase_create("ttail_file_minmax() checks");
188
+	tcase_add_checked_fixture(tc_file_minmax,
189
+		setup_closest_fileinit, teardown_closest_fileinit);
190
+	tcase_add_test(tc_file_minmax, test_file_minmax1);
191
+
171 192
 	suite_add_tcase(s, tc_search_closest_fileinit);
172 193
 	suite_add_tcase(s, tc_file_line);
194
+	suite_add_tcase(s, tc_file_minmax);
173 195
 	return s;
174 196
 }
175 197
 

Loading…
Cancel
Save