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

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