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

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