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 9.3KB

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