Bugfix & function implementation

Now we have a running minimal ttail :
src/ttail -d "Mar 6 00:29" -f '%B%n%d %H:%M' -l tests/samples/1.log -l tests/samples/1.1.log
This commit is contained in:
Yann Weber 2017-03-17 17:48:40 +01:00
commit fd5d6cad18
6 changed files with 110 additions and 17 deletions

View file

@ -55,6 +55,11 @@ int ttail_search_closest(ttail_t*, const struct tm*);
*@return 0 if ok -1 if fatal error 1 if not found
*/
int _ttail_search_closest_stdin(ttail_t*, const struct tm*);
/**@brief Output result loglines to stdout
*@param ttail_t*
*@param int fd
*/
void _ttail_search_print_stdin(ttail_t*, int);
/**@brief Loglines comparison function
*@param ttail_t* ttail

View file

@ -68,6 +68,13 @@ struct _ttail_search_file_s
*@return 0 if ok -1 if fatal error 1 if not found
*/
int _ttail_search_closest_files(ttail_t*, const struct tm*);
/**@brief Output result loglines to stdout
*@param ttail_t*
*@param int fd
*/
void _ttail_search_print_files(ttail_t*, int);
int _ttail_search_closest_files_init(ttail_t*);
int _ttail_search_closest_files_set_fsizes(ttail_t*);

View file

@ -1,14 +1,22 @@
#include "include/ttail.h"
#include "include/ttail_init.h"
/**@mainpage
*
*Super options : -d -r -p -v -m
*/
int main(int argc, char **argv)
{
ttail_t *ttail;
ttail = ttail_init(argc, argv);
if(!(ttail = ttail_init(argc, argv)))
{
return 1;
}
if(ttail_init_check(ttail) < 0)
{
ttail_free(ttail);
return 1;
}
return 0;
return ttail_search_closest(ttail, &(ttail->date_min));
}

View file

@ -88,7 +88,6 @@ given\n");
break;
case 'l':
fprintf(stderr,"-l with %s\n", optarg);
ret = ttail_add_logfile(res, optarg);
if(ret < 0)
{

View file

@ -2,6 +2,7 @@
int ttail_search_closest(ttail_t* ttail, const struct tm *tm)
{
int ret;
if(ttail->session != NULL)
{
fprintf(stderr, "A session is allready started\n");
@ -16,9 +17,28 @@ int ttail_search_closest(ttail_t* ttail, const struct tm *tm)
return -1;
}
*/
return ttail->logfile_sz?\
ret = ttail->logfile_sz?\
_ttail_search_closest_files(ttail, tm):\
_ttail_search_closest_stdin(ttail, tm);
if(ret < 0)
{
fprintf(stderr, "Error will searching\n");
return -1;
}
if(ret > 0)
{
return ret;
}
if(ttail->logfile_sz)
{
_ttail_search_print_files(ttail, 1);
_ttail_search_file_free(ttail);
}
else
{
_ttail_search_print_stdin(ttail, 1);
}
return 0;
}
int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)
@ -26,17 +46,29 @@ int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)
return 0;
}
void _ttail_search_print_stdin(ttail_t* t, int fd)
{
return;
}
int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
{
const char *subst, *ret;
ttail_tm_init(tm);
subst = ttail_logline_subst(ttail, logline);
if(!subst)
if(ttail->flag & TTAIL_FLAG_PREFIX)
{
memset(tm, 0,sizeof(struct tm));
return 1;
subst = ttail_logline_subst(ttail, logline);
if(!subst)
{
memset(tm, 0,sizeof(struct tm));
return 1;
}
}
else
{
subst = logline;
}
ret = strptime(subst, ttail->fmt, tm);
if(!ret)
@ -53,8 +85,12 @@ const char* ttail_logline_subst(ttail_t* t, const char* logline)
size_t nmatch;
int ret;
char err[1024];
if(t->prefix_sz >= 0)
if(!t->prefix_sz)
{
return logline;
}
else if(t->prefix_sz > 0)
{
/* constant subst */
return strlen(logline) < t->prefix_sz ? \

View file

@ -91,6 +91,48 @@ File sorting not implemented yet\n");
return -1;
}
void _ttail_search_print_files(ttail_t* t, int out)
{
size_t i;
int fd;
char buf[8192];
int r;
for(i=t->session->file.id; i<t->logfile_sz; i++)
{
if(!t->logfile[i])
{
continue;
}
fd = fileno(t->logfile[i]);
if(i==t->session->file.id)
{
lseek(fd, t->session->file.off, SEEK_SET);
} else {
lseek(fd, 0, SEEK_SET);
}
while(1)
{
r = read(fd, buf, sizeof(buf));
if(r == -1)
{
perror("unable to read file");
return;
}
else if(r == 0)
{
break;
}
r = write(out, buf, r);
if(r == -1 || r == 0)
{
perror("Unable to write result");
return;
}
}
}
}
int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
const struct tm** ftm)
{
@ -104,7 +146,8 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
valid = 0;
if(ttail_tm_cmp(&(ftm[0][0]), in) > 0)
{
return 1;
*off = *id = 0;
return 0;
}
while(1)
{
@ -134,11 +177,6 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
}
else if(cmin > 0)
{
if(!*id)
{
/* not found */
return 1;
}
/* found at start of file */
off = 0;
return 0;