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.2KB

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