123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- /*
- * Copyright 2017 Yann Weber
- *
- * This file is part of Ttail.
- *
- * Ttail is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * Ttail is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Ttail. If not, see <http://www.gnu.org/licenses/>.
- */
- #ifndef _ttail_search_files_h__
- #define _ttail_search_files_h__
-
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <regex.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
-
- typedef struct _ttail_search_file_s ttail_search_file_t;
-
- struct _ttail_files_off_s
- {
- size_t id;
- off_t off;
- };
-
- typedef struct _ttail_files_off_s ttail_files_off_t;
-
- /*<! Private search session for logfiles */
- struct _ttail_search_file_s
- {
- /*<! logfile sizes */
- off_t *file_sz;
- #ifdef TTAIL_HUGE_FILE
- /*<! Shift width to apply on size to compute stuff */
- short sz_div;
- #endif
- /*<! Computed files start size
- *@todo delete, useless
- */
- off_t *vfile;
- /*<! Computed file sizes sum
- *@todo delete useless
- */
- off_t vsz;
- /*<! Computed position
- *@todo delete, useless
- */
- off_t vpos;
-
- /*<! buffer for ttail_getiline() macro */
- char *buf;
- /*<! buffer size for ttail_getiline() macro */
- size_t buf_sz;
-
- /*<! Closest offset to min date*/
- ttail_files_off_t off_min;
- /*<! Closest offset to max date*/
- ttail_files_off_t off_max;
- };
-
- #include "config.h"
- #include "ttail.h"
- #include "ttail_init.h"
- #include "ttail_search.h"
-
- /**@brief Convenient wrapper for getline
- *@param ttail_t* TTAIL
- *@param size_t ID file id
- *@return @ref getline()
- */
- #define ttail_file_getline(TTAIL, ID) (getline(\
- &(TTAIL->session->file.buf), &(TTAIL->session->file.buf_sz),\
- TTAIL->logfile[ID]))
-
- /*<!Accessor to getline wrapper buffer */
- #define ttail_file_getline_buf(TTAIL) (TTAIL->session->file.buf)
-
- /**@brief Init the ttail->session
- *@note Needed for format autodetection
- *@return 0 on success -1 on failure
- */
- int ttail_search_files_init(ttail_t*);
-
- /**@brief Date format initialisation using logfiles
- *
- *If no format set yet attempt to guess it from logfiles
- *@param ttail_t*
- *@return 0 on success -1 on failure 1 if format was allready set
- *@see _ttail_search_files_fmt_guess()
- */
- int _ttail_search_files_fmt_init(ttail_t*);
-
- /**@brief @ref ttail_search_closest() implementation for logfiles
- *
- *@warning Expect that ttail_search_closest_files_init() has been called
- *Will set struct _ttail_search_file_s.id and struct _ttail_search_file_s.off
- *@param ttail_t*
- *@return 0 if ok -1 if fatal error 1 if not found
- */
- int _ttail_search_closest_files(ttail_t*);
-
- /**@brief Output result loglines to stdout
- *@param ttail_t*
- *@param int fd
- */
- void _ttail_search_print_files(ttail_t*, int);
-
- int _ttail_search_closest_files_set_fsizes(ttail_t*);
-
- /**@brief Binary search of the last logline with a date < tm
- *@param ttail ttail_t*
- *@param tm struct tm*
- *param min short if 1 process min else process max
- *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
- *@return 0 if ok -1 if error 1 if empty result
- */
- int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
- const struct tm**, short);
-
- /**@brief Binary search of the last logline with a date < tm in a file
- *
- *@note uses ttail_t session data to know where to search
- *@param ttail ttail_t*
- *@param tm struct tm*
- *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
- *param min short if 1 process min else process max
- *@return 0 if ok -1 if error 1 if empty result
- */
- int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
- const struct tm**, short);
-
- /**@brief Attempt to reopen a file
- *@param ttail_t* ttail
- *@param size_t id file id in ttail
- *@return 0 on success, -1 on failure and errno is set
- *@throw EINVAL if id is too big
- */
- int _ttail_file_reopen(ttail_t*, size_t);
-
- /**@brief Set min & max date of file
- *@param ttail_t* ttail
- *@param size_t id file id in ttail
- *@param struct tm[2] will be set to min & max
- *@return 0 on success 1 if no date found or no fp -1 if error
- */
- inline int _ttail_file_minmax(ttail_t*, size_t, struct tm[2]);
-
- /**@brief Search next line
- *
- *Set f pos to next line begining and return the position
- *@param ttail_t* t
- *@param size_t id logfile id
- *@return -1 on error 0 on EOF else return the next line position
- */
- inline long _ttail_file_next_line(ttail_t*, size_t);
-
- /**@brief Search line start
- *
- *Set f pos to line begining and return the position
- *@param ttail_t* t
- *@param size_t id logfile id
- *@return -1 on error else return the next line position
- */
- inline long _ttail_file_start_line(ttail_t*, size_t);
-
- /**@brief Search last line with a date < tm from EOF
- *@param ttail ttail_t*
- *@param id size_t
- *@param tm const struct tm*
- *@return -1 on error else the offset of the line first chr
- *@todo checks
- */
- inline off_t _ttail_file_search_from_end(ttail_t*, size_t, const struct tm*);
-
- /**@brief Read a line from off and compare its date to tm
- *@param t ttail_t*
- *@param id site_t
- *@param off off_t
- *@param tm const struct tm*
- *@param res int* see return value of @ref ttail_tm_cmp()
- *@return 0 on success -1 on error 1 if no date found
- */
- inline int _ttail_file_off_cmp(ttail_t*, size_t, off_t, const struct tm*, int*);
-
- /**@brief Same than _ttail_file_off_cmp() but from current pos
- *@return 0 on success -1 on error 1 if no date found*/
- inline int _ttail_file_cur_cmp(ttail_t*, size_t, const struct tm*, int*);
-
- /**@brief Free the ttail_search_file_t session
- *@param ttail_t* ttail
- */
- void _ttail_search_file_free(ttail_t*);
- #endif
|