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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include "ttail.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_add_logfile(ttail_t* res, const char* filename)
  120. {
  121. void *tmp;
  122. FILE *fp;
  123. char *fname;
  124. int ret, i;
  125. for(i=0; i<res->logfile_sz; i++)
  126. {
  127. if(strcmp(filename, res->logfile_name[i]) == 0)
  128. {
  129. fprintf(stderr, "File '%s' allready added\n",
  130. filename);
  131. return -1;
  132. }
  133. }
  134. res->logfile_sz++;
  135. tmp = res->logfile;
  136. res->logfile = realloc(res->logfile,
  137. sizeof(FILE*)*res->logfile_sz);
  138. ret = 0;
  139. if(!res->logfile)
  140. {
  141. perror("Unable to allocate memory for logfiles");
  142. res->logfile = tmp;
  143. goto ttail_add_logfile_fpalloc_err;
  144. }
  145. fp = fopen(filename, "r");
  146. if(!fp)
  147. {
  148. fprintf(stderr, "Unable to open file : %s\n", filename);
  149. ret = 1;
  150. }
  151. res->logfile[res->logfile_sz-1] = fp;
  152. tmp = res->logfile_name;
  153. res->logfile_name = realloc(res->logfile_name,
  154. sizeof(char*)*res->logfile_sz);
  155. if(!res->logfile_name)
  156. {
  157. perror("Unable to allocate memory for logfiles");
  158. res->logfile_name = tmp;
  159. goto ttail_add_logfile_fnalloc_err;
  160. }
  161. fname = malloc(sizeof(char)*(strlen(filename)+1));
  162. if(!fname)
  163. {
  164. perror("Unable to allocate memory for logfiles");
  165. goto ttail_add_logfile_fnalloc_err;
  166. }
  167. strcpy(fname, filename);
  168. res->logfile_name[res->logfile_sz-1] = fname;
  169. return ret;
  170. ttail_add_logfile_fnalloc_err:
  171. fclose(res->logfile[res->logfile_sz-2]);
  172. ttail_add_logfile_fpalloc_err:
  173. res->logfile_sz--;
  174. return -1;
  175. }
  176. int ttail_set_prefix(ttail_t* res, const char* regex)
  177. {
  178. int ret;
  179. char *re_errbuff;
  180. size_t re_errbuff_sz;
  181. if(res->flag & TTAIL_FLAG_PREFIX)
  182. {
  183. fprintf(stderr, "Regex prefix allready set");
  184. return 1;
  185. }
  186. res->flag |= TTAIL_FLAG_PREFIX;
  187. ret = regcomp(&res->date_prefix, regex, 0);
  188. if(!ret)
  189. {
  190. return 0;
  191. }
  192. /*compilation error */
  193. res->flag ^= TTAIL_FLAG_PREFIX;
  194. re_errbuff_sz = regerror(ret,
  195. &res->date_prefix, NULL, 0);
  196. re_errbuff = malloc(
  197. sizeof(char)*re_errbuff_sz);
  198. if(!re_errbuff)
  199. {
  200. perror("Failed to allocate memory for regex compilation \
  201. error message");
  202. goto ttail_set_prefix_err;
  203. }
  204. regerror(ret, &res->date_prefix,
  205. re_errbuff,
  206. sizeof(char)*re_errbuff_sz);
  207. fprintf(stderr, "Regex compilation fails : %s", re_errbuff);
  208. free(re_errbuff);
  209. ttail_set_prefix_err:
  210. return -1;
  211. }
  212. int ttail_set_dates(ttail_t* res, char* dates[2])
  213. {
  214. int c, ret;
  215. char *date;
  216. for(c=0;c<2;c++)
  217. {
  218. memset(c==0?&(res->date_min):&(res->date_max), 0, \
  219. sizeof(struct tm));
  220. if(!dates[c])
  221. {
  222. continue;
  223. }
  224. if(!(res->flag & TTAIL_FLAG_FORMAT))
  225. {
  226. /* no format specified */
  227. ret = ttail_format_guess(res, dates[c],
  228. c==0?&(res->date_min):&(res->date_max));
  229. if(res < 0)
  230. {
  231. fprintf(stderr, "Unable to guess format for \
  232. date '%s'\n", dates[c]);
  233. return -1;
  234. }
  235. res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
  236. continue;
  237. }
  238. date = strptime(dates[c], res->fmt,
  239. c==0?&(res->date_min):&(res->date_max));
  240. if(!date)
  241. {
  242. fprintf(stderr, "Unable to parse date-%s '%s' with \
  243. format '%s'\n", c==0?"min":"max", dates[c], res->fmt);
  244. return -1;
  245. }
  246. else if(*date != '\0')
  247. {
  248. fprintf(stderr, "Leading caracters for date-%s : %s\n",\
  249. c==0?"min":"max", date);
  250. }
  251. res->flag |= c?TTAIL_FLAG_DATE_MAX:TTAIL_FLAG_DATE_MIN;
  252. }
  253. if(dates[0]) { free(dates[0]); dates[0] = NULL; }
  254. if(dates[1]) { free(dates[1]); dates[1] = NULL; }
  255. return 0;
  256. }
  257. int ttail_format_guess(ttail_t* t, const char* date_str, struct tm* tm)
  258. {
  259. int i, res;
  260. char *res_ret, *ret;
  261. char *fmt[] = TTAIL_DEFAULT_FORMATS;
  262. struct tm dte;
  263. memset(&dte, 0, sizeof(struct tm));
  264. if(t->flag & TTAIL_FLAG_FORMAT)
  265. {
  266. fprintf(stderr, "Format allready set\n");
  267. return -2;
  268. }
  269. res = -1;
  270. res_ret = NULL;
  271. i=0;
  272. while(fmt[i])
  273. {
  274. ret = strptime(date_str, fmt[i], &dte);
  275. if(ret)
  276. {
  277. if(!res_ret || strlen(res_ret) > strlen(ret))
  278. {
  279. res_ret = ret;
  280. res = i;
  281. if(tm)
  282. {
  283. memcpy(tm, &dte, sizeof(struct tm));
  284. }
  285. if(t->fmt)
  286. {
  287. free(t->fmt);
  288. }
  289. t->fmt = malloc(
  290. sizeof(char)*(strlen(fmt[i])+1));
  291. if(!t->fmt)
  292. {
  293. perror("Unable to allocate memory\
  294. for date format");
  295. t->flag ^= TTAIL_FLAG_FORMAT;
  296. return -1;
  297. }
  298. strcpy(t->fmt, fmt[i]);
  299. t->flag |= TTAIL_FLAG_FORMAT;
  300. }
  301. }
  302. i++;
  303. }
  304. if(!res_ret)
  305. {
  306. if(tm)
  307. {
  308. memset(tm, 0, sizeof(struct tm));
  309. }
  310. return -1;
  311. }
  312. return res;
  313. }
  314. void ttail_free(ttail_t* t)
  315. {
  316. size_t i;
  317. for(i=0; i<t->logfile_sz; i++)
  318. {
  319. if(t->logfile[i])
  320. {
  321. fclose(t->logfile[i]);
  322. }
  323. if(t->logfile_name[i])
  324. {
  325. free(t->logfile_name[i]);
  326. }
  327. }
  328. t->logfile_sz = 0;
  329. if(t->fmt != NULL)
  330. {
  331. free(t->fmt);
  332. }
  333. free(t);
  334. optind=0;
  335. }