Fixing issue #4 for logfiles

Not done for stdin
This commit is contained in:
Yann Weber 2017-05-15 13:43:44 +02:00
commit 7dbc99e4a0
10 changed files with 114 additions and 55 deletions

View file

@ -59,9 +59,6 @@ typedef struct _ttail_s ttail_t;
"%Y/%m/%d:%H:%M",NULL}
/**@brief Pointer on autodetect format provider */
typedef int (*ttail_search_fmtdetect_provider)(ttail_t*, int*, size_t*, char***);
struct _ttail_s
{
char **logfile_name; /*!< logfiles name */
@ -89,10 +86,6 @@ struct _ttail_s
int flag;
ttail_search_t *session;
/**@brief Set by session init function to allow format autodetection
* on loglines */
ttail_search_fmtdetect_provider fmtdetect_provider;
};
#endif

View file

@ -300,7 +300,7 @@ int _ttail_set_date_relative(ttail_t*, char*, int);
*@param ttail_t* An initialized ttail_t
*@return -1 if error 0 else
*/
int _ttail_norm_dates(ttail_t*);
int ttail_norm_dates(ttail_t*);
/**@brief Attempt to guess a dateformat
*@param const char* date as a dtring

View file

@ -29,7 +29,6 @@
typedef struct _ttail_search_file_s ttail_search_file_t;
struct _ttail_files_off_s
{
size_t id;
@ -73,6 +72,7 @@ struct _ttail_search_file_s
#include "config.h"
#include "ttail.h"
#include "ttail_init.h"
#include "ttail_search.h"
/**@brief Convenient wrapper for getline
@ -93,6 +93,15 @@ struct _ttail_search_file_s
*/
int ttail_search_files_init(ttail_t*);
/**@brief Date format initialisation using logfiles
*
*If no format set yet attempt to guess it from logfiles
*@param ttail_t*
*@return 0 on success -1 on failure 1 if format was allready set
*@see _ttail_search_files_fmt_guess()
*/
int _ttail_search_files_fmt_init(ttail_t*);
/**@brief @ref ttail_search_closest() implementation for logfiles
*
*@warning Expect that ttail_search_closest_files_init() has been called
@ -102,15 +111,6 @@ int ttail_search_files_init(ttail_t*);
*/
int _ttail_search_closest_files(ttail_t*);
/**@brief Provide datas for format autodetection
*@param ttail_t* t
*@param int* : retry left
*@param size_t* : line count
*@param char*** : pointer on lines buffer
*@return -1 on error 1 on autodetection impossible 0 on success
*/
int _ttail_search_fmtdetect_provider_files(ttail_t*, int*, size_t*, char***);
/**@brief Output result loglines to stdout
*@param ttail_t*
*@param int fd

View file

@ -23,6 +23,7 @@ int main(int argc, char **argv)
{
int res;
ttail_t *ttail;
if(!(ttail = ttail_init(argc, argv)))
{
usage();
@ -40,6 +41,25 @@ int main(int argc, char **argv)
ttail_free(ttail);
return 1;
}
//Check that a format was detected
if(!(ttail->flag & TTAIL_FLAG_FORMAT))
{
fprintf(stderr, "No date format set nor detected. Abording.\n");
return -1;
}
else if(!ttail->fmt)
{
fprintf(stderr, "Date format flag set but no format found\n");
return -1;
}
if(ttail_norm_dates(ttail) < 0)
{
fprintf(stderr, "Unable to normalize dates\n");
return -1;
}
res = ttail_search_closest(ttail);
if(res < 0)
{

View file

@ -73,7 +73,7 @@ ttail_t *ttail_init(int argc, char **argv)
res->logfile = NULL;
res->logfile_name = NULL;
res->logfile_sz = 0;
res->prefix_sz = -1;
res->prefix_sz = 0;
res->session = NULL;
ttail_tm_init(&(res->date_min));
ttail_tm_init(&(res->date_max));
@ -222,16 +222,6 @@ mandatory\n");
return -1;
}
}
if(!(t->flag & TTAIL_FLAG_FORMAT))
{
fprintf(stderr, "No date format set nor detected. Abording.\n");
return -1;
}
else if(!t->fmt)
{
fprintf(stderr, "Date format flag set but no format found\n");
return -1;
}
return 0;
}
@ -324,6 +314,7 @@ int ttail_set_prefix(ttail_t* res, const char* regex)
ret = regcomp(&res->date_prefix, regex, cflags);
if(!ret)
{
res->prefix_sz = -1;
return 0;
}
/*compilation error */
@ -488,7 +479,7 @@ is not matched in '%s'\n",\
int _ttail_set_date_relative(ttail_t* res, char* date, int c)
{
time_t now;
char *unit;
char *unit, str_date[64];
int value;
struct tm *tm, *tm_p;
short mod;
@ -613,11 +604,17 @@ int _ttail_set_date_relative(ttail_t* res, char* date, int c)
fprintf(stderr,"Invalid relative date '%s'\n", date);
return -1;
}
if(res->verbose > 0)
{
strftime(str_date, 64, "%c", tm);
fprintf(stderr, "%s date set to %s\n",
c?"stop":"start", str_date);
}
return 0;
}
int _ttail_norm_dates(ttail_t* ttail)
int ttail_norm_dates(ttail_t* ttail)
{
struct tm *tm_p, tm;
/* Huge buffer but no way to make a difference between error and
@ -697,7 +694,6 @@ int ttail_format_guess(const char* date_str, struct tm* tm)
}
return -1;
}
return res;
}

View file

@ -141,12 +141,6 @@ date-max\n");
return -1;
}
int _ttail_search_fmtdetect_provider_files(ttail_t *t, int *try_left,
size_t* line_sz, char*** lines_ptr)
{
return 1;
}
void _ttail_search_print_files(ttail_t* t, int out)
{
size_t i;
@ -500,7 +494,11 @@ int ttail_search_files_init(ttail_t* t)
{
goto _ttail_search_closest_files_err;
}
t->fmtdetect_provider = _ttail_search_fmtdetect_provider_files;
if(_ttail_search_files_fmt_init(t) < 0)
{
goto _ttail_search_closest_files_err;
}
return 0;
@ -513,6 +511,67 @@ int ttail_search_files_init(ttail_t* t)
return -1;
}
int _ttail_search_files_fmt_init(ttail_t* t)
{
int fmt_id;
size_t i,j;
const char *buff;
const char *fmt[] = TTAIL_DEFAULT_FORMATS;
if(t->flag & TTAIL_FLAG_FORMAT)
{
return 1;
}
fmt_id = -1;
for(i=0; i<t->logfile_sz; i++)
{
if(!t->logfile[i]) {
continue;
}
for(j=0; j<10; j++)
{
//try to guess fmt on the 10 first lines
if(ttail_file_getline(t, i) < 0) {
break;
}
buff = ttail_logline_subst(t, ttail_file_getline_buf(t));
//buff contains the lines starting by the date
fmt_id = ttail_format_guess(buff, NULL);
if(fmt_id >= 0)
{
break;
}
}
rewind(t->logfile[i]);
if(fmt_id >= 0)
{
if(t->verbose > 0)
{
fprintf(stderr,
"Detected format %s in file %s\n",
fmt[fmt_id], t->logfile_name[i]);
}
break;
}
}
if(fmt_id >= 0)
{
buff = fmt[fmt_id];
t->fmt = malloc(sizeof(char) * (strlen(buff)+1));
if(!t->fmt)
{
perror("Unable to allocate memory for date format");
return -1;
}
strcpy(t->fmt, buff);
t->flag |= TTAIL_FLAG_FORMAT;
return 0;
}
fprintf(stderr, "Unable to detect date format from logfiles\n");
return -1;
}
int _ttail_search_closest_files_set_fsizes(ttail_t* t)
{
size_t i;

View file

@ -28,7 +28,6 @@ int ttail_search_std_init(ttail_t* t)
}
t->session->std.buff = NULL;
t->session->std.buff_sz = 0;
t->fmtdetect_provider = _ttail_search_fmtdetect_provider_std;
return 0;
}

View file

@ -48,8 +48,8 @@ START_TEST (test_argparse_empty_verbose)
END_TEST
START_TEST (test_argparse_empty_prefixsz)
{
ck_assert_msg(ttail->prefix_sz == -1,
"ttail_t.prefix_sz should be NULL");
ck_assert_msg(ttail->prefix_sz == 0,
"ttail_t.prefix_sz should be 0");
}
END_TEST
START_TEST (test_argparse_empty_session)

View file

@ -13,13 +13,6 @@ START_TEST (test_init_check_bad1)
}
END_TEST
START_TEST (test_init_check_bad2)
{
ttail->flag |= TTAIL_FLAG_DATE_MAX;
ck_assert_int_eq(ttail_init_check(ttail), -1);
}
END_TEST
START_TEST (test_init_check1)
{
char *arg[] = {"88/10/22", NULL};
@ -45,7 +38,6 @@ END_TEST
TTAIL_CHECK_START("ttail init checks", "ttail_init_check() checks")
TTAIL_SET_FIXTURE(setup_ttail_empty, teardown_ttail);
TTAIL_ADD_TEST(test_init_check_bad1);
TTAIL_ADD_TEST(test_init_check_bad2);
TTAIL_ADD_TEST(test_init_check1);
TTAIL_CHECK_END

View file

@ -19,7 +19,7 @@ START_TEST (test_norm_dates_flags_none)
ttail->date_min = tm;
ttail->date_max = tm;
ret = _ttail_norm_dates(ttail);
ret = ttail_norm_dates(ttail);
ck_assert_int_eq(ret, 0);
tm = ttail->date_min;
@ -52,7 +52,7 @@ START_TEST (test_norm_dates_flags_min)
ttail->date_min = tm;
ttail->date_max = tm;
ret = _ttail_norm_dates(ttail);
ret = ttail_norm_dates(ttail);
ck_assert_int_eq(ret, 0);
tm = ttail->date_min;
@ -85,7 +85,7 @@ START_TEST (test_norm_dates_flags_max)
ttail->date_min = tm;
ttail->date_max = tm;
ret = _ttail_norm_dates(ttail);
ret = ttail_norm_dates(ttail);
ck_assert_int_eq(ret, 0);
tm = ttail->date_min;
@ -119,7 +119,7 @@ START_TEST (test_norm_dates_flags_both)
ttail->date_min = tm;
ttail->date_max = tm;
ret = _ttail_norm_dates(ttail);
ret = ttail_norm_dates(ttail);
ck_assert_int_eq(ret, 0);
tm = ttail->date_min;
@ -153,7 +153,7 @@ START_TEST (test_norm_dates_flags_full)
ttail->date_min = tm;
ttail->date_max = tm;
ret = _ttail_norm_dates(ttail);
ret = ttail_norm_dates(ttail);
ck_assert_int_eq(ret, 0);
tm = ttail->date_min;