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_search_std.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This file is part of Ttail.
  3. *
  4. * Ttail is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * any later version.
  8. *
  9. * Ttail is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with Ttail. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "ttail_search_std.h"
  18. int _ttail_search_closest_std_init(ttail_t* t)
  19. {
  20. t->session = (ttail_search_t*)malloc(sizeof(ttail_search_stdin_t));
  21. if(!t->session)
  22. {
  23. perror("Unable to allocate memory for search session");
  24. return -1;
  25. }
  26. t->session->std.buff = NULL;
  27. t->session->std.buff_sz = 0;
  28. return 0;
  29. }
  30. int _ttail_search_closest_stdin(ttail_t* t)
  31. {
  32. struct tm tm;
  33. ssize_t rd_sz;
  34. int ret, tmp;
  35. if(_ttail_search_closest_std_init(t) < 0)
  36. {
  37. return -1;
  38. }
  39. if(!(t->flag & TTAIL_FLAG_DATE_MIN))
  40. {
  41. return 0;
  42. }
  43. while((rd_sz = ttail_std_getline(t)) > 0)
  44. {
  45. ttail_tm_init(&tm);
  46. ret = ttail_logline2date(t, ttail_std_getline_buff(t), &tm);
  47. if(ret < 0)
  48. {
  49. return -1;
  50. }
  51. else if(ret > 0)
  52. {
  53. continue;
  54. }
  55. ret = ttail_tm_cmp(&tm, &(t->date_min));
  56. if(ret >= 0)
  57. {
  58. //buffer contains the first line to print
  59. return 0;
  60. }
  61. }
  62. if(rd_sz < 0)
  63. {
  64. tmp = errno;
  65. ret = feof(stdin);
  66. if(ret <= 0)
  67. {
  68. errno = tmp;
  69. perror("Error while reading loglines from stdin");
  70. return -1;
  71. }
  72. }
  73. return 0;
  74. }
  75. void _ttail_search_print_stdin(ttail_t* t, int fd)
  76. {
  77. struct tm tm;
  78. ssize_t rd_sz;
  79. int ret, tmp;
  80. rd_sz = strlen(ttail_std_getline_buff(t));
  81. if(!(t->flag & TTAIL_FLAG_DATE_MAX))
  82. {
  83. do
  84. {
  85. ret = write(1, ttail_std_getline_buff(t),
  86. sizeof(char)*rd_sz);
  87. if(ret < 0)
  88. {
  89. perror("Unable to write to stdout");
  90. return;
  91. }
  92. }
  93. while((rd_sz = ttail_std_getline(t)) > 0);
  94. }
  95. else
  96. {
  97. do
  98. {
  99. ttail_tm_init(&tm);
  100. ret = ttail_logline2date(t, ttail_std_getline_buff(t),
  101. &tm);
  102. //dropping errors
  103. if(ret == 0)
  104. {
  105. ret = ttail_tm_cmp(&tm, &(t->date_max));
  106. if(ret > 0)
  107. {
  108. return;
  109. }
  110. }
  111. ret = write(1, ttail_std_getline_buff(t),
  112. sizeof(char)*rd_sz);
  113. if(ret < 0)
  114. {
  115. perror("Unable to write to stdout");
  116. return;
  117. }
  118. }
  119. while((rd_sz = ttail_std_getline(t)) > 0);
  120. }
  121. if(rd_sz < 0)
  122. {
  123. tmp = errno;
  124. ret = feof(stdin);
  125. if(ret <= 0)
  126. {
  127. errno = tmp;
  128. perror("Error while reading loglines from stdin");
  129. }
  130. }
  131. return;
  132. }
  133. void _ttail_search_stdin_free(ttail_t* t)
  134. {
  135. if(t->session->std.buff)
  136. {
  137. free(t->session->std.buff);
  138. }
  139. }