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

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