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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 Move an element to end
  80. *@param void* v : array
  81. *@param size_t i : elt to move
  82. *@param size_t count : v elements count - 1
  83. *@param type t : v type
  84. */
  85. #define TTAIL_MVF(v, i, count, t) {\
  86. t tmp;\
  87. tmp = v[i];\
  88. memmove(v+i, v+i+1, (count - i) * sizeof(t));\
  89. v[count] = tmp;\
  90. }
  91. /**@brief Move back an element to j position
  92. *@warning j < i
  93. *@param void* v
  94. *@param size_t i : src index
  95. *@param size_t j : dst index
  96. *@param type t : v type
  97. */
  98. #define TTAIL_MVBF(v, i, j, t) {\
  99. t tmp;\
  100. tmp = v[i];\
  101. memmove(v+j+1, v+j, (i - j)*sizeof(t));\
  102. v[j] = tmp;\
  103. }
  104. /**@brief Init the ttail->session
  105. *@note Needed for format autodetection
  106. *@return 0 on success -1 on failure
  107. */
  108. int ttail_search_files_init(ttail_t*);
  109. /**@brief Date format initialisation using logfiles
  110. *
  111. *If no format set yet attempt to guess it from logfiles
  112. *@param ttail_t*
  113. *@return 0 on success -1 on failure 1 if format was allready set
  114. *@see _ttail_search_files_fmt_guess()
  115. */
  116. int _ttail_search_files_fmt_init(ttail_t*);
  117. /**@brief @ref ttail_search_closest() implementation for logfiles
  118. *
  119. *@warning Expect that ttail_search_closest_files_init() has been called
  120. *Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
  121. *@param ttail_t*
  122. *@return 0 if ok -1 if fatal error 1 if not found
  123. */
  124. int _ttail_search_closest_files(ttail_t*);
  125. /**@brief Output result loglines to stdout
  126. *@param ttail_t*
  127. *@param int fd
  128. */
  129. void _ttail_search_print_files(ttail_t*, int);
  130. int _ttail_search_closest_files_set_fsizes(ttail_t*);
  131. /**@brief Binary search of the last logline with a date < tm
  132. *@param ttail ttail_t*
  133. *@param tm struct tm*
  134. *param min short if 1 process min else process max
  135. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  136. *@return 0 if ok -1 if error 1 if empty result
  137. */
  138. int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
  139. const struct tm**, short);
  140. /**@brief Binary search of the last logline with a date < tm in a file
  141. *
  142. *@note uses ttail_t session data to know where to search
  143. *@param ttail ttail_t*
  144. *@param tm struct tm*
  145. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  146. *param min short if 1 process min else process max
  147. *@return 0 if ok -1 if error 1 if empty result
  148. */
  149. int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
  150. const struct tm**, short);
  151. /**@brief Attempt to reopen a file
  152. *@param ttail_t* ttail
  153. *@param size_t id file id in ttail
  154. *@return 0 on success, -1 on failure and errno is set
  155. *@throw EINVAL if id is too big
  156. */
  157. int _ttail_file_reopen(ttail_t*, size_t);
  158. /**@brief Set min & max date of file
  159. *@param ttail_t* ttail
  160. *@param size_t id file id in ttail
  161. *@param struct tm[2] will be set to min & max
  162. *@return 0 on success 1 if no date found or no fp -1 if error
  163. */
  164. inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
  165. /**@brief Sort logfiles list
  166. *@param ttail_t* ttail
  167. *@param struct tm** ftm : files min and max date
  168. *@return 0 on success -1 if error
  169. */
  170. inline int _ttail_file_sort(ttail_t*, struct tm**);
  171. /**@brief Search next line
  172. *
  173. *Set f pos to next line begining and return the position
  174. *@param ttail_t* t
  175. *@param size_t id logfile id
  176. *@return -1 on error 0 on EOF else return the next line position
  177. */
  178. inline long _ttail_file_next_line(ttail_t*, size_t);
  179. /**@brief Search line start
  180. *
  181. *Set f pos to line begining and return the position
  182. *@param ttail_t* t
  183. *@param size_t id logfile id
  184. *@return -1 on error else return the next line position
  185. */
  186. inline long _ttail_file_start_line(ttail_t*, size_t);
  187. /**@brief Search last line with a date < tm from EOF
  188. *@param ttail ttail_t*
  189. *@param id size_t
  190. *@param tm const struct tm*
  191. *@return -1 on error else the offset of the line first chr
  192. */
  193. inline off_t _ttail_file_search_from_end(ttail_t*, size_t, const struct tm*);
  194. /**@brief Read a line from off and compare its date to tm
  195. *@param t ttail_t*
  196. *@param id site_t
  197. *@param off off_t
  198. *@param tm const struct tm*
  199. *@param res int* see return value of @ref ttail_tm_cmp()
  200. *@return 0 on success -1 on error 1 if no date found
  201. */
  202. inline int _ttail_file_off_cmp(ttail_t*, size_t, off_t, const struct tm*, int*);
  203. /**@brief Same than _ttail_file_off_cmp() but from current pos
  204. *@return 0 on success -1 on error 1 if no date found*/
  205. inline int _ttail_file_cur_cmp(ttail_t*, size_t, const struct tm*, int*);
  206. /**@brief Free the ttail_search_file_t session
  207. *@param ttail_t* ttail
  208. */
  209. void _ttail_search_file_free(ttail_t*);
  210. #endif