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_search.h"
  68. /**@brief Convenient wrapper for getline
  69. *@param ttail_t* TTAIL
  70. *@param size_t ID file id
  71. *@return @ref getline()
  72. */
  73. #define ttail_file_getline(TTAIL, ID) (getline(\
  74. &(TTAIL->session->file.buf), &(TTAIL->session->file.buf_sz),\
  75. TTAIL->logfile[ID]))
  76. /*<!Accessor to getline wrapper buffer */
  77. #define ttail_file_getline_buf(TTAIL) (TTAIL->session->file.buf)
  78. /**@brief Init the ttail->session
  79. *@note Needed for format autodetection
  80. *@return 0 on success -1 on failure
  81. */
  82. int ttail_search_files_init(ttail_t*);
  83. /**@brief @ref ttail_search_closest() implementation for logfiles
  84. *
  85. *@warning Expect that ttail_search_closest_files_init() has been called
  86. *Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
  87. *@param ttail_t*
  88. *@return 0 if ok -1 if fatal error 1 if not found
  89. */
  90. int _ttail_search_closest_files(ttail_t*);
  91. /**@brief Provide datas for format autodetection
  92. *@param ttail_t* t
  93. *@param int* : retry left
  94. *@param size_t* : line count
  95. *@param char*** : pointer on lines buffer
  96. *@return -1 on error 1 on autodetection impossible 0 on success
  97. */
  98. int _ttail_search_fmtdetect_provider_files(ttail_t*, int*, size_t*, char***);
  99. /**@brief Output result loglines to stdout
  100. *@param ttail_t*
  101. *@param int fd
  102. */
  103. void _ttail_search_print_files(ttail_t*, int);
  104. int _ttail_search_closest_files_set_fsizes(ttail_t*);
  105. /**@brief Binary search of the last logline with a date < tm
  106. *@param ttail ttail_t*
  107. *@param tm struct tm*
  108. *param min short if 1 process min else process max
  109. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  110. *@return 0 if ok -1 if error 1 if empty result
  111. */
  112. int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
  113. const struct tm**, short);
  114. /**@brief Binary search of the last logline with a date < tm in a file
  115. *
  116. *@note uses ttail_t session data to know where to search
  117. *@param ttail ttail_t*
  118. *@param tm struct tm*
  119. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  120. *param min short if 1 process min else process max
  121. *@return 0 if ok -1 if error 1 if empty result
  122. */
  123. int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
  124. const struct tm**, short);
  125. /**@brief Attempt to reopen a file
  126. *@param ttail_t* ttail
  127. *@param size_t id file id in ttail
  128. *@return 0 on success, -1 on failure and errno is set
  129. *@throw EINVAL if id is too big
  130. */
  131. int _ttail_file_reopen(ttail_t*, size_t);
  132. /**@brief Set min & max date of file
  133. *@param ttail_t* ttail
  134. *@param size_t id file id in ttail
  135. *@param struct tm[2] will be set to min & max
  136. *@return 0 on success 1 if no date found or no fp -1 if error
  137. */
  138. inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
  139. /**@brief Search next line
  140. *
  141. *Set f pos to next line begining and return the position
  142. *@param ttail_t* t
  143. *@param size_t id logfile id
  144. *@return -1 on error 0 on EOF else return the next line position
  145. */
  146. inline long _ttail_file_next_line(ttail_t*, size_t);
  147. /**@brief Search line start
  148. *
  149. *Set f pos to line begining and return the position
  150. *@param ttail_t* t
  151. *@param size_t id logfile id
  152. *@return -1 on error else return the next line position
  153. */
  154. inline long _ttail_file_start_line(ttail_t*, size_t);
  155. /**@brief Search last line with a date < tm from EOF
  156. *@param ttail ttail_t*
  157. *@param id size_t
  158. *@param tm const struct tm*
  159. *@return -1 on error else the offset of the line first chr
  160. *@todo checks
  161. */
  162. inline off_t _ttail_file_search_from_end(ttail_t*, size_t, const struct tm*);
  163. /**@brief Read a line from off and compare its date to tm
  164. *@param t ttail_t*
  165. *@param id site_t
  166. *@param off off_t
  167. *@param tm const struct tm*
  168. *@param res int* see return value of @ref ttail_tm_cmp()
  169. *@return 0 on success -1 on error 1 if no date found
  170. */
  171. inline int _ttail_file_off_cmp(ttail_t*, size_t, off_t, const struct tm*, int*);
  172. /**@brief Same than _ttail_file_off_cmp() but from current pos
  173. *@return 0 on success -1 on error 1 if no date found*/
  174. inline int _ttail_file_cur_cmp(ttail_t*, size_t, const struct tm*, int*);
  175. /**@brief Free the ttail_search_file_t session
  176. *@param ttail_t* ttail
  177. */
  178. void _ttail_search_file_free(ttail_t*);
  179. #endif