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 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "ttail_search.h"
  2. int ttail_search_closest(ttail_t* ttail)
  3. {
  4. int ret;
  5. if(ttail->session != NULL)
  6. {
  7. fprintf(stderr, "A session is allready started\n");
  8. return -1;
  9. }
  10. ret = ttail->logfile_sz?\
  11. _ttail_search_closest_files(ttail):\
  12. _ttail_search_closest_stdin(ttail);
  13. if(ret < 0)
  14. {
  15. return -1;
  16. }
  17. if(ret > 0)
  18. {
  19. return ret;
  20. }
  21. return 0;
  22. }
  23. void ttail_search_print_res(ttail_t* t)
  24. {
  25. if(t->logfile_sz)
  26. {
  27. _ttail_search_print_files(t, 1);
  28. _ttail_search_file_free(t);
  29. }
  30. else
  31. {
  32. _ttail_search_print_stdin(t, 1);
  33. }
  34. }
  35. int _ttail_search_closest_stdin(ttail_t* t)
  36. {
  37. return 0;
  38. }
  39. void _ttail_search_print_stdin(ttail_t* t, int fd)
  40. {
  41. return;
  42. }
  43. int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
  44. {
  45. const char *subst, *ret;
  46. ttail_tm_init(tm);
  47. if(ttail->flag & TTAIL_FLAG_PREFIX)
  48. {
  49. subst = ttail_logline_subst(ttail, logline);
  50. if(!subst)
  51. {
  52. memset(tm, 0,sizeof(struct tm));
  53. return 1;
  54. }
  55. }
  56. else
  57. {
  58. subst = logline;
  59. }
  60. ret = strptime(subst, ttail->fmt, tm);
  61. if(!ret)
  62. {
  63. memset(tm, 0,sizeof(struct tm));
  64. return 2;
  65. }
  66. return 0;
  67. }
  68. const char* ttail_logline_subst(ttail_t* t, const char* logline)
  69. {
  70. regmatch_t pmatch[1];
  71. size_t nmatch;
  72. int ret;
  73. char err[1024];
  74. if(!t->prefix_sz)
  75. {
  76. return logline;
  77. }
  78. else if(t->prefix_sz > 0)
  79. {
  80. /* constant subst */
  81. return strlen(logline) < t->prefix_sz ? \
  82. NULL:logline + t->prefix_sz;
  83. }
  84. /* regex subst */
  85. nmatch=1;
  86. ret = regexec(&(t->date_prefix), logline, nmatch, pmatch, 0);\
  87. if(ret)
  88. {
  89. regerror(ret, &(t->date_prefix), err,1024);
  90. fprintf(stderr, "Exec error : %s\n", err);
  91. return NULL;
  92. }
  93. return logline + pmatch[0].rm_eo;
  94. }
  95. int ttail_tm_cmp(const struct tm *ta, const struct tm *tb)
  96. {
  97. int r;
  98. r=0;
  99. if(ta->tm_year >= 0 && ta->tm_year >= 0)
  100. {
  101. r = ta->tm_year - tb->tm_year;
  102. if(r) { return r; }
  103. }
  104. if(ta->tm_mon >= 0 && ta->tm_mon >= 0)
  105. {
  106. r = ta->tm_mon - tb->tm_mon;
  107. if(r) { return r; }
  108. }
  109. if(ta->tm_mday >= 0 && ta->tm_mday >= 0)
  110. {
  111. r = ta->tm_mday - tb->tm_mday;
  112. if(r) { return r; }
  113. }
  114. if(ta->tm_hour >= 0 && ta->tm_hour >= 0)
  115. {
  116. r = ta->tm_hour - tb->tm_hour;
  117. if(r) { return r; }
  118. }
  119. if(ta->tm_min >= 0 && ta->tm_min >= 0)
  120. {
  121. r = ta->tm_min - tb->tm_min;
  122. if(r) { return r; }
  123. }
  124. if(ta->tm_sec >= 0 && ta->tm_sec >= 0)
  125. {
  126. r = ta->tm_sec - tb->tm_sec;
  127. }
  128. return r;
  129. }
  130. void ttail_tm_init(struct tm* tm)
  131. {
  132. size_t i;
  133. int *ptr;
  134. ptr = (int*)tm;
  135. for(i=0; i<sizeof(struct tm)/sizeof(int); i++)
  136. {
  137. *ptr = -1;
  138. ptr++;
  139. }
  140. }
  141. void ttail_tm_print(const struct tm* tm)
  142. {
  143. printf("year:%d month:%d mday:%d hour:%d min:%d sec:%d",
  144. tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min,
  145. tm->tm_sec);
  146. }