Add another prefix subst method + tests
Now we can specify a constant length to cut
This commit is contained in:
parent
b0b9e0a77e
commit
ce41eddb7a
4 changed files with 62 additions and 9 deletions
|
|
@ -21,6 +21,7 @@ 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",\
|
||||
|
|
@ -45,6 +46,9 @@ struct _ttail_s
|
|||
|
||||
/*! A regex matching the datetime prefix in loglines */
|
||||
regex_t date_prefix;
|
||||
/*! An alternative to regex is to give a constant number of char
|
||||
to delete */
|
||||
int prefix_sz;
|
||||
|
||||
/*! A strptime format matching datetime in logfile */
|
||||
char *fmt;
|
||||
|
|
|
|||
|
|
@ -16,14 +16,15 @@
|
|||
|
||||
#define TTAIL_LONG_OPT {\
|
||||
{"verbose", no_argument, 0, 'v'},\
|
||||
{"re-prefix", required_argument, 0, 'p'},\
|
||||
{"re-prefix", required_argument, 0, 'r'},\
|
||||
{"prefix-len", required_argument, 0, 'p'},\
|
||||
{"date-format", required_argument, 0, 'f'},\
|
||||
{"date-min", required_argument, 0, 'd'},\
|
||||
{"date-max", required_argument, 0, 'm'},\
|
||||
{"logfile", required_argument, 0, 'l'},\
|
||||
{0, 0, 0, 0 }\
|
||||
}
|
||||
#define TTAIL_SHORT_OPT "p:f:d:l:m:"
|
||||
#define TTAIL_SHORT_OPT "r:p:f:d:l:m:"
|
||||
|
||||
/**@brief Parse cli arguments and return a ttail_t
|
||||
*@param int argc
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ ttail_t *ttail_init(int argc, char **argv)
|
|||
res->logfile = NULL;
|
||||
res->logfile_name = NULL;
|
||||
res->logfile_sz = 0;
|
||||
res->prefix_sz = -1;
|
||||
memset(&(res->date_min), 0, sizeof(struct tm));
|
||||
memset(&(res->date_max), 0, sizeof(struct tm));
|
||||
|
||||
|
|
@ -38,12 +39,27 @@ ttail_t *ttail_init(int argc, char **argv)
|
|||
case 'v':
|
||||
res->verbose++;
|
||||
break;
|
||||
case 'p':
|
||||
case 'r':
|
||||
if(ttail_set_prefix(res, optarg))
|
||||
{
|
||||
goto ttail_init_err;
|
||||
}
|
||||
break;
|
||||
case 'p':
|
||||
if(res->flag & TTAIL_FLAG_PREFIX)
|
||||
{
|
||||
fprintf(stderr, "Preffix allready \
|
||||
set\n");
|
||||
goto ttail_init_err;
|
||||
}
|
||||
res->flag |= TTAIL_FLAG_PREFIX;
|
||||
res->prefix_sz = atoi(optarg);
|
||||
if(res->prefix_sz <= 0)
|
||||
{
|
||||
fprintf(stderr, "Prefix size have \
|
||||
to be > 0");
|
||||
goto ttail_init_err;
|
||||
}
|
||||
case 'f':
|
||||
if(res->flag & TTAIL_FLAG_FORMAT)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -60,6 +60,12 @@ START_TEST (test_argparse_empty_verbose)
|
|||
"ttail_t.verbose should be 0");
|
||||
}
|
||||
END_TEST
|
||||
START_TEST (test_argparse_empty_prefixsz)
|
||||
{
|
||||
ck_assert_msg(ttail->prefix_sz == -1,
|
||||
"ttail_t.prefix_sz should be NULL");
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/*
|
||||
* Bad arguments
|
||||
|
|
@ -255,17 +261,17 @@ START_TEST (test_argparse_file_multiple)
|
|||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_argparse_prefix_short)
|
||||
START_TEST (test_argparse_reprefix_short)
|
||||
{
|
||||
ttail_t *t;
|
||||
char *args[] = {"foo", "-p", "^[^ ]+ "};
|
||||
char *args[] = {"foo", "-r", "^[^ ]+ "};
|
||||
t = ttail_init(3, args);
|
||||
ck_assert(t != NULL);
|
||||
ck_assert((t->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_argparse_prefix_long)
|
||||
START_TEST (test_argparse_reprefix_long)
|
||||
{
|
||||
ttail_t *t;
|
||||
char *args[] = {"foo", "--re-prefix", "^[^ ]+ "};
|
||||
|
|
@ -275,10 +281,33 @@ START_TEST (test_argparse_prefix_long)
|
|||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_argparse_prefixlen_short)
|
||||
{
|
||||
ttail_t *t;
|
||||
char *args[] = {"foo", "-p", "10"};
|
||||
t = ttail_init(3, args);
|
||||
ck_assert(t != NULL);
|
||||
ck_assert((t->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
|
||||
ck_assert_int_eq(t->prefix_sz, 10);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST (test_argparse_prefixlen_long)
|
||||
{
|
||||
ttail_t *t;
|
||||
char *args[] = {"foo", "--prefix-len", "42"};
|
||||
t = ttail_init(3, args);
|
||||
ck_assert(t != NULL);
|
||||
ck_assert((t->flag & TTAIL_FLAG_PREFIX) == TTAIL_FLAG_PREFIX);
|
||||
ck_assert_int_eq(t->prefix_sz, 42);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
|
||||
START_TEST (test_argparse_prefix_multiple)
|
||||
{
|
||||
ttail_t *t;
|
||||
char *args[] = {"foo", "--re-prefix", "^[^ ]+ ", "-p", "^[ -]+ "};
|
||||
char *args[] = {"foo", "--re-prefix", "^[^ ]+ ", "-p", "20"};
|
||||
t = ttail_init(5, args);
|
||||
ck_assert(t == NULL);
|
||||
}
|
||||
|
|
@ -331,6 +360,7 @@ Suite * ttail_init_suite(void)
|
|||
tcase_add_test(tc_argparse_empty, test_argparse_empty_flag);
|
||||
tcase_add_test(tc_argparse_empty, test_argparse_empty_fmt);
|
||||
tcase_add_test(tc_argparse_empty, test_argparse_empty_verbose);
|
||||
tcase_add_test(tc_argparse_empty, test_argparse_empty_prefixsz);
|
||||
|
||||
tc_argparse_badarg = tcase_create("bad arguments parsing");
|
||||
tcase_add_test(tc_argparse_badarg, test_argparse_bad1);
|
||||
|
|
@ -349,8 +379,10 @@ Suite * ttail_init_suite(void)
|
|||
tcase_add_test(tc_argparse_files, test_argparse_file_multiple);
|
||||
|
||||
tc_argparse_prefix = tcase_create("re-prefix arguments parsing");
|
||||
tcase_add_test(tc_argparse_prefix, test_argparse_prefix_short);
|
||||
tcase_add_test(tc_argparse_prefix, test_argparse_prefix_long);
|
||||
tcase_add_test(tc_argparse_prefix, test_argparse_reprefix_short);
|
||||
tcase_add_test(tc_argparse_prefix, test_argparse_reprefix_long);
|
||||
tcase_add_test(tc_argparse_prefix, test_argparse_prefixlen_short);
|
||||
tcase_add_test(tc_argparse_prefix, test_argparse_prefixlen_long);
|
||||
tcase_add_test(tc_argparse_prefix, test_argparse_prefix_multiple);
|
||||
|
||||
tc_argparse_fmt = tcase_create("date format arguments parsing");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue