timed tail for logfiles. Display loglines given a minimum date and/or a maximum date.
c
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. while(1)
  23. {
  24. static struct option long_options[] =
  25. {
  26. {"verbose", no_argument, 0, 'v'},
  27. {"re-prefix", required_argument, 0, 'p'},
  28. {"date-format", required_argument, 0, 'f'},
  29. {"date-min", required_argument, 0, 'd'},
  30. {"date-max", required_argument, 0, 'm'},
  31. {"logfile", required_argument, 0, 'l'}
  32. };
  33. opt_i = 0;
  34. c = getopt_long(argc, argv, "p:f:d:l:m:", long_options, &opt_i);
  35. if(c == -1)
  36. break;
  37. switch(c)
  38. {
  39. case 'v':
  40. res->verbose++;
  41. break;
  42. case 'p':
  43. if(ttail_set_prefix(res, optarg) < 0)
  44. {
  45. goto ttail_init_err;
  46. }
  47. break;
  48. case 'f':
  49. if(res->fmt)
  50. {
  51. fprintf(stderr,"Warning : multiple\
  52. date format given\n");
  53. }
  54. res->fmt = malloc(
  55. sizeof(char)*(strlen(optarg)+1));
  56. if(!res->fmt)
  57. {
  58. perror("Unable to allocate memory\
  59. for date format");
  60. goto ttail_init_err;
  61. }
  62. strcpy(res->fmt, optarg);
  63. break;
  64. case 'l':
  65. fprintf(stderr,"-l with %s\n", optarg);
  66. ret = ttail_add_logfile(res, optarg);
  67. if(ret < 0)
  68. {
  69. goto ttail_init_err;
  70. }
  71. break;
  72. case 'd':
  73. case 'm':
  74. date = malloc(sizeof(char)*(strlen(optarg)+1));
  75. if(!date)
  76. {
  77. goto ttail_init_err;
  78. }
  79. strcpy(date, optarg);
  80. dates[c=='d'?0:1] = date;
  81. break;
  82. case '?':
  83. fprintf(stderr, "Bad argument : %s\n", argv[optind]);
  84. goto init_badarg;
  85. break;
  86. default:
  87. fprintf(stderr, "Bad argument : 0%o\n", c);
  88. goto ttail_init_err;
  89. }
  90. }
  91. if(optind < argc)
  92. {
  93. init_badarg:
  94. fprintf(stderr, "bad option :");
  95. while(optind < argc)
  96. {
  97. fprintf(stderr, argv[optind++]);
  98. }
  99. fprintf(stderr, "\n");
  100. }
  101. for(c=0;c<c;c++)
  102. {
  103. if(!dates[c])
  104. {
  105. continue;
  106. }
  107. date = strptime(dates[c], res->fmt,
  108. c==0?&(res->date_min):&(res->date_max));
  109. if(!date)
  110. {
  111. fprintf(stderr, "Unable to parse date-%s '%s' with \
  112. format '%s'\n", c==0?"min":"max", dates[c], res->fmt);
  113. goto ttail_init_err;
  114. }
  115. else if(*date != '\0')
  116. {
  117. fprintf(stderr, "Leading caracters for date-%s : %s\n",\
  118. c==0?"min":"max", date);
  119. }
  120. free(dates[c]);
  121. }
  122. return res;
  123. ttail_init_err:
  124. if(dates[0]) { free(dates[0]); }
  125. if(dates[1]) { free(dates[1]); }
  126. ttail_free(res);
  127. ttail_init_alloc_err:
  128. return NULL;
  129. }
  130. int ttail_add_logfile(ttail_t* res, const char* filename)
  131. {
  132. void *tmp;
  133. FILE *fp;
  134. char *fname;
  135. int ret, i;
  136. for(i=0; i<res->logfile_sz; i++)
  137. {
  138. if(strcmp(filename, res->logfile_name[i]) == 0)
  139. {
  140. fprintf(stderr, "File '%s' allready added\n",
  141. filename);
  142. return -1;
  143. }
  144. }
  145. res->logfile_sz++;
  146. tmp = res->logfile;
  147. res->logfile = realloc(res->logfile,
  148. sizeof(FILE*)*res->logfile_sz);
  149. ret = 0;
  150. if(!res->logfile)
  151. {
  152. perror("Unable to allocate memory for logfiles");
  153. res->logfile = tmp;
  154. goto ttail_add_logfile_fpalloc_err;
  155. }
  156. fp = fopen(filename, "r");
  157. if(!fp)
  158. {
  159. fprintf(stderr, "Unable to open file : %s\n", filename);
  160. ret = 1;
  161. }
  162. res->logfile[res->logfile_sz-1] = fp;
  163. tmp = res->logfile_name;
  164. res->logfile_name = realloc(res->logfile_name,
  165. sizeof(char*)*res->logfile_sz);
  166. if(!res->logfile_name)
  167. {
  168. perror("Unable to allocate memory for logfiles");
  169. res->logfile_name = tmp;
  170. goto ttail_add_logfile_fnalloc_err;
  171. }
  172. fname = malloc(sizeof(char)*(strlen(filename)+1));
  173. if(!fname)
  174. {
  175. perror("Unable to allocate memory for logfiles");
  176. goto ttail_add_logfile_fnalloc_err;
  177. }
  178. strcpy(fname, filename);
  179. res->logfile_name[res->logfile_sz-1] = fname;
  180. return ret;
  181. ttail_add_logfile_fnalloc_err:
  182. fclose(res->logfile[res->logfile_sz-2]);
  183. ttail_add_logfile_fpalloc_err:
  184. res->logfile_sz--;
  185. return -1;
  186. }
  187. int ttail_set_prefix(ttail_t* res, const char* regex)
  188. {
  189. int ret;
  190. char *re_errbuff;
  191. size_t re_errbuff_sz;
  192. if(res->flag & TTAIL_FLAG_PREFIX)
  193. {
  194. fprintf(stderr, "Warning : Regex prefix allready set");
  195. return 0;
  196. }
  197. res->flag |= TTAIL_FLAG_PREFIX;
  198. ret = regcomp(&res->date_prefix, regex, 0);
  199. if(!ret)
  200. {
  201. return 0;
  202. }
  203. /*compilation error */
  204. re_errbuff_sz = regerror(ret,
  205. &res->date_prefix, NULL, 0);
  206. re_errbuff = malloc(
  207. sizeof(char)*re_errbuff_sz);
  208. if(!re_errbuff)
  209. {
  210. perror("Failed to allocate memory for regex compilation \
  211. error message");
  212. goto ttail_set_prefix_err;
  213. }
  214. regerror(ret, &res->date_prefix,
  215. re_errbuff,
  216. sizeof(char)*re_errbuff_sz);
  217. fprintf(stderr, "Regex compilation fails : %s", re_errbuff);
  218. free(re_errbuff);
  219. ttail_set_prefix_err:
  220. return -1;
  221. }
  222. void ttail_free(ttail_t* t)
  223. {
  224. size_t i;
  225. for(i=0; i<t->logfile_sz; i++)
  226. {
  227. if(t->logfile[i])
  228. {
  229. fclose(t->logfile[i]);
  230. }
  231. if(t->logfile_name[i])
  232. {
  233. free(t->logfile_name[i]);
  234. }
  235. }
  236. t->logfile_sz = 0;
  237. if(t->fmt != NULL)
  238. {
  239. free(t->fmt);
  240. }
  241. free(t);
  242. optind=0;
  243. }