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_files.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef _ttail_search_files_h__
  2. #define _ttail_search_files_h__
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <regex.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. typedef struct _ttail_search_file_s ttail_search_file_t;
  11. struct _ttail_files_off_s
  12. {
  13. size_t id;
  14. off_t off;
  15. };
  16. typedef struct _ttail_files_off_s ttail_files_off_t;
  17. /*<! Private search session for logfiles */
  18. struct _ttail_search_file_s
  19. {
  20. /*<! logfile sizes */
  21. off_t *file_sz;
  22. #ifdef TTAIL_HUGE_FILE
  23. /*<! Shift width to apply on size to compute stuff */
  24. short sz_div;
  25. #endif
  26. /*<! Computed files start size
  27. *@todo delete, useless
  28. */
  29. off_t *vfile;
  30. /*<! Computed file sizes sum
  31. *@todo delete useless
  32. */
  33. off_t vsz;
  34. /*<! Computed position
  35. *@todo delete, useless
  36. */
  37. off_t vpos;
  38. /*<! buffer for ttail_getiline() macro */
  39. char *buf;
  40. /*<! buffer size for ttail_getiline() macro */
  41. size_t buf_sz;
  42. /*<! Closest offset to min date*/
  43. ttail_files_off_t off_min;
  44. /*<! Closest offset to max date*/
  45. ttail_files_off_t off_max;
  46. };
  47. #include "config.h"
  48. #include "ttail.h"
  49. #include "ttail_search.h"
  50. /**@brief Convenient wrapper for getline
  51. *@param ttail_t* TTAIL
  52. *@param size_t ID file id
  53. *@return @ref getline()
  54. */
  55. #define ttail_getline(TTAIL, ID) (getline(\
  56. &(TTAIL->session->file.buf), &(TTAIL->session->file.buf_sz),\
  57. TTAIL->logfile[ID]))
  58. /*<!Accessor to getline wrapper buffer */
  59. #define ttail_getline_buf(TTAIL) (TTAIL->session->file.buf)
  60. /**@brief @ref ttail_search_closest() implementation for logfiles
  61. *
  62. *Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
  63. *@param ttail_t*
  64. *@return 0 if ok -1 if fatal error 1 if not found
  65. */
  66. int _ttail_search_closest_files(ttail_t*);
  67. /**@brief Output result loglines to stdout
  68. *@param ttail_t*
  69. *@param int fd
  70. */
  71. void _ttail_search_print_files(ttail_t*, int);
  72. int _ttail_search_closest_files_init(ttail_t*);
  73. int _ttail_search_closest_files_set_fsizes(ttail_t*);
  74. /**@brief Binary search of the last logline with a date < tm
  75. *@param ttail ttail_t*
  76. *@param tm struct tm*
  77. *param min short if 1 process min else process max
  78. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  79. *@return 0 if ok -1 if error 1 if empty result
  80. */
  81. int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
  82. const struct tm**, short);
  83. /**@brief Binary search of the last logline with a date < tm in a file
  84. *
  85. *@note uses ttail_t session data to know where to search
  86. *@param ttail ttail_t*
  87. *@param tm struct tm*
  88. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  89. *param min short if 1 process min else process max
  90. *@return 0 if ok -1 if error 1 if empty result
  91. */
  92. int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
  93. const struct tm**, short);
  94. /**@brief Attempt to reopen a file
  95. *@param ttail_t* ttail
  96. *@param size_t id file id in ttail
  97. *@return 0 on success, -1 on failure and errno is set
  98. *@throw EINVAL if id is too big
  99. */
  100. int _ttail_file_reopen(ttail_t*, size_t);
  101. /**@brief Set min & max date of file
  102. *@param ttail_t* ttail
  103. *@param size_t id file id in ttail
  104. *@param struct tm[2] will be set to min & max
  105. *@return 0 on success 1 if no date found or no fp -1 if error
  106. */
  107. inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
  108. /**@brief Search next line
  109. *
  110. *Set f pos to next line begining and return the position
  111. *@param FILE* f
  112. *@return -1 on error 0 on EOF else return the next line position
  113. *@todo change header to int _ttail_file_next_line(ttail_t, size_t, size_t*)
  114. */
  115. inline long _ttail_file_next_line(FILE*);
  116. /**@brief Search line start
  117. *
  118. *Set f pos to line begining and return the position
  119. *@param FILE* f
  120. *@return -1 on error else return the next line position
  121. *@todo change header to int _ttail_file_start_line(ttail_t, size_t, size_t*)
  122. */
  123. inline long _ttail_file_start_line(FILE*);
  124. /**@brief Search last line with a date < tm from EOF
  125. *@param ttail ttail_t*
  126. *@param id size_t
  127. *@param tm const struct tm*
  128. *@return -1 on error else the offset of the line first chr
  129. *@todo checks
  130. */
  131. inline off_t _ttail_from_search_from_end(ttail_t*, size_t, const struct tm*);
  132. /**@brief Read a line from off and compare its date to tm
  133. *@param t ttail_t*
  134. *@param id site_t
  135. *@param off off_t
  136. *@param tm const struct tm*
  137. *@param res int* see return value of @ref ttail_tm_cmp()
  138. *@return 0 on success -1 on error 1 if no date found
  139. */
  140. inline int _ttail_file_off_cmp(ttail_t*, size_t, off_t, const struct tm*, int*);
  141. /**@brief Same than _ttail_file_off_cmp() but from current pos
  142. *@return 0 on success -1 on error 1 if no date found*/
  143. inline int _ttail_file_cur_cmp(ttail_t*, size_t, const struct tm*, int*);
  144. /**@brief Free the ttail_search_file_t session
  145. *@param ttail_t* ttail
  146. */
  147. void _ttail_search_file_free(ttail_t*);
  148. #endif