12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include "ttail_search.h"
-
- int ttail_search_closest(ttail_t* ttail, const struct tm *tm)
- {
- if(ttail->session != NULL)
- {
- fprintf(stderr, "A session is allready started\n");
- return -1;
- }
- /*
- ttail->session = (ttail_search_t*)malloc(ttail->logfile_sz?
- sizeof(ttail_search_file_t):sizeof(ttail_search_stdin_t));
- if(!ttail->session)
- {
- perror("Unable to allocate memory for search session");
- return -1;
- }
- */
- return ttail->logfile_sz?\
- _ttail_search_closest_files(ttail, tm):\
- _ttail_search_closest_stdin(ttail, tm);
- }
-
- int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)
- {
- return 0;
- }
-
- int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
- {
- const char *subst, *ret;
-
- memset(tm, 0,sizeof(struct tm));
-
- subst = ttail_logline_subst(ttail, logline);
- if(!subst)
- {
- memset(tm, 0,sizeof(struct tm));
- return 1;
- }
- 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 >= 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, "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 = ta->tm_year - tb->tm_year;
- if(r) { return r; }
- r = ta->tm_mon - tb->tm_mon;
- if(r) { return r; }
- r = ta->tm_mday - tb->tm_mday;
- if(r) { return r; }
- r = ta->tm_hour - tb->tm_hour;
- if(r) { return r; }
- r = ta->tm_min - tb->tm_min;
- if(r) { return r; }
- r = ta->tm_sec - tb->tm_sec;
- return r;
- }
|