timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ttail_search.c 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "ttail_search.h"
  2. int ttail_search_closest(ttail_t* ttail, const struct tm *tm)
  3. {
  4. if(ttail->session != NULL)
  5. {
  6. fprintf(stderr, "A session is allready started\n");
  7. return -1;
  8. }
  9. /*
  10. ttail->session = (ttail_search_t*)malloc(ttail->logfile_sz?
  11. sizeof(ttail_search_file_t):sizeof(ttail_search_stdin_t));
  12. if(!ttail->session)
  13. {
  14. perror("Unable to allocate memory for search session");
  15. return -1;
  16. }
  17. */
  18. return ttail->logfile_sz?\
  19. _ttail_search_closest_files(ttail, tm):\
  20. _ttail_search_closest_stdin(ttail, tm);
  21. }
  22. int _ttail_search_closest_stdin(ttail_t* t, const struct tm* tm)
  23. {
  24. return 0;
  25. }
  26. int ttail_logline2date(ttail_t* ttail, const char* logline, struct tm* tm)
  27. {
  28. const char *subst, *ret;
  29. memset(tm, 0,sizeof(struct tm));
  30. subst = ttail_logline_subst(ttail, logline);
  31. if(!subst)
  32. {
  33. memset(tm, 0,sizeof(struct tm));
  34. return 1;
  35. }
  36. ret = strptime(subst, ttail->fmt, tm);
  37. if(!ret)
  38. {
  39. memset(tm, 0,sizeof(struct tm));
  40. return 2;
  41. }
  42. return 0;
  43. }
  44. const char* ttail_logline_subst(ttail_t* t, const char* logline)
  45. {
  46. regmatch_t pmatch[1];
  47. size_t nmatch;
  48. int ret;
  49. char err[1024];
  50. if(t->prefix_sz >= 0)
  51. {
  52. /* constant subst */
  53. return strlen(logline) < t->prefix_sz ? \
  54. NULL:logline + t->prefix_sz;
  55. }
  56. /* regex subst */
  57. nmatch=1;
  58. ret = regexec(&(t->date_prefix), logline, nmatch, pmatch, 0);\
  59. if(ret)
  60. {
  61. regerror(ret, &(t->date_prefix), err,1024);
  62. fprintf(stderr, "Exec error : %s\n", err);
  63. return NULL;
  64. }
  65. return logline + pmatch[0].rm_eo;
  66. }
  67. int ttail_tm_cmp(const struct tm *ta, const struct tm *tb)
  68. {
  69. int r;
  70. r = ta->tm_year - tb->tm_year;
  71. if(r) { return r; }
  72. r = ta->tm_mon - tb->tm_mon;
  73. if(r) { return r; }
  74. r = ta->tm_mday - tb->tm_mday;
  75. if(r) { return r; }
  76. r = ta->tm_hour - tb->tm_hour;
  77. if(r) { return r; }
  78. r = ta->tm_min - tb->tm_min;
  79. if(r) { return r; }
  80. r = ta->tm_sec - tb->tm_sec;
  81. return r;
  82. }