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_init.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_init_h__
  20. #define _ttail_init_h__
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <getopt.h>
  24. #include <regex.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include "ttail.h"
  32. #define TTAIL_LONG_OPT {\
  33. {"date-min", required_argument, 0, 'd'},\
  34. {"date-max", required_argument, 0, 'm'},\
  35. {"date-format", required_argument, 0, 'f'},\
  36. {"prefix-len", required_argument, 0, 'p'},\
  37. {"re-prefix", required_argument, 0, 'r'},\
  38. {"re-extended", no_argument, 0, 'E'},\
  39. {"re-ignore-case", no_argument, 0, 'i'},\
  40. {"permissive", no_argument, 0, 'P'},\
  41. {"verbose", no_argument, 0, 'v'},\
  42. {"help", no_argument, 0, 'h'},\
  43. {0, 0, 0, 0 }\
  44. }
  45. #define TTAIL_SHORT_OPT "vr:Eip:f:d:l:m:Ph"
  46. #define TTAIL_OPT_HELP {\
  47. {"Start to output loglines starting from this date","DATE"},\
  48. {"Stop to output loglines before this date","DATE"},\
  49. {"Set the date format (see man strptime for supported date formats"\
  50. ,"FORMAT"},\
  51. {"Indicate to strip a fixed len prefix","CHAR_COUNT"},\
  52. {"Matched part of logline will be stripped","REGEX"},\
  53. {"Interpret REGEX as an extended regular expression (specified by \
  54. POSIX) ",NULL},\
  55. {"Ignore case distinctions (specified by POSIX)",NULL},\
  56. {"Tell ttail to not stop on loglines not formatted as expected",NULL},\
  57. {"Augment the verbosity level",NULL},\
  58. {"Print this help and exit",NULL},\
  59. {"",NULL}\
  60. }
  61. #define TTAIL_HELP_TEXT "\tLOGFILES\n\t\tLogfiles, can take '-' as value to \
  62. tell ttail to read from stdin instead of files\n\n\
  63. Date formats\n\n\
  64. \tTwo date formats are allowed for dates arguments (-d , -m) :\n\
  65. \t- the same than in the logfile\n\
  66. \t- relative from now prefixing the argument with '#-' and given a unit\n\
  67. \t\t- y : year\n\
  68. \t\t- M : Month\n\
  69. \t\t- d : day\n\
  70. \t\t- h : hour\n\
  71. \t\t- m : min\n\
  72. \t\t- s : sec\n\
  73. "
  74. #define TTAIL_NORMDATE(ttail,tm,FIELD) {\
  75. if(ttail->flag & TTAIL_FLAG_DATE_MIN && (tm)->FIELD == -1)\
  76. { ttail->date_min.FIELD = -1; }\
  77. if(ttail->flag & TTAIL_FLAG_DATE_MAX && (tm)->FIELD == -1)\
  78. { ttail->date_max.FIELD = -1; }\
  79. }
  80. /**<! Print help & usage */
  81. void usage();
  82. /**@brief Parse cli arguments and return a ttail_t
  83. *@param int argc
  84. *@param char** argv
  85. *@return NULL on error
  86. */
  87. ttail_t *ttail_init(int, char**);
  88. /**@brief Checks that init returns a valid ttail_t runtime
  89. *@param ttail_t* t
  90. *@return 0 if no error -1 if fatal error
  91. */
  92. int ttail_init_check(ttail_t*);
  93. /**@brief Add a logfile
  94. *@param ttail_t*
  95. *@param const char * filename
  96. *@return 0 if no errors 1 if unable to open file -1 if fatal error
  97. */
  98. int ttail_add_logfile(ttail_t*, const char*);
  99. /**@brief Set a date prefix regex
  100. *@param ttail_t*
  101. *@param const char * regex
  102. *@return 0 if no errors 1 if allready set -1 if compilation fails
  103. */
  104. int ttail_set_prefix(ttail_t*, const char*);
  105. /**@brief Set a date format
  106. *
  107. * The supported input field descriptors are listed below. In case a text string (such as the name
  108. * of a day of the week or a month name) is to be matched, the comparison is case insensitive. In
  109. * case a number is to be matched, leading zeros are permitted but not required.
  110. *
  111. * \%% The % character.
  112. *
  113. * \%a or %A
  114. * The name of the day of the week according to the current locale, in abbreviated form or
  115. * the full name.
  116. *
  117. * \%b or %B or %h
  118. * The month name according to the current locale, in abbreviated form or the full name.
  119. *
  120. * \%c The date and time representation for the current locale.
  121. *
  122. * \%C The century number (0-99).
  123. *
  124. * \%d or %e
  125. * The day of month (1-31).
  126. *
  127. * \%D Equivalent to %m/%d/%y. (This is the American style date, very confusing to non-Ameri‐
  128. * cans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard format
  129. * is %Y-%m-%d.)
  130. *
  131. * \%H The hour (0-23).
  132. *
  133. * \%I The hour on a 12-hour clock (1-12).
  134. *
  135. * \%j The day number in the year (1-366).
  136. *
  137. * \%m The month number (1-12).
  138. *
  139. * \%M The minute (0-59).
  140. *
  141. * \%n Arbitrary whitespace.
  142. *
  143. * \%p The locale's equivalent of AM or PM. (Note: there may be none.)
  144. *
  145. * \%r The 12-hour clock time (using the locale's AM or PM). In the POSIX locale equivalent to
  146. * \%I:%M:%S %p. If t_fmt_ampm is empty in the LC_TIME part of the current locale, then the
  147. * behavior is undefined.
  148. *
  149. * \%R Equivalent to %H:%M.
  150. *
  151. * \%S The second (0-60; 60 may occur for leap seconds; earlier also 61 was allowed).
  152. *
  153. * \%t Arbitrary whitespace.
  154. *
  155. * \%T Equivalent to %H:%M:%S.
  156. *
  157. * \%U The week number with Sunday the first day of the week (0-53). The first Sunday of Janu‐
  158. * ary is the first day of week 1.
  159. *
  160. * \%w The ordinal number of the day of the week (0-6), with Sunday = 0.
  161. *
  162. * \%W The week number with Monday the first day of the week (0-53). The first Monday of Janu‐
  163. * ary is the first day of week 1.
  164. *
  165. * \%x The date, using the locale's date format.
  166. *
  167. * \%X The time, using the locale's time format.
  168. *
  169. * \%y The year within century (0-99). When a century is not otherwise specified, values in the
  170. * range 69-99 refer to years in the twentieth century (1969-1999); values in the range
  171. * 00-68 refer to years in the twenty-first century (2000-2068).
  172. *
  173. * \%Y The year, including century (for example, 1991).
  174. *
  175. * Some field descriptors can be modified by the E or O modifier characters to indicate that an
  176. * alternative format or specification should be used. If the alternative format or specification
  177. * does not exist in the current locale, the unmodified field descriptor is used.
  178. *
  179. *
  180. * The E modifier specifies that the input string may contain alternative locale-dependent versions
  181. * of the date and time representation:
  182. *
  183. * \%Ec The locale's alternative date and time representation.
  184. *
  185. * \%EC The name of the base year (period) in the locale's alternative representation.
  186. *
  187. * \%Ex The locale's alternative date representation.
  188. *
  189. * \%EX The locale's alternative time representation.
  190. *
  191. * \%Ey The offset from %EC (year only) in the locale's alternative representation.
  192. *
  193. * \%EY The full alternative year representation.
  194. *
  195. * The O modifier specifies that the numerical input may be in an alternative locale-dependent for‐
  196. * mat:
  197. *
  198. * \%Od or %Oe
  199. * The day of the month using the locale's alternative numeric symbols; leading zeros are
  200. * permitted but not required.
  201. *
  202. * \%OH The hour (24-hour clock) using the locale's alternative numeric symbols.
  203. *
  204. * \%OI The hour (12-hour clock) using the locale's alternative numeric symbols.
  205. *
  206. * \%Om The month using the locale's alternative numeric symbols.
  207. *
  208. * \%OM The minutes using the locale's alternative numeric symbols.
  209. *
  210. * \%OS The seconds using the locale's alternative numeric symbols.
  211. *
  212. * \%OU The week number of the year (Sunday as the first day of the week) using the locale's
  213. * alternative numeric symbols.
  214. *
  215. * \%Ow The ordinal number of the day of the week (Sunday=0),
  216. * using the locale's alternative numeric symbols.
  217. *
  218. * \%OW The week number of the year (Monday as the first day of the week) using the locale's
  219. * alternative numeric symbols.
  220. *
  221. * \%Oy The year (offset from %C) using the locale's alternative numeric symbols.
  222. *
  223. *
  224. *@param ttail_t*
  225. *@param const char * format
  226. *@return 0 if no errors 1 if allready set -1 if compilation fails
  227. */
  228. int ttail_set_fmt(ttail_t*, const char*);
  229. /**@brief Set the extended regex flag
  230. *@param ttail ttail_t*
  231. *@return -1 on error else 0
  232. */
  233. int ttail_set_flag_re_ex(ttail_t*);
  234. /**@brief Set the case insensitive regex flag
  235. *@param ttail ttail_t*
  236. *@return -1 on error else 0
  237. */
  238. int ttail_set_flag_re_ci(ttail_t*);
  239. /**@brief Set dates min/max
  240. *
  241. *After the call dates are free and set to NULL except if error
  242. *@param ttail_t ttail instance
  243. *@param char*[2] dates {min,max} both can be NULL
  244. *@return -1 if error 0 else
  245. */
  246. int ttail_set_dates(ttail_t*, char*[2]);
  247. /**@brief Set a date using format or format detection
  248. *
  249. *@param ttail_t ttail instance
  250. *@param char*[2] dates {min,max} both can be NULL
  251. *@param int c the date id to handle
  252. *@return -1 if error 0 else
  253. */
  254. int _ttail_set_date_fmt(ttail_t*, char*, int);
  255. /**@brief Set a ttail_t date in relative format
  256. *
  257. * The function will set values for date_min or date_max given a relative
  258. * format (described bellow) from now (returned by localtime() )
  259. *
  260. *Relative date formats are "-#", an integer followed by a unit. Regcognized
  261. *units are :
  262. *- y[ear]
  263. *- M[onth]
  264. *- d[ay]
  265. *- h[our]
  266. *- m[in]
  267. *- s[ec]
  268. *
  269. *Examples :
  270. *- -#2s
  271. *- -#3Month
  272. *@param ttail_t ttail instance
  273. *@param char*[2] dates {min,max} both can be NULL
  274. *@param int c the date id to handle
  275. *@return -1 if error 0 else
  276. */
  277. int _ttail_set_date_relative(ttail_t*, char*, int);
  278. /**@brief Normalize dates using ttail.fmt
  279. *
  280. *When dates are relatives from now all the struct tm fields are set. In
  281. *logfiles year or seconds can be missing from date format. In those case we
  282. *have to set to -1 missing fields
  283. *@note must be called after arg parsing, ttail_set_dates() calls and
  284. *ttail_format_guess() calls
  285. *@param ttail_t* An initialized ttail_t
  286. *@return -1 if error 0 else
  287. */
  288. int ttail_norm_dates(ttail_t*);
  289. /**@brief Attempt to guess a dateformat
  290. *@param const char* date as a dtring
  291. *@param struct tm* if non NULL will be set to detected date
  292. *@return -1 if no guess else id in TTAIL_DEFAULT_FORMATS
  293. */
  294. int ttail_format_guess(const char*, struct tm*);
  295. void ttail_free(ttail_t*);
  296. #endif