timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ttail_search_files.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 @ref ttail_search_closest() implementation for logfiles
  79. *
  80. *Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
  81. *@param ttail_t*
  82. *@return 0 if ok -1 if fatal error 1 if not found
  83. */
  84. int _ttail_search_closest_files(ttail_t*);
  85. /**@brief Output result loglines to stdout
  86. *@param ttail_t*
  87. *@param int fd
  88. */
  89. void _ttail_search_print_files(ttail_t*, int);
  90. int _ttail_search_closest_files_init(ttail_t*);
  91. int _ttail_search_closest_files_set_fsizes(ttail_t*);
  92. /**@brief Binary search of the last logline with a date < tm
  93. *@param ttail ttail_t*
  94. *@param tm struct tm*
  95. *param min short if 1 process min else process max
  96. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  97. *@return 0 if ok -1 if error 1 if empty result
  98. */
  99. int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
  100. const struct tm**, short);
  101. /**@brief Binary search of the last logline with a date < tm in a file
  102. *
  103. *@note uses ttail_t session data to know where to search
  104. *@param ttail ttail_t*
  105. *@param tm struct tm*
  106. *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
  107. *param min short if 1 process min else process max
  108. *@return 0 if ok -1 if error 1 if empty result
  109. */
  110. int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
  111. const struct tm**, short);
  112. /**@brief Attempt to reopen a file
  113. *@param ttail_t* ttail
  114. *@param size_t id file id in ttail
  115. *@return 0 on success, -1 on failure and errno is set
  116. *@throw EINVAL if id is too big
  117. */
  118. int _ttail_file_reopen(ttail_t*, size_t);
  119. /**@brief Set min & max date of file
  120. *@param ttail_t* ttail
  121. *@param size_t id file id in ttail
  122. *@param struct tm[2] will be set to min & max
  123. *@return 0 on success 1 if no date found or no fp -1 if error
  124. */
  125. inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
  126. /**@brief Search next line
  127. *
  128. *Set f pos to next line begining and return the position
  129. *@param FILE* f
  130. *@return -1 on error 0 on EOF else return the next line position
  131. *@todo change header to int _ttail_file_next_line(ttail_t, size_t, size_t*)
  132. */
  133. inline long _ttail_file_next_line(FILE*);
  134. /**@brief Search line start
  135. *
  136. *Set f pos to line begining and return the position
  137. *@param FILE* f
  138. *@return -1 on error else return the next line position
  139. *@todo change header to int _ttail_file_start_line(ttail_t, size_t, size_t*)
  140. */
  141. inline long _ttail_file_start_line(FILE*);
  142. /**@brief Search last line with a date < tm from EOF
  143. *@param ttail ttail_t*
  144. *@param id size_t
  145. *@param tm const struct tm*
  146. *@return -1 on error else the offset of the line first chr
  147. *@todo checks
  148. */
  149. inline off_t _ttail_from_search_from_end(ttail_t*, size_t, const struct tm*);
  150. /**@brief Read a line from off and compare its date to tm
  151. *@param t ttail_t*
  152. *@param id site_t
  153. *@param off off_t
  154. *@param tm const struct tm*
  155. *@param res int* see return value of @ref ttail_tm_cmp()
  156. *@return 0 on success -1 on error 1 if no date found
  157. */
  158. inline int _ttail_file_off_cmp(ttail_t*, size_t, off_t, const struct tm*, int*);
  159. /**@brief Same than _ttail_file_off_cmp() but from current pos
  160. *@return 0 on success -1 on error 1 if no date found*/
  161. inline int _ttail_file_cur_cmp(ttail_t*, size_t, const struct tm*, int*);
  162. /**@brief Free the ttail_search_file_t session
  163. *@param ttail_t* ttail
  164. */
  165. void _ttail_search_file_free(ttail_t*);
  166. #endif