timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ttail_search.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2017 Yann Weber
  3. *
  4. * This file is part of Ttail.
  5. *
  6. * Ttail is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * Ttail is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ttail. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "ttail_search.h"
  20. int ttail_search_init_session(ttail_t* ttail)
  21. {
  22. if(ttail->session != NULL)
  23. {
  24. fprintf(stderr, "A session is allready started\n");
  25. return -1;
  26. }
  27. return ttail->logfile_sz?\
  28. ttail_search_files_init(ttail):\
  29. ttail_search_std_init(ttail);
  30. }
  31. int ttail_search_closest(ttail_t* ttail)
  32. {
  33. int ret;
  34. if(ttail->session == NULL)
  35. {
  36. fprintf(stderr, "No session started yet\n");
  37. return -1;
  38. }
  39. ret = ttail->logfile_sz?\
  40. _ttail_search_closest_files(ttail):\
  41. _ttail_search_closest_stdin(ttail);
  42. if(ret < 0)
  43. {
  44. return -1;
  45. }
  46. if(ret > 0)
  47. {
  48. return ret;
  49. }
  50. return 0;
  51. }
  52. void ttail_search_print_res(ttail_t* t)
  53. {
  54. if(t->logfile_sz)
  55. {
  56. _ttail_search_print_files(t, 1);
  57. _ttail_search_file_free(t);
  58. }
  59. else
  60. {
  61. _ttail_search_print_stdin(t, 1);
  62. _ttail_search_stdin_free(t);
  63. }
  64. }
  65. int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
  66. {
  67. const char *subst, *ret;
  68. ttail_tm_init(tm);
  69. if(ttail->flag & TTAIL_FLAG_PREFIX)
  70. {
  71. subst = ttail_logline_subst(ttail, logline);
  72. if(!subst)
  73. {
  74. memset(tm, 0,sizeof(struct tm));
  75. return 1;
  76. }
  77. }
  78. else
  79. {
  80. subst = logline;
  81. }
  82. ret = strptime(subst, ttail->fmt, tm);
  83. if(!ret)
  84. {
  85. memset(tm, 0,sizeof(struct tm));
  86. return 2;
  87. }
  88. return 0;
  89. }
  90. const char* ttail_logline_subst(ttail_t* t, const char* logline)
  91. {
  92. regmatch_t pmatch[1];
  93. size_t nmatch;
  94. int ret;
  95. char err[1024];
  96. if(!t->prefix_sz)
  97. {
  98. return logline;
  99. }
  100. else if(t->prefix_sz > 0)
  101. {
  102. /* constant subst */
  103. return strlen(logline) < t->prefix_sz ? \
  104. NULL:logline + t->prefix_sz;
  105. }
  106. /* regex subst */
  107. nmatch=1;
  108. ret = regexec(&(t->date_prefix), logline, nmatch, pmatch, 0);\
  109. if(ret)
  110. {
  111. regerror(ret, &(t->date_prefix), err,1024);
  112. fprintf(stderr, "Exec error : %s\n", err);
  113. return NULL;
  114. }
  115. return logline + pmatch[0].rm_eo;
  116. }
  117. int ttail_tm_cmp(const struct tm *ta, const struct tm *tb)
  118. {
  119. int r;
  120. r=0;
  121. if(ta->tm_year >= 0 && tb->tm_year >= 0)
  122. {
  123. r = ta->tm_year - tb->tm_year;
  124. if(r) { return r; }
  125. }
  126. if(ta->tm_mon >= 0 && tb->tm_mon >= 0)
  127. {
  128. r = ta->tm_mon - tb->tm_mon;
  129. if(r) { return r; }
  130. }
  131. if(ta->tm_mday >= 0 && tb->tm_mday >= 0)
  132. {
  133. r = ta->tm_mday - tb->tm_mday;
  134. if(r) { return r; }
  135. }
  136. if(ta->tm_hour >= 0 && tb->tm_hour >= 0)
  137. {
  138. r = ta->tm_hour - tb->tm_hour;
  139. if(r) { return r; }
  140. }
  141. if(ta->tm_min >= 0 && tb->tm_min >= 0)
  142. {
  143. r = ta->tm_min - tb->tm_min;
  144. if(r) { return r; }
  145. }
  146. if(ta->tm_sec >= 0 && tb->tm_sec >= 0)
  147. {
  148. r = ta->tm_sec - tb->tm_sec;
  149. }
  150. return r;
  151. }
  152. void ttail_tm_init(struct tm* tm)
  153. {
  154. size_t i;
  155. int *ptr;
  156. ptr = (int*)tm;
  157. for(i=0; i<sizeof(struct tm)/sizeof(int); i++)
  158. {
  159. *ptr = -1;
  160. ptr++;
  161. }
  162. }
  163. void ttail_tm_print(const struct tm* tm)
  164. {
  165. printf("year:%d month:%d mday:%d hour:%d min:%d sec:%d",
  166. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
  167. tm->tm_sec);
  168. }