Browse Source

Implements logline subst + tests

Yann Weber 7 years ago
parent
commit
5de36b3ecc
4 changed files with 104 additions and 8 deletions
  1. 1
    1
      src/include/ttail_search.h
  2. 1
    1
      src/ttail_init.c
  3. 22
    2
      src/ttail_search.c
  4. 80
    4
      tests/ttail_search_check.c

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

@@ -81,7 +81,7 @@ int ttail_logline2date(ttail_t*, const char*, struct tm*);
81 81
 /**@brief Apply the choosen substitution to the logline
82 82
  *@param ttail_t* ttail
83 83
  *@param const char* logline
84
- *@return a pointer on the end of the subst string
84
+ *@return a pointer on the end of the subst string or NULL if subst fails
85 85
  */
86 86
 const char* ttail_logline_subst(ttail_t*, const char*);
87 87
 

+ 1
- 1
src/ttail_init.c View File

@@ -252,7 +252,7 @@ int ttail_set_prefix(ttail_t* res, const char* regex)
252 252
 	{
253 253
 		cflags |= REG_ICASE;
254 254
 	}
255
-	ret = regcomp(&res->date_prefix, regex, 0);
255
+	ret = regcomp(&res->date_prefix, regex, cflags);
256 256
 	if(!ret)
257 257
 	{
258 258
 		return 0;

+ 22
- 2
src/ttail_search.c View File

@@ -32,7 +32,27 @@ int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
32 32
 	return 0;
33 33
 }
34 34
 
35
-const char* ttail_logline_subst(ttail_t* ttail, const char* logline)
35
+const char* ttail_logline_subst(ttail_t* t, const char* logline)
36 36
 {
37
-	return logline;
37
+	regmatch_t pmatch[1];
38
+	size_t nmatch;
39
+	int ret;
40
+	char err[1024];
41
+
42
+	if(t->prefix_sz >= 0)
43
+	{
44
+		/* constant subst */
45
+		return strlen(logline) < t->prefix_sz ? \
46
+			NULL:logline + t->prefix_sz;
47
+	}
48
+	/* regex subst */
49
+	nmatch=1;
50
+	ret = regexec(&(t->date_prefix), logline, nmatch, pmatch, 0);\
51
+	if(ret)
52
+	{
53
+		regerror(ret, &(t->date_prefix), err,1024);
54
+		fprintf(stderr, "Exec error : %s\n", err);
55
+		return NULL;
56
+	}
57
+	return logline + pmatch[0].rm_eo;
38 58
 }

+ 80
- 4
tests/ttail_search_check.c View File

@@ -168,13 +168,80 @@ START_TEST (test_file_minmax1)
168 168
 }
169 169
 END_TEST
170 170
 
