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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2017 Yann Weber
  3. *
  4. * This file is part of Ttail.
  5. *
  6. * Ttail is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * any later version.
  10. *
  11. * Ttail is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Ttail. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef _ttail_search_files_h__
  20. #define _ttail_search_files_h__
  21. #include <errno.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <regex.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. typedef struct _ttail_search_file_s ttail_search_file_t;
  29. struct _ttail_files_off_s
  30. {
  31. size_t id;
  32. off_t off;
  33. };
  34. typedef struct _ttail_files_off_s ttail_files_off_t;
  35. /*<! Private search session for logfiles */
  36. struct _ttail_search_file_s
  37. {
  38. /*<! logfile sizes */
  39. off_t *file_sz;
  40. #ifdef TTAIL_HUGE_FILE
  41. /*<! Shift width to apply on size to compute stuff */
  42. short sz_div;
  43. #endif
  44. /*<! Computed files start size
  45. *@todo delete, useless
  46. */
  47. off_t *vfile;
  48. /*<! Computed file sizes sum
  49. *@todo delete useless
  50. */
  51. off_t vsz;
  52. /*<! Computed position
  53. *@todo delete, useless
  54. */
  55. off_t vpos;
  56. /*<! buffer for ttail_getiline() macro */
  57. char *buf;
  58. /*<! buffer size for ttail_getiline() macro */
  59. size_t buf_sz;
  60. /*<! Closest offset to min date*/
  61. ttail_files_off_t off_min;
  62. /*<! Closest offset to max date*/
  63. ttail_files_off_t off_max;
  64. };
  65. #include "config.h"
  66. #include "ttail.h"
  67. #include "ttail_init.h"
  68. #include "ttail_search.h"
  69. /**@brief Convenient wrapper for getline
  70. *@param ttail_t* TTAIL
  71. *@param size_t ID file id
  72. *@return @ref getline()
  73. */
  74. #define ttail_file_getline(TTAIL, ID) (getline(\
  75. &(TTAIL->session->file.buf), &(TTAIL->session->file.buf_sz),\
  76. TTAIL->logfile[ID]))
  77. /*<!Accessor to getline wrapper buffer */
  78. #define ttail_file_getline_buf(TTAIL) (TTAIL->session->file.buf)
  79. /**@brief Init the ttail->session
  80. *@note Needed for format autodetection
  81. *@return 0 on success -1 on failure
  82. */
  83. int ttail_search_files_init(ttail_t*);
  84. /**@brief Date format initialisation using logfiles
  85. *
  86. *If no format set yet attempt to guess it from logfiles
  87. *@param ttail_t*
  88. *@return 0 on success -1 on failure 1 if format was allready set
  89. *@see _ttail_search_files_fmt_guess()
  90. */
  91. int _ttail_search_files_fmt_init(ttail_t*);
  92. /**@brief @ref ttail_search_closest() implementation for logfiles
  93. *
  94. *@warning Expect that ttail_search_closest_files_init() has been called
  95. *Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
  96. *@param ttail_t*
  97. *@return 0 if ok -1 if fatal error 1 if not found
  98. */
  99. int _ttail_search_closest_files(ttail_t*);
  100. /**@brief Output result loglines to stdout
  101. *@param ttail_t*
  102. *@param int fd
  103. */
  104. void _ttail_search_print_files(ttail_t*, int);
  105. int _ttail_search_closest_files_set_fsizes(ttail_t*);
  106. /**@brief Binary search of the last logline with a date < tm
  107. *@param ttail ttail_t*
  108. *@param tm struct tm*
  109. *param min short if 1 process min else process max
  110. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  111. *@return 0 if ok -1 if error 1 if empty result
  112. */
  113. int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
  114. const struct tm**, short);
  115. /**@brief Binary search of the last logline with a date < tm in a file
  116. *
  117. *@note uses ttail_t session data to know where to search
  118. *@param ttail ttail_t*
  119. *@param tm struct tm*
  120. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  121. *param min short if 1 process min else process max
  122. *@return 0 if ok -1 if error 1 if empty result
  123. */
  124. int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
  125. const struct tm**, short);
  126. /**@brief Attempt to reopen a file
  127. *@param ttail_t* ttail
  128. *@param size_t id file id in ttail
  129. *@return 0 on success, -1 on failure and errno is set
  130. *@throw EINVAL if id is too big
  131. */
  132. int _ttail_file_reopen(ttail_t*, size_t);
  133. /**@brief Set min & max date of file
  134. *@param ttail_t* ttail
  135. *@param size_t id file id in ttail
  136. *@param struct tm[2] will be set to min & max
  137. *@return 0 on success 1 if no date found or no fp -1 if error
  138. */
  139. inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
  140. /**@brief Search next line
  141. *
  142. *Set f pos to next line begining and return the position
  143. *@param ttail_t* t
  144. *@param size_t id logfile id
  145. *@return -1 on error 0 on EOF else return the next line position
  146. */
  147. inline long _ttail_file_next_line(ttail_t*, size_t);
  148. /**@brief Search line start
  149. *
  150. *Set f pos to line begining and return the position
  151. *@param ttail_t* t
  152. *@param size_t id logfile id
  153. *@return -1 on error else return the next line position
  154. */
  155. inline long _ttail_file_start_line(ttail_t*, size_t);
  156. /**@brief Search last line with a date < tm from EOF
  157. *@param ttail ttail_t*
  158. *@param id size_t
  159. *@param tm const struct tm*
  160. *@return -1 on error else the offset of the line first chr
  161. *@todo checks
  162. */
  163. inline off_t _ttail_file_search_from_end(ttail_t*, size_t, const struct tm*);
  164. /**@brief Read a line from off and compare its date to tm
  165. *@param t ttail_t*
  166. *@param id site_t
  167. *@param off off_t
  168. *@param tm const struct tm*
  169. *@param res int* see return value of @ref ttail_tm_cmp()
  170. *@return 0 on success -1 on error 1 if no date found
  171. */
  172. inline int _ttail_file_off_cmp(ttail_t*, size_t, off_t, const struct tm*, int*);
  173. /**@brief Same than _ttail_file_off_cmp() but from current pos
  174. *@return 0 on success -1 on error 1 if no date found*/
  175. inline int _ttail_file_cur_cmp(ttail_t*, size_t, const struct tm*, int*);
  176. /**@brief Free the ttail_search_file_t session
  177. *@param ttail_t* ttail
  178. */
  179. void _ttail_search_file_free(ttail_t*);
  180. #endif