timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
Ви не можете вибрати більше 25 тем Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "ttail_search.h"
  2. int ttail_search_closest(ttail_t* ttail)
  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):\
  20. _ttail_search_closest_stdin(ttail);
  21. }
  22. int _ttail_search_closest_stdin(ttail_t* t)
  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. }