123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- /*
- * 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_init_h__
- #define _ttail_init_h__
-
- #include <ctype.h>
- #include <errno.h>
- #include <getopt.h>
- #include <regex.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <time.h>
- #include <unistd.h>
-
- #include "ttail.h"
-
- /**@todo set logfiles as "leadings" option */
- #define TTAIL_LONG_OPT {\
- {"verbose", no_argument, 0, 'v'},\
- {"re-prefix", required_argument, 0, 'r'},\
- {"re-extended", no_argument, 0, 'E'},\
- {"re-ignore-case", no_argument, 0, 'i'},\
- {"prefix-len", required_argument, 0, 'p'},\
- {"date-format", required_argument, 0, 'f'},\
- {"date-min", required_argument, 0, 'd'},\
- {"date-max", required_argument, 0, 'm'},\
- {"help", no_argument, 0, 'h'},\
- {0, 0, 0, 0 }\
- }
- #define TTAIL_SHORT_OPT "vr:Eip:f:d:l:m:h"
-
- #define TTAIL_OPT_HELP {\
- {"Augment the verbosity level",NULL},\
- {"Matched part of logline will be stripped","REGEX"},\
- {"Interpret REGEX as an extended regular expression (specified by \
- POSIX) ",NULL},\
- {"Ignore case distinctions (specified by POSIX)",NULL},\
- {"Indicate to strip a fixed len prefix","CHAR_COUNT"},\
- {"Set the date format (see man strptime for supported date formats"\
- ,"FORMAT"},\
- {"Start to output loglines starting from this date","DATE"},\
- {"Stop to output loglines before this date","DATE"},\
- {"Print this help and exit",NULL},\
- {"",NULL}\
- }
-
- #define TTAIL_HELP_TEXT "\tLOGFILES\n\t\tLogfiles, can take '-' as value to \
- tell ttail to read from stdin instead of files\n\n\
- Date formats\n\n\
- \tTwo date formats are allowed for dates arguments (-d , -m) :\n\
- \t- the same than in the logfile\n\
- \t- relative from now prefixing the argument with '#-' and given a unit\n\
- \t\t- y : year\n\
- \t\t- M : Month\n\
- \t\t- d : day\n\
- \t\t- h : hour\n\
- \t\t- m : min\n\
- \t\t- s : sec\n\
- "
-
- #define TTAIL_NORMDATE(ttail,tm,FIELD) {\
- if(ttail->flag & TTAIL_FLAG_DATE_MIN && (tm)->FIELD == -1)\
- { ttail->date_min.FIELD = -1; }\
- if(ttail->flag & TTAIL_FLAG_DATE_MAX && (tm)->FIELD == -1)\
- { ttail->date_max.FIELD = -1; }\
- }
-
- /**<! Print help & usage */
- void usage();
-
- /**@brief Parse cli arguments and return a ttail_t
- *@param int argc
- *@param char** argv
- *@return NULL on error
- */
- ttail_t *ttail_init(int, char**);
-
- /**@brief Checks that init returns a valid ttail_t runtime
- *@param ttail_t* t
- *@return 0 if no error -1 if fatal error
- */
- int ttail_init_check(ttail_t*);
-
- /**@brief Add a logfile
- *@param ttail_t*
- *@param const char * filename
- *@return 0 if no errors 1 if unable to open file -1 if fatal error
- */
- int ttail_add_logfile(ttail_t*, const char*);
-
- /**@brief Set a date prefix regex
- *@param ttail_t*
- *@param const char * regex
- *@return 0 if no errors 1 if allready set -1 if compilation fails
- */
- int ttail_set_prefix(ttail_t*, const char*);
-
- /**@brief Set a date format
- *
- * The supported input field descriptors are listed below. In case a text string (such as the name
- * of a day of the week or a month name) is to be matched, the comparison is case insensitive. In
- * case a number is to be matched, leading zeros are permitted but not required.
- *
- * \%% The % character.
- *
- * \%a or %A
- * The name of the day of the week according to the current locale, in abbreviated form or
- * the full name.
- *
- * \%b or %B or %h
- * The month name according to the current locale, in abbreviated form or the full name.
- *
- * \%c The date and time representation for the current locale.
- *
- * \%C The century number (0-99).
- *
- * \%d or %e
- * The day of month (1-31).
- *
- * \%D Equivalent to %m/%d/%y. (This is the American style date, very confusing to non-Ameri‐
- * cans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard format
- * is %Y-%m-%d.)
- *
- * \%H The hour (0-23).
- *
- * \%I The hour on a 12-hour clock (1-12).
- *
- * \%j The day number in the year (1-366).
- *
- * \%m The month number (1-12).
- *
- * \%M The minute (0-59).
- *
- * \%n Arbitrary whitespace.
- *
- * \%p The locale's equivalent of AM or PM. (Note: there may be none.)
- *
- * \%r The 12-hour clock time (using the locale's AM or PM). In the POSIX locale equivalent to
- * \%I:%M:%S %p. If t_fmt_ampm is empty in the LC_TIME part of the current locale, then the
- * behavior is undefined.
- *
- * \%R Equivalent to %H:%M.
- *
- * \%S The second (0-60; 60 may occur for leap seconds; earlier also 61 was allowed).
- *
- * \%t Arbitrary whitespace.
- *
- * \%T Equivalent to %H:%M:%S.
- *
- * \%U The week number with Sunday the first day of the week (0-53). The first Sunday of Janu‐
- * ary is the first day of week 1.
- *
- * \%w The ordinal number of the day of the week (0-6), with Sunday = 0.
- *
- * \%W The week number with Monday the first day of the week (0-53). The first Monday of Janu‐
- * ary is the first day of week 1.
- *
- * \%x The date, using the locale's date format.
- *
- * \%X The time, using the locale's time format.
- *
- * \%y The year within century (0-99). When a century is not otherwise specified, values in the
- * range 69-99 refer to years in the twentieth century (1969-1999); values in the range
- * 00-68 refer to years in the twenty-first century (2000-2068).
- *
- * \%Y The year, including century (for example, 1991).
- *
- * Some field descriptors can be modified by the E or O modifier characters to indicate that an
- * alternative format or specification should be used. If the alternative format or specification
- * does not exist in the current locale, the unmodified field descriptor is used.
- *
- *
- * The E modifier specifies that the input string may contain alternative locale-dependent versions
- * of the date and time representation:
- *
- * \%Ec The locale's alternative date and time representation.
- *
- * \%EC The name of the base year (period) in the locale's alternative representation.
- *
- * \%Ex The locale's alternative date representation.
- *
- * \%EX The locale's alternative time representation.
- *
- * \%Ey The offset from %EC (year only) in the locale's alternative representation.
- *
- * \%EY The full alternative year representation.
- *
- * The O modifier specifies that the numerical input may be in an alternative locale-dependent for‐
- * mat:
- *
- * \%Od or %Oe
- * The day of the month using the locale's alternative numeric symbols; leading zeros are
- * permitted but not required.
- *
- * \%OH The hour (24-hour clock) using the locale's alternative numeric symbols.
- *
- * \%OI The hour (12-hour clock) using the locale's alternative numeric symbols.
- *
- * \%Om The month using the locale's alternative numeric symbols.
- *
- * \%OM The minutes using the locale's alternative numeric symbols.
- *
- * \%OS The seconds using the locale's alternative numeric symbols.
- *
- * \%OU The week number of the year (Sunday as the first day of the week) using the locale's
- * alternative numeric symbols.
- *
- * \%Ow The ordinal number of the day of the week (Sunday=0),
- * using the locale's alternative numeric symbols.
- *
- * \%OW The week number of the year (Monday as the first day of the week) using the locale's
- * alternative numeric symbols.
- *
- * \%Oy The year (offset from %C) using the locale's alternative numeric symbols.
- *
- *
- *@param ttail_t*
- *@param const char * format
- *@return 0 if no errors 1 if allready set -1 if compilation fails
- */
- int ttail_set_fmt(ttail_t*, const char*);
-
- /**@brief Set the extended regex flag
- *@param ttail ttail_t*
- *@return -1 on error else 0
- */
- int ttail_set_flag_re_ex(ttail_t*);
-
- /**@brief Set the case insensitive regex flag
- *@param ttail ttail_t*
- *@return -1 on error else 0
- */
- int ttail_set_flag_re_ci(ttail_t*);
-
- /**@brief Set dates min/max
- *
- *After the call dates are free and set to NULL except if error
- *@param ttail_t ttail instance
- *@param char*[2] dates {min,max} both can be NULL
- *@return -1 if error 0 else
- */
- int ttail_set_dates(ttail_t*, char*[2]);
-
- /**@brief Set a date using format or format detection
- *
- *@param ttail_t ttail instance
- *@param char*[2] dates {min,max} both can be NULL
- *@param int c the date id to handle
- *@return -1 if error 0 else
- */
- int _ttail_set_date_fmt(ttail_t*, char*, int);
-
- /**@brief Set a date relative from now
- *
- *Relative date formats are "-#", an integer followed by a unit. Regcognized
- *units are :
- *- y[ear]
- *- M[onth]
- *- d[ay]
- *- h[our]
- *- m[in]
- *- s[ec]
- *
- *Examples :
- *- -#2s
- *- -#3Month
- *@param ttail_t ttail instance
- *@param char*[2] dates {min,max} both can be NULL
- *@param int c the date id to handle
- *@return -1 if error 0 else
- *@todo checks
- */
- int _ttail_set_date_relative(ttail_t*, char*, int);
-
- /**@brief Normalize dates using ttail.fmt
- *
- *When dates are relatives from now all the struct tm fields are set. In
- *logfiles year or seconds can be missing from date format. In those case we
- *have to set to -1 missing fields
- *@note must be called after arg parsing, ttail_set_dates() calls and
- *ttail_format_guess() calls
- *@param ttail_t* An initialized ttail_t
- *@return -1 if error 0 else
- */
- int ttail_norm_dates(ttail_t*);
-
- /**@brief Attempt to guess a dateformat
- *@param const char* date as a dtring
- *@param struct tm* if non NULL will be set to detected date
- *@return -1 if no guess else id in TTAIL_DEFAULT_FORMATS
- */
- int ttail_format_guess(const char*, struct tm*);
-
-
- void ttail_free(ttail_t*);
-
-
- #endif
|