diff --git a/src/include/ttail.h b/src/include/ttail.h index 590919e..77f0665 100644 --- a/src/include/ttail.h +++ b/src/include/ttail.h @@ -21,10 +21,21 @@ typedef struct _ttail_s ttail_t; #define TTAIL_FLAG_DATE_MIN 2 #define TTAIL_FLAG_DATE_MAX 4 #define TTAIL_FLAG_FORMAT 8 -#define TTAIL_DEFAULT_FORMATS {"%m", "%A %B %d, %Y %H:%M:%S", "%A", "%B", \ -"%m/%d/%y %I %p", "%d,%m,%Y %H:%M", "at %A the %dst of %B in %Y",\ -"run job at %I %p,%B %dnd", "%A den %d. %B %Y %H.%M Uhr",\ -"%c", "%y/%m/%d", "%Y/%m/%d", "%y-%m-%d", "%Y/%m/%d:%H:%M",NULL} +#define TTAIL_DEFAULT_FORMATS {"%m",\ +"%A %B %d, %Y %H:%M:%S",\ +"%A",\ +"%B%n%d %H:%M:%S",\ +"%B%n%d %H:%M",\ +"%m/%d/%y %I %p",\ +"%d,%m,%Y %H:%M",\ +"at %A the %dst of %B in %Y",\ +"run job at %I %p,%B %dnd",\ +"%A den %d. %B %Y %H.%M Uhr",\ +"%c",\ +"%y/%m/%d",\ +"%Y/%m/%d",\ +"%y-%m-%d",\ +"%Y/%m/%d:%H:%M",NULL} struct _ttail_s { diff --git a/src/include/ttail_search_files.h b/src/include/ttail_search_files.h index 58d1724..919feab 100644 --- a/src/include/ttail_search_files.h +++ b/src/include/ttail_search_files.h @@ -71,6 +71,14 @@ int _ttail_search_file_sorted(ttail_t*); */ int _ttail_file_reopen(ttail_t*, size_t); +/**@brief Set min & max date of file + *@param ttail_t* ttail + *@param size_t id file id in ttail + *@param struct tm[2] will be set to min & max + *@return 0 on success 1 if no date found or no fp -1 if error + */ +int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]); + /**@brief Search next line * *Set f pos to next line begining and return the position diff --git a/src/ttail_search_files.c b/src/ttail_search_files.c index e781e17..c9120ae 100644 --- a/src/ttail_search_files.c +++ b/src/ttail_search_files.c @@ -112,9 +112,78 @@ int _ttail_search_closest_files_set_fsizes(ttail_t* t) int _ttail_search_file_sorted(ttail_t* t) { + /* files start & stop log tm, file start id = i*2 file stop = i*2+1 */ + struct tm *ftm; + + ftm = malloc(sizeof(struct tm)*t->logfile_sz*2); + if(!ftm) + { + perror("Unable to allocate memory"); + return -1; + } + + free(ftm); return -1; } +int _ttail_file_minmax(ttail_t* t, size_t id, struct tm tm[2]) +{ + FILE *fp; + long cur; + memset(tm, 0, sizeof(struct tm)*2); + fp = t->logfile[id]; + if(!fp) + { + return 1; + } + if(!fseek(fp, 0, SEEK_SET)) + { + perror("Unable to manipulate fp"); + return -1; + } + while(1) + { + if(ttail_getline(t, id) < 0) + { + return 1; + } + if(!ttail_logline2date(t, ttail_getline_buf(t), tm)) + { + break; + } + } + if(!fseek(fp, -1, SEEK_END)) + { + perror("Unable to manipulate fp"); + return -1; + } + while(1) + { + if((cur = _ttail_file_start_line(fp)) < 0) + { + return -1; + } + if(ttail_getline(t, id) < 0) + { + return 1; + } + if(!ttail_logline2date(t, ttail_getline_buf(t), tm+1)) + { + break; + } + if(!cur) + { + return 1; + } + if(!fseek(fp, cur-1, SEEK_SET)) + { + perror("Unable to manipulate fp"); + return -1; + } + } + return 0; +} + int _ttail_file_reopen(ttail_t* t, size_t id) { if(t->logfile[id]) diff --git a/tests/ttail_init_check.c b/tests/ttail_init_check.c index a14220a..71e3707 100644 --- a/tests/ttail_init_check.c +++ b/tests/ttail_init_check.c @@ -244,6 +244,26 @@ START_TEST (test_init_guess_datetime) } END_TEST +START_TEST (test_init_guess_datetime2) +{ + int ret; + struct tm tm; + char res[] = "%B%n%d %H:%M:%S"; + ret = ttail_format_guess(ttail, "Mar 6 00:01:39 pilgrim dhclient", &tm); + ck_assert_int_ne(ret, -1); + ck_assert_str_eq(fmt[ret], res); + ck_assert(ttail->fmt != NULL); + ck_assert_str_eq(ttail->fmt, res); + ck_assert((ttail->flag & TTAIL_FLAG_FORMAT) == TTAIL_FLAG_FORMAT); + ck_assert_int_eq(tm.tm_year, 0); + ck_assert_int_eq(tm.tm_mon, 2); + ck_assert_int_eq(tm.tm_mday, 6); + ck_assert_int_eq(tm.tm_hour, 0); + ck_assert_int_eq(tm.tm_min, 1); + ck_assert_int_eq(tm.tm_sec, 39); +} +END_TEST + START_TEST (test_init_guess_noguess) { int ret; @@ -290,7 +310,7 @@ START_TEST (test_init_guess_fmt_set) int ret; char res[] = "%Y/%m/%d"; ret = ttail_format_guess(ttail, "1988/10/22", NULL); - ck_assert_int_eq(ret, 11); + ck_assert_str_eq(fmt[ret], res); ret = ttail_format_guess(ttail, "1988/10/22", NULL); ck_assert_int_eq(ret, -2); ck_assert_str_eq(ttail->fmt, res); @@ -500,6 +520,7 @@ Suite * ttail_init_suite(void) setup_ttail_empty, teardown_ttail); tcase_add_test(tc_init_fmt_guess, test_init_guess_date); tcase_add_test(tc_init_fmt_guess, test_init_guess_datetime); + tcase_add_test(tc_init_fmt_guess, test_init_guess_datetime2); tcase_add_test(tc_init_fmt_guess, test_init_guess_noguess); tcase_add_test(tc_init_fmt_guess, test_init_guess_date_notm); tcase_add_test(tc_init_fmt_guess, test_init_guess_noguess_notm); diff --git a/tests/ttail_search_check.c b/tests/ttail_search_check.c index 139a23d..0669c3f 100644 --- a/tests/ttail_search_check.c +++ b/tests/ttail_search_check.c @@ -146,10 +146,26 @@ START_TEST (test_file_line_start) } END_TEST +START_TEST (test_file_minmax1) +{ + int r; + struct tm tm[2]; + printf("%d\n", ttail->logfile_sz); + ttail->flag |= TTAIL_FLAG_FORMAT; + //ttail->fmt = "%b%n%d %H:%M"; + r = _ttail_search_closest_files_init(ttail); + ck_assert_int_eq(r, 0); + /* + r = _ttail_file_minmax(ttail, 0, tm); + */ +} +END_TEST + Suite * ttail_search_suite(void) { Suite *s; - TCase *tc_search_closest_fileinit, *tc_file_line; + TCase *tc_search_closest_fileinit, *tc_file_line, + *tc_file_minmax; s = suite_create("ttail search checks"); tc_search_closest_fileinit = tcase_create("\ @@ -168,8 +184,14 @@ ttail_logline_closest_files_init() checks"); tcase_add_test(tc_file_line, test_file_line_next); tcase_add_test(tc_file_line, test_file_line_start); + tc_file_minmax = tcase_create("ttail_file_minmax() checks"); + tcase_add_checked_fixture(tc_file_minmax, + setup_closest_fileinit, teardown_closest_fileinit); + tcase_add_test(tc_file_minmax, test_file_minmax1); + suite_add_tcase(s, tc_search_closest_fileinit); suite_add_tcase(s, tc_file_line); + suite_add_tcase(s, tc_file_minmax); return s; }