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.2KB

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