171
-Suite * ttail_search_suite(void)
171
+START_TEST (test_search_subst_const1)
172
+{
173
+	char expl[] = "Hello world !";
174
+	const char *res;
175
+	ttail->flag |= TTAIL_FLAG_PREFIX;
176
+	ttail->prefix_sz = 4;
177
+	res = ttail_logline_subst(ttail, expl);
178
+	ck_assert(res != NULL);
179
+	ck_assert(res == expl+4);
180
+}
181
+END_TEST
182
+
183
+START_TEST (test_search_subst_const2)
184
+{
185
+	char expl[] = "Hello world !";
186
+	const char *res;
187
+	ttail->flag |= TTAIL_FLAG_PREFIX;
188
+	ttail->prefix_sz = 0;
189
+	res = ttail_logline_subst(ttail, expl);
190
+	ck_assert(res != NULL);
191
+	ck_assert(res == expl);
192
+}
193
+END_TEST
194
+
195
+START_TEST (test_search_subst_re1)
196
+{
197
+	char expl[] = "1337 Foo Bar - Hello world !";
198
+	char re[] = "^1337 Fo* Bar - ";
199
+	const char *res;
200
+	int ret;
201
+	ret = ttail_set_prefix(ttail, re);
202
+	ck_assert_int_eq(ret,0);
203
+	res = ttail_logline_subst(ttail, expl);
204
+	ck_assert(res != NULL);
205
+	ck_assert_str_eq(res, "Hello world !");
206
+}
207
+END_TEST
208
+
209
+START_TEST (test_search_subst_re2)
210
+{
211
+	char expl[] = "1337 Foo Bar - Hello world !";
212
+	char re[] = "^[0-9]+ Fo{2} Bar - ";
213
+	const char *res;
214
+	int ret;
215
+	ttail->flag |= TTAIL_FLAG_EXTENDED_RE;
216
+	ret = ttail_set_prefix(ttail, re);
217
+	ck_assert_int_eq(ret,0);
218
+	res = ttail_logline_subst(ttail, expl);
219
+	ck_assert(res != NULL);
220
+	ck_assert_str_eq(res, "Hello world !");
221
+}
222
+END_TEST
223
+
224
+START_TEST (test_search_subst_re_nomatch)
225
+{
226
+	char expl[] = "1337 Foo Bar - Hello world !";
227
+	char re[] = "Never match m*";
228
+	const char *res;
229
+	int ret;
230
+	ret = ttail_set_prefix(ttail, re);
231
+	ck_assert_int_eq(ret,0);
232
+	res = ttail_logline_subst(ttail, expl);
233
+	ck_assert(res == NULL);
234
+}
235
+END_TEST
236
+
237
+Suite * ttail_search_files_suite(void)
172 238
 {
173 239
 	Suite *s;
240
+	TCase *tc_search_subst;
174 241
 	TCase *tc_search_closest_fileinit, *tc_file_line,
175 242
 		*tc_file_minmax;
176 243
 
177
-	s = suite_create("ttail search checks");
244
+	s = suite_create("ttail search_files checks");
178 245
 	tc_search_closest_fileinit = tcase_create("\
179 246
 ttail_logline_closest_files_init() checks");
180 247
 	tcase_add_checked_fixture(tc_search_closest_fileinit,
@@ -196,6 +263,16 @@ ttail_logline_closest_files_init() checks");
196 263
 		setup_closest_fileinit, teardown_closest_fileinit);
197 264
 	tcase_add_test(tc_file_minmax, test_file_minmax1);
198 265
 
266
+	tc_search_subst = tcase_create("ttail_logline_subst() checks");
267
+	tcase_add_checked_fixture(tc_search_subst,
268
+		setup_closest_fileinit, teardown_closest_fileinit);
269
+	tcase_add_test(tc_search_subst, test_search_subst_const1);
270
+	tcase_add_test(tc_search_subst, test_search_subst_const2);
271
+	tcase_add_test(tc_search_subst, test_search_subst_re1);
272
+	tcase_add_test(tc_search_subst, test_search_subst_re2);
273
+	tcase_add_test(tc_search_subst, test_search_subst_re_nomatch);
274
+
275
+	suite_add_tcase(s, tc_search_subst);
199 276
 	suite_add_tcase(s, tc_search_closest_fileinit);
200 277
 	suite_add_tcase(s, tc_file_line);
201 278
 	suite_add_tcase(s, tc_file_minmax);
@@ -209,9 +286,8 @@ int main(int argc, char **argv)
209 286
 
210 287
 	chdir(dirname(argv[0])); /* move in ./tests dir */
211 288
 
212
-	sr = srunner_create(ttail_search_suite());
289
+	sr = srunner_create(ttail_search_files_suite());
213 290
 	srunner_set_fork_status(sr, CK_FORK);
214
-
215 291
 	srunner_run_all(sr,CK_VERBOSE);
216 292
 	number_failed = srunner_ntests_failed(sr);
217 293
 	srunner_free(sr);

Loading…
Cancel
Save