/* * 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 . */ #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; itm_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); } }