Browse Source

Updated the search_closest_files function

Yann Weber 7 years ago
parent
commit
71dc7b9d3b
2 changed files with 102 additions and 2 deletions
  1. 71
    1
      src/ttail_search_files.c
  2. 31
    1
      tests/ttail_search_check.c

+ 71
- 1
src/ttail_search_files.c View File

@@ -3,12 +3,82 @@
3 3
 int _ttail_search_closest_files(ttail_t* t)
4 4
 {
5 5
 	int ret;
6
+	size_t i, prev;
7
+	struct tm **ftm; /* t->logfile_sz len of struct tm[2] */
6 8
 	ret = _ttail_search_closest_files_init(t);
7 9
 	if(ret)
8 10
 	{
9 11
 		return ret;
10 12
 	}
11
-	/* Checking that files are sorted well */
13
+	/* Storing min & max of each files in ftm and checking that files are 
14
+	 * sorted well */
15
+	ftm = malloc(sizeof(struct tm*) * t->logfile_sz);
16
+	if(!ftm)
17
+	{
18
+		perror("Unable to allocate memory");
19
+		goto _ttail_search_closest_files_alloc_err;
20
+	}
21
+	for(i=0; i<t->logfile_sz; i++)
22
+	{
23
+		ftm[i] = malloc(sizeof(struct tm)*2);
24
+		if(!ftm[i])
25
+		{
26
+			perror("Unable to allocate memory for time");
27
+			goto _ttail_search_closest_files_alloc_loop_err;
28
+		}
29
+		if(!t->logfile[i])
30
+		{
31
+			continue;
32
+		}
33
+		ret = _ttail_file_minmax(t, 0, ftm[i]);
34
+		if(ret < 0)
35
+		{
36
+			fprintf(stderr, "Minmax error\n");
37
+			goto _ttail_search_closest_files_loop_err;
38
+		}
39
+		else if (ret == 1)
40
+		{
41
+			if(t->verbose)
42
+			{
43
+				fprintf(stderr, "Warning : unable to find \
44
+a valid date in '%s'\n", t->logfile_name[i]);
45
+			}
46
+			free(ftm[i]);
47
+			ftm[i] = NULL;
48
+			fclose(t->logfile[i]);
49
+			t->logfile[i] = NULL;
50
+		}
51
+		prev = i;
52
+		if(i && prev &&
53
+			ttail_tm_cmp(&(ftm[prev][1]), &(ftm[i][0])) > 0)
54
+		{
55
+			/* files are not sorted */
56
+			fprintf(stderr, "Files do not seems to be sorted. \
57
+File sorting not implemented yet\n");
58
+			goto _ttail_search_closest_files_loop_err;
59
+		}
60
+	}
61
+	/* TODO begining binary search of date_min */
62
+
63
+	return 0;
64
+	goto _ttail_search_closest_files_err;
65
+
66
+	_ttail_search_closest_files_err:
67
+	i = t->logfile_sz;
68
+	do
69
+	{
70
+		_ttail_search_closest_files_alloc_loop_err:
71
+		i--;
72
+		_ttail_search_closest_files_loop_err:
73
+		if(ftm[i])
74
+		{
75
+			free(ftm[i]);
76
+		}
77
+	}while(i);
78
+	free(ftm);
79
+	_ttail_search_closest_files_alloc_err:
80
+	return -1;
81
+}
12 82
 
13 83
 	return 0;
14 84
 }

+ 31
- 1
tests/ttail_search_check.c View File

@@ -154,7 +154,6 @@ START_TEST (test_file_minmax1)
154 154
 	/**@todo complete the testcase */
155 155
 	int r;
156 156
 	struct tm tm[2];
157
-	printf("%ld\n", ttail->logfile_sz);
158 157
 	ttail->flag |= TTAIL_FLAG_PREFIX;
159 158
 	ttail->prefix_sz = 0;
160 159
 	ttail_set_fmt(ttail, "%b%n%d %H:%M");
@@ -302,12 +301,36 @@ START_TEST (test_search_log2date_faildate)
302 301
 }
303 302
 END_TEST
304 303
 
304
+/*
305
+ *	tests ttail_search_closest_files()
306
+ */
307
+
308
+START_TEST (test_search_files1)
309
+{
310
+	int ret;
311
+	size_t i;
312
+	for(i=1;i<ttail->logfile_sz;i++)
313
+	{
314
+		fclose(ttail->logfile[i]);
315
+		ttail->logfile[i] = NULL;
316
+	}
317
+	ttail->flag |= TTAIL_FLAG_PREFIX;
318
+	ttail->prefix_sz = 0;
319
+	ttail_set_fmt(ttail, "%B%n%d %H:%M");
320
+
321
+	ret = _ttail_search_closest_files(ttail);
322
+	ck_assert_int_eq(ret, 0);
323
+	ck_assert(ttail->session->file.id == 0);
324
+}
325
+END_TEST
326
+
305 327
 Suite * ttail_search_files_suite(void)
306 328
 {
307 329
 	Suite *s;
308 330
 	TCase *tc_search_closest_fileinit, *tc_file_line,
309 331
 		*tc_file_minmax;
310 332
 	TCase *tc_search_subst, *tc_search_logline2date;
333
+	TCase *tc_search_files;
311 334
 
312 335
 	s = suite_create("ttail search_files checks");
313 336
 	tc_search_closest_fileinit = tcase_create("\
@@ -347,11 +370,18 @@ ttail_logline_closest_files_init() checks");
347 370
 	tcase_add_test(tc_search_subst, test_search_log2date_failpref);
348 371
 	tcase_add_test(tc_search_subst, test_search_log2date_faildate);
349 372
 
373
+	tc_search_files = tcase_create("ttail_logline2date() checks");
374
+	tcase_add_checked_fixture(tc_search_files,
375
+		setup_closest_fileinit, teardown_closest_fileinit);
376
+	tcase_add_test(tc_search_files, test_search_files1);
377
+	
378
+
350 379
 	suite_add_tcase(s, tc_search_closest_fileinit);
351 380
 	suite_add_tcase(s, tc_file_line);
352 381
 	suite_add_tcase(s, tc_file_minmax);
353 382
 	suite_add_tcase(s, tc_search_subst);
354 383
 	suite_add_tcase(s, tc_search_logline2date);
384
+	suite_add_tcase(s, tc_search_files);
355 385
 	return s;
356 386
 }
357 387
 

Loading…
Cancel
Save