libfuncs is collection of code (list, queue, circular buffer, io, logging, etc.). https://georgi.unixsol.org/programs/libfuncs/
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.

log.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * IPTV.bg Media Proxy
  3. * LOG functions
  4. *
  5. * Copyright (C) 2006-2008 Unix Solutions Ltd.
  6. * Written by Luben Karavelov (luben@unixsol.org)
  7. *
  8. */
  9. /* Needed for POLLRDHUP */
  10. #define _GNU_SOURCE 1
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdarg.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <netdb.h>
  17. #include <arpa/inet.h>
  18. #include <netinet/in.h>
  19. #include <netinet/tcp.h>
  20. #include <sys/socket.h>
  21. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <poll.h>
  25. #include <errno.h>
  26. #include <time.h>
  27. #include <pthread.h>
  28. #include "libfuncs.h"
  29. #include "io.h"
  30. #include "queue.h"
  31. #include "asyncdns.h"
  32. #include "log.h"
  33. static FILE *OUT_FD = NULL;
  34. struct logger {
  35. int use_stderr;
  36. int use_syslog;
  37. char *host_ident;
  38. char *log_host;
  39. int log_port;
  40. int log_sock;
  41. int dienow : 1,
  42. dying : 1;
  43. QUEUE *queue;
  44. pthread_t thread;
  45. };
  46. static void log_connect(struct logger *l) {
  47. struct sockaddr_in sockname;
  48. if (!l->use_syslog)
  49. return;
  50. while (1) {
  51. if (l->dying || l->dienow)
  52. break;
  53. int active = 1;
  54. int dret = async_resolve_host(l->log_host, l->log_port, &sockname, 500, &active);
  55. if (dret != 0) {
  56. log_perror("Could not resolve log host.", errno);
  57. sleep(2);
  58. continue;
  59. }
  60. l->log_sock = socket(PF_INET, SOCK_STREAM, 0);
  61. if (l->log_sock < 0) {
  62. log_perror("Could not create syslog socket", errno);
  63. sleep(2);
  64. continue;
  65. }
  66. if (connect(l->log_sock, (struct sockaddr *) &sockname, sizeof (sockname)) < 0) {
  67. log_perror("Could not connect to log host.", errno);
  68. shutdown_fd(&l->log_sock);
  69. sleep(2);
  70. continue;
  71. }
  72. int on = 1;
  73. setsockopt(l->log_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(int));
  74. break;
  75. }
  76. }
  77. static void * log_thread(void *param) {
  78. char date[64], logline[1024], *msg=NULL;
  79. struct logger *l = (struct logger *)param;
  80. if (l->log_sock < 0)
  81. log_connect(l);
  82. while (l && !l->dienow) {
  83. msg = queue_get(l->queue); // Waits...
  84. if (!msg)
  85. break;
  86. time_t now = time(NULL);
  87. memset(date, 0, sizeof(date));
  88. struct tm ltime;
  89. localtime_r(&now, &ltime);
  90. strftime(date, sizeof(date)-1, "%b %d %H:%M:%S", &ltime);
  91. memset(logline, 0, sizeof(logline));
  92. snprintf(logline, sizeof(logline)-1, "<30>%s host %s: %s", date, l->host_ident, msg);
  93. logline[sizeof(logline)-2] = '\n';
  94. logline[sizeof(logline)-1] = '\0';
  95. if (l->use_stderr)
  96. fprintf(OUT_FD, "%s", logline+4);
  97. while (l->use_syslog) {
  98. struct pollfd fdset[1];
  99. int fdready;
  100. do {
  101. fdset[0].fd = l->log_sock;
  102. fdset[0].events = POLLOUT | POLLERR | POLLHUP | POLLNVAL | POLLRDHUP;
  103. fdready = poll(fdset, 1, 5 * 1000);
  104. } while (fdready == -1 && errno == EINTR);
  105. if (fdready < 1 || (fdready == 1 && fdset[0].revents != POLLOUT)) { /* Timeout || error */
  106. do { /* Try to reconnect to log host */
  107. if (l->dienow)
  108. goto OUT;
  109. LOGf("ERROR: Lost connection to log server (%s), fd: %i\n", fdready == 1 ? "poll error" : "timeout", l->log_sock);
  110. shutdown_fd(&l->log_sock);
  111. log_connect(l);
  112. sleep(2);
  113. } while (l->log_sock < 0);
  114. } else {
  115. if (fdwrite(l->log_sock, logline, strlen(logline)) > 0)
  116. break;
  117. else
  118. if (l->dienow)
  119. goto OUT;
  120. }
  121. }
  122. FREE(msg);
  123. }
  124. OUT:
  125. FREE(msg);
  126. pthread_exit(0);
  127. }
  128. static struct logger *logger = NULL;
  129. void log_init(char *host_ident, int use_syslog, int use_stderr, char *log_host, int log_port) {
  130. logger = calloc(1, sizeof(struct logger));
  131. logger->queue = queue_new();
  132. logger->host_ident = strdup(host_ident);
  133. if (log_host)
  134. logger->log_host = strdup(log_host);
  135. logger->log_port = log_port;
  136. logger->log_sock = -1;
  137. logger->use_syslog = use_syslog;
  138. logger->use_stderr = use_stderr;
  139. pthread_create(&logger->thread, NULL , &log_thread, logger);
  140. }
  141. void log_set_out_fd(FILE *new_out_fd) {
  142. OUT_FD = new_out_fd;
  143. }
  144. void log_close() {
  145. logger->dying = 1;
  146. int count = 0;
  147. while (logger->queue->items && count++ < 250)
  148. usleep(1000);
  149. logger->dienow = 1;
  150. queue_wakeup(logger->queue);
  151. pthread_join(logger->thread, NULL);
  152. shutdown_fd(&logger->log_sock);
  153. queue_free(&logger->queue);
  154. FREE(logger->host_ident);
  155. FREE(logger->log_host);
  156. FREE(logger);
  157. }
  158. void LOG(const char *msg) {
  159. if (OUT_FD == NULL)
  160. OUT_FD = stderr;
  161. if (!logger || logger->dying) {
  162. fprintf(OUT_FD, "%s", msg);
  163. } else {
  164. queue_add(logger->queue, strdup(msg));
  165. }
  166. }
  167. void LOGf(const char *fmt, ...) {
  168. char msg[1024];
  169. va_list args;
  170. va_start(args, fmt);
  171. vsnprintf(msg, sizeof(msg)-1, fmt, args);
  172. va_end(args);
  173. msg[sizeof(msg)-2] = '\n';
  174. msg[sizeof(msg)-1] = '\0';
  175. LOG(msg);
  176. }
  177. void log_perror(const char *message, int _errno) {
  178. char msg[1024];
  179. snprintf(msg, sizeof(msg)-1, "PERROR: %s | %s\n", message, strerror(_errno));
  180. msg[sizeof(msg)-2] = '\n';
  181. msg[sizeof(msg)-1] = '\0';
  182. LOG(msg);
  183. }