123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /*
- * Copyright 2017 Yann Weber
- *
- * This file is part of Ttail.
- *
- * Ttail is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * Ttail is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Ttail. If not, see <http://www.gnu.org/licenses/>.
- */
- #include "ttail_search.h"
-
- int ttail_search_init_session(ttail_t* ttail)
- {
- if(ttail->session != NULL)
- {
- fprintf(stderr, "A session is allready started\n");
- return -1;
- }
- return ttail->logfile_sz?\
- ttail_search_files_init(ttail):\
- ttail_search_std_init(ttail);
- }
-
- int ttail_search_closest(ttail_t* ttail)
- {
- int ret;
- if(ttail->session == NULL)
- {
- fprintf(stderr, "No session started yet\n");
- return -1;
- }
- ret = ttail->logfile_sz?\
- _ttail_search_closest_files(ttail):\
- _ttail_search_closest_stdin(ttail);
- if(ret < 0)
- {
- return -1;
- }
- if(ret > 0)
- {
- return ret;
- }
- return 0;
- }
-
- void ttail_search_print_res(ttail_t* t)
- {
- if(t->logfile_sz)
- {
- _ttail_search_print_files(t, 1);
- _ttail_search_file_free(t);
- }
- else
- {
- _ttail_search_print_stdin(t, 1);
- _ttail_search_stdin_free(t);
- }
- }
-
- int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
- {
- const char *subst, *ret;
-
- ttail_tm_init(tm);
-
- if(ttail->flag & TTAIL_FLAG_PREFIX)
- {
- 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)
- {
- memset(tm, 0,sizeof(struct tm));
- return 2;
- }
- return 0;
- }
-
- const char* ttail_logline_subst(ttail_t* t, const char* logline)
- {
- regmatch_t pmatch[1];
- size_t nmatch;
- int ret;
- char err[1024];
-
- if(!t->prefix_sz)
- {
- return logline;
- }
- else if(t->prefix_sz > 0)
- {
- /* constant subst */
- return strlen(logline) < t->prefix_sz ? \
- NULL:logline + t->prefix_sz;
- }
- /* regex subst */
- nmatch=1;
- ret = regexec(&(t->date_prefix), logline, nmatch, pmatch, 0);
- if(ret)
- {
- regerror(ret, &(t->date_prefix), err,1024);
- fprintf(stderr, "RegEx exec error : %s\n", err);
- return NULL;
- }
- return logline + pmatch[0].rm_eo;
- }
-
- int ttail_tm_cmp(const struct tm *ta, const struct tm *tb)
- {
- int r;
- r=0;
- if(ta->tm_year >= 0 && tb->tm_year >= 0)
- {
- r = ta->tm_year - tb->tm_year;
- if(r) { return r; }
- }
- if(ta->tm_mon >= 0 && tb->tm_mon >= 0)
- {
- r = ta->tm_mon - tb->tm_mon;
- if(r) { return r; }
- }
- if(ta->tm_mday >= 0 && tb->tm_mday >= 0)
- {
- r = ta->tm_mday - tb->tm_mday;
- if(r) { return r; }
- }
- if(ta->tm_hour >= 0 && tb->tm_hour >= 0)
- {
- r = ta->tm_hour - tb->tm_hour;
- if(r) { return r; }
- }
- if(ta->tm_min >= 0 && tb->tm_min >= 0)
- {
- r = ta->tm_min - tb->tm_min;
- if(r) { return r; }
- }
- if(ta->tm_sec >= 0 && tb->tm_sec >= 0)
- {
- r = ta->tm_sec - tb->tm_sec;
- }
- return r;
- }
-
- void ttail_tm_init(struct tm* tm)
- {
- size_t i;
- int *ptr;
- ptr = (int*)tm;
- for(i=0; i<sizeof(struct tm)/sizeof(int); i++)
- {
- *ptr = -1;
- ptr++;
- }
- }
-
- void ttail_tm_print(const struct tm* tm)
- {
- printf("year:%d month:%d mday:%d hour:%d min:%d sec:%d",
- tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
- tm->tm_sec);
- }
-
- void ttail_tm_fprint(FILE *stream, const struct tm* tm)
- {
- if(tm->tm_year >= 0)
- {
- fprintf(stream, "year : %d ", tm->tm_year + 1900);
- }
- if(tm->tm_mon >= 0)
- {
- fprintf(stream, "month : %d ", tm->tm_mon);
- }
- if(tm->tm_mday >= 0)
- {
- fprintf(stream, "day : %d ", tm->tm_mday);
- }
- if(tm->tm_hour >= 0)
- {
- fprintf(stream, "hour : %d ", tm->tm_hour);
- }
- if(tm->tm_min >= 0)
- {
- fprintf(stream, "minute : %d ", tm->tm_min);
- }
- if(tm->tm_sec >= 0)
- {
- fprintf(stream, "secs : %d ", tm->tm_sec);
- }
- }
|