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.c 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #include "ttail_init.h"
  2. ttail_t *ttail_init(int argc, char **argv)
  3. {
  4. ttail_t *res;
  5. int c, opt_i, ret;
  6. char *dates[2] = { NULL, NULL };
  7. char *date;
  8. res = malloc(sizeof(ttail_t));
  9. if(!res)
  10. {
  11. perror("Unable to allocate memory");
  12. goto ttail_init_alloc_err;
  13. }
  14. opterr = 0;
  15. optind = 0;
  16. res->verbose = 0;
  17. res->fmt = NULL;
  18. res->flag = 0;
  19. res->logfile = NULL;
  20. res->logfile_name = NULL;
  21. res->logfile_sz = 0;
  22. res->prefix_sz = -1;
  23. memset(&(res->date_min), 0, sizeof(struct tm));
  24. memset(&(res->date_max), 0, sizeof(struct tm));
  25. while(1)
  26. {
  27. static struct option long_options[] = TTAIL_LONG_OPT;
  28. c = getopt_long(argc, argv, TTAIL_SHORT_OPT, long_options, &opt_i);
  29. if(c == -1)
  30. break;
  31. switch(c)
  32. {
  33. case 'v':
  34. res->verbose++;
  35. break;
  36. case 'r':
  37. if(ttail_set_prefix(res, optarg))
  38. {
  39. goto ttail_init_err;
  40. }
  41. break;
  42. case 'p':
  43. if(res->flag & TTAIL_FLAG_PREFIX)
  44. {
  45. fprintf(stderr, "Preffix allready \
  46. set\n");
  47. goto ttail_init_err;
  48. }
  49. res->flag |= TTAIL_FLAG_PREFIX;
  50. res->prefix_sz = atoi(optarg);
  51. if(res->prefix_sz <= 0)
  52. {
  53. fprintf(stderr, "Prefix size have \
  54. to be > 0");
  55. goto ttail_init_err;
  56. }
  57. break;
  58. case 'E':
  59. case 'I':
  60. /**@todo checks */
  61. if(res->flag & TTAIL_FLAG_PREFIX)
  62. {
  63. fprintf(stderr, "-E & -I options \
  64. have to be set BEFORE -r option\n");
  65. goto ttail_init_err;
  66. }
  67. res->flag |= c=='E'?TTAIL_FLAG_EXTENDED_RE:\
  68. REG_ICASE;
  69. break;
  70. case 'f':
  71. if(res->flag & TTAIL_FLAG_FORMAT)
  72. {
  73. fprintf(stderr,"Multiple date format \
  74. given\n");
  75. goto ttail_init_err;
  76. }
  77. res->fmt = malloc(
  78. sizeof(char)*(strlen(optarg)+1));
  79. if(!res->fmt)
  80. {
  81. perror("Unable to allocate memory\
  82. for date format");
  83. goto ttail_init_err;
  84. }
  85. strcpy(res->fmt, optarg);
  86. res->flag |= TTAIL_FLAG_FORMAT;
  87. break;
  88. case 'l':
  89. fprintf(stderr,"-l with %s\n", optarg);
  90. ret = ttail_add_logfile(res, optarg);
  91. if(ret < 0)
  92. {
  93. goto ttail_init_err;
  94. }
  95. break;
  96. case 'd':
  97. case 'm':
  98. date = malloc(sizeof(char)*(strlen(optarg)+1));
  99. if(!date)
  100. {
  101. goto ttail_init_err;
  102. }
  103. strcpy(date, optarg);
  104. dates[c=='d'?0:1] = date;
  105. break;
  106. case '?':
  107. optind--;
  108. goto init_badarg;
  109. break;
  110. default:
  111. fprintf(stderr, "Bad argument\n");
  112. goto init_badarg;
  113. }
  114. }
  115. if(optind < argc)
  116. {
  117. init_badarg:
  118. fprintf(stderr, "bad option :");
  119. while(optind < argc)
  120. {
  121. fprintf(stderr, argv[optind++]);
  122. }
  123. fprintf(stderr, "\n");
  124. goto ttail_init_err;
  125. }
  126. if(ttail_set_dates(res, dates) < 0)
  127. {
  128. goto ttail_init_err;
  129. }
  130. return res;
  131. ttail_init_err:
  132. if(dates[0]) { free(dates[0]); }
  133. if(dates[1]) { free(dates[1]); }
  134. ttail_free(res);
  135. ttail_init_alloc_err:
  136. return NULL;
  137. }
  138. int ttail_init_check(ttail_t* t)
  139. {
  140. if(t->flag & TTAIL_FLAG_DATE_MAX)
  141. {
  142. fprintf(stderr, "Warning : date-max not yet implemented\n");
  143. }
  144. if(!(t->flag & TTAIL_FLAG_DATE_MIN))
  145. {
  146. fprintf(stderr, "--date-min -d is mandatory\n");
  147. return -1;
  148. }
  149. if(!(t->flag & TTAIL_FLAG_FORMAT))
  150. {
  151. fprintf(stderr, "No date format set nor detected. Abording.\n");
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. int ttail_add_logfile(ttail_t* res, const char* filename)
  157. {
  158. void *tmp;
  159. FILE *fp;
  160. char *fname;
  161. int ret, i;
  162. for(i=0; i<res->logfile_sz; i++)
  163. {
  164. if(strcmp(filename, res->logfile_name[i]) == 0)
  165. {
  166. fprintf(stderr, "File '%s' allready added\n",
  167. filename);
  168. return -1;
  169. }
  170. }
  171. res->logfile_sz++;
  172. tmp = res->logfile;
  173. res->logfile = realloc(res->logfile,
  174. sizeof(FILE*)*res->logfile_sz);
  175. ret = 0;
  176. if(!res->logfile)
  177. {
  178. perror("Unable to allocate memory for logfiles");
  179. res->logfile = tmp;
  180. goto ttail_add_logfile_fpalloc_err;
  181. }
  182. fp = fopen(filename, "r");
  183. if(!fp)
  184. {
  185. fprintf(stderr, "Unable to open file : %s\n", filename);
  186. ret = 1;
  187. }
  188. res->logfile[res->logfile_sz-1] = fp;
  189. tmp = res->logfile_name;
  190. res->logfile_name = realloc(res->logfile_name,
  191. sizeof(char*)*res->logfile_sz);
  192. if(!res->logfile_name)
  193. {
  194. perror("Unable to allocate memory for logfiles");
  195. res->logfile_name = tmp;
  196. goto ttail_add_logfile_fnalloc_err;
  197. }
  198. fname = malloc(sizeof(char)*(strlen(filename)+1));
  199. if(!fname)
  200. {
  201. perror("Unable to allocate memory for logfiles");
  202. goto ttail_add_logfile_fnalloc_err;
  203. }
  204. strcpy(fname, filename);
  205. res->logfile_name[res->logfile_sz-1] = fname;
  206. return ret;
  207. ttail_add_logfile_fnalloc_err:
  208. fclose(res->logfile[res->logfile_sz-2]);
  209. ttail_add_logfile_fpalloc_err:
  210. res->logfile_sz--;
  211. return -1;
  212. }
  213. int ttail_set_prefix(ttail_t* res, const char* regex)
  214. {
  215. int ret, cflags;
  216. char *re_errbuff;
  217. size_t re_errbuff_sz;
  218. if(res->flag & TTAIL_FLAG_PREFIX)
  219. {
  220. fprintf(stderr, "Regex prefix allready set");
  221. return 1;
  222. }
  223. res->flag |= TTAIL_FLAG_PREFIX;
  224. cflags = 0; /** @todo checks */
  225. if(res->flag & TTAIL_FLAG_EXTENDED_RE)
  226. {
  227. cflags |= REG_EXTENDED;
  228. }
  229. if(res->flag & TTAIL_FLAG_EXTENDED_CI)
  230. {
  231. cflags |= REG_ICASE;
  232. }
  233. ret = regcomp(&res->date_prefix, regex, cflags);
  234. if(!ret)
  235. {
  236. return 0;
  237. }
  238. /*compilation error */
  239. res->flag ^= TTAIL_FLAG_PREFIX;
  240. re_errbuff_sz = regerror(ret,
  241. &res->date_prefix, NULL, 0);
  242. re_errbuff = malloc(
  243. sizeof(char)*re_errbuff_sz);
  244. if(!re_errbuff)
  245. {
  246. perror("Failed to allocate memory for regex compilation \
  247. error message");
  248. goto ttail_set_prefix_err;
  249. }
  250. regerror(ret, &res->date_prefix,
  251. re_errbuff,
  252. sizeof(char)*re_errbuff_sz);
  253. fprintf(stderr, "Regex compilation fails : %s", re_errbuff);
  254. free(re_errbuff);
  255. ttail_set_prefix_err:
  256. return -1;
  257. }
  258. int ttail_set_dates(ttail_t* res, char* dates[2])
  259. {
  260. int c, ret;
  261. char *date;
  262. for(c=0;c<2;c++)
  263. {
  264. memset(c==0?&(res->date_min):&(res->date_max), 0, \
  265. sizeof(struct tm));
  266. if(!dates[c])
  267. {
  268. continue;
  269. }
  270. if(!(res->flag & TTAIL_FLAG_FORMAT))
  271. {
  272. /* no format specified */
  273. ret = ttail_format_guess(res, dates[c],
  274. c==0?&(res->date_min):&(res->date_max));
  275. if(ret < 0)
  276. {
  277. fprintf(stderr, "Unable to guess format for \
  278. date '%s'\n", dates[c]);
  279. return -1;
  280. }
  281. res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
  282. continue;
  283. }
  284. date = strptime(dates[c], res->fmt,
  285. c==0?&(res->date_min):&(res->date_max));
  286. if(!date)
  287. {
  288. fprintf(stderr, "Unable to parse date-%s '%s' with \
  289. format '%s'\n", c==0?"min":"max", dates[c], res->fmt);
  290. return -1;
  291. }
  292. else if(*date != '\0')
  293. {
  294. fprintf(stderr, "Leading caracters for date-%s : %s\n",\
  295. c==0?"min":"max", date);
  296. }
  297. res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
  298. }
  299. if(dates[0]) { free(dates[0]); dates[0] = NULL; }
  300. if(dates[1]) { free(dates[1]); dates[1] = NULL; }
  301. return 0;
  302. }
  303. int ttail_format_guess(ttail_t* t, const char* date_str, struct tm* tm)
  304. {
  305. int i, res;
  306. char *res_ret, *ret;
  307. char *fmt[] = TTAIL_DEFAULT_FORMATS;
  308. struct tm dte;
  309. memset(&dte, 0, sizeof(struct tm));
  310. if(t->flag & TTAIL_FLAG_FORMAT)
  311. {
  312. fprintf(stderr, "Format allready set\n");
  313. return -2;
  314. }
  315. res = -1;
  316. res_ret = NULL;
  317. i=0;
  318. while(fmt[i])
  319. {
  320. ret = strptime(date_str, fmt[i], &dte);
  321. if(ret)
  322. {
  323. if(!res_ret || strlen(res_ret) > strlen(ret))
  324. {
  325. res_ret = ret;
  326. res = i;
  327. if(tm)
  328. {
  329. memcpy(tm, &dte, sizeof(struct tm));
  330. }
  331. if(t->fmt)
  332. {
  333. free(t->fmt);
  334. }
  335. t->fmt = malloc(
  336. sizeof(char)*(strlen(fmt[i])+1));
  337. if(!t->fmt)
  338. {
  339. perror("Unable to allocate memory\
  340. for date format");
  341. t->flag ^= TTAIL_FLAG_FORMAT;
  342. return -1;
  343. }
  344. strcpy(t->fmt, fmt[i]);
  345. t->flag |= TTAIL_FLAG_FORMAT;
  346. }
  347. }
  348. i++;
  349. }
  350. if(!res_ret)
  351. {
  352. if(tm)
  353. {
  354. memset(tm, 0, sizeof(struct tm));
  355. }
  356. return -1;
  357. }
  358. return res;
  359. }
  360. void ttail_free(ttail_t* t)
  361. {
  362. size_t i;
  363. if(t->flag & TTAIL_FLAG_PREFIX && t->prefix_sz < 0)
  364. {
  365. regfree(&(t->date_prefix));
  366. }
  367. for(i=0; i<t->logfile_sz; i++)
  368. {
  369. if(t->logfile[i])
  370. {
  371. fclose(t->logfile[i]);
  372. }
  373. if(t->logfile_name[i])
  374. {
  375. free(t->logfile_name[i]);
  376. }
  377. }
  378. free(t->logfile);
  379. free(t->logfile_name);
  380. t->logfile_sz = 0;
  381. if(t->fmt != NULL)
  382. {
  383. free(t->fmt);
  384. }
  385. free(t);
  386. optind=0;
  387. }