log.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* $OpenBSD: err.c,v 1.2 2002/06/25 15:50:15 mickey Exp $ */
  2. /*
  3. * log.c
  4. *
  5. * Based on err.c, which was adapted from OpenBSD libc *err* *warn* code.
  6. *
  7. * Copyright (c) 2005-2012 Niels Provos and Nick Mathewson
  8. *
  9. * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  10. *
  11. * Copyright (c) 1993
  12. * The Regents of the University of California. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of the University nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. */
  38. #include "event2/event-config.h"
  39. #include "evconfig-private.h"
  40. #ifdef _WIN32
  41. #include <winsock2.h>
  42. #define WIN32_LEAN_AND_MEAN
  43. #include <windows.h>
  44. #undef WIN32_LEAN_AND_MEAN
  45. #endif
  46. #include <sys/types.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <stdarg.h>
  50. #include <string.h>
  51. #include <errno.h>
  52. #include "event2/event.h"
  53. #include "event2/util.h"
  54. #include "log-internal.h"
  55. static void event_log(int severity, const char *msg);
  56. static void event_exit(int errcode) EV_NORETURN;
  57. static event_fatal_cb fatal_fn = NULL;
  58. #ifdef EVENT_DEBUG_LOGGING_ENABLED
  59. #ifdef USE_DEBUG
  60. #define DEFAULT_MASK EVENT_DBG_ALL
  61. #else
  62. #define DEFAULT_MASK 0
  63. #endif
  64. EVENT2_EXPORT_SYMBOL ev_uint32_t event_debug_logging_mask_ = DEFAULT_MASK;
  65. #endif /* EVENT_DEBUG_LOGGING_ENABLED */
  66. void
  67. event_enable_debug_logging(ev_uint32_t which)
  68. {
  69. #ifdef EVENT_DEBUG_LOGGING_ENABLED
  70. event_debug_logging_mask_ = which;
  71. #endif
  72. }
  73. void
  74. event_set_fatal_callback(event_fatal_cb cb)
  75. {
  76. fatal_fn = cb;
  77. }
  78. static void
  79. event_exit(int errcode)
  80. {
  81. if (fatal_fn) {
  82. fatal_fn(errcode);
  83. exit(errcode); /* should never be reached */
  84. } else if (errcode == EVENT_ERR_ABORT_)
  85. abort();
  86. else
  87. exit(errcode);
  88. }
  89. void
  90. event_err(int eval, const char *fmt, ...)
  91. {
  92. va_list ap;
  93. va_start(ap, fmt);
  94. event_logv_(EVENT_LOG_ERR, strerror(errno), fmt, ap);
  95. va_end(ap);
  96. event_exit(eval);
  97. }
  98. void
  99. event_warn(const char *fmt, ...)
  100. {
  101. va_list ap;
  102. va_start(ap, fmt);
  103. event_logv_(EVENT_LOG_WARN, strerror(errno), fmt, ap);
  104. va_end(ap);
  105. }
  106. void
  107. event_sock_err(int eval, evutil_socket_t sock, const char *fmt, ...)
  108. {
  109. va_list ap;
  110. int err = evutil_socket_geterror(sock);
  111. va_start(ap, fmt);
  112. event_logv_(EVENT_LOG_ERR, evutil_socket_error_to_string(err), fmt, ap);
  113. va_end(ap);
  114. event_exit(eval);
  115. }
  116. void
  117. event_sock_warn(evutil_socket_t sock, const char *fmt, ...)
  118. {
  119. va_list ap;
  120. int err = evutil_socket_geterror(sock);
  121. va_start(ap, fmt);
  122. event_logv_(EVENT_LOG_WARN, evutil_socket_error_to_string(err), fmt, ap);
  123. va_end(ap);
  124. }
  125. void
  126. event_errx(int eval, const char *fmt, ...)
  127. {
  128. va_list ap;
  129. va_start(ap, fmt);
  130. event_logv_(EVENT_LOG_ERR, NULL, fmt, ap);
  131. va_end(ap);
  132. event_exit(eval);
  133. }
  134. void
  135. event_warnx(const char *fmt, ...)
  136. {
  137. va_list ap;
  138. va_start(ap, fmt);
  139. event_logv_(EVENT_LOG_WARN, NULL, fmt, ap);
  140. va_end(ap);
  141. }
  142. void
  143. event_msgx(const char *fmt, ...)
  144. {
  145. va_list ap;
  146. va_start(ap, fmt);
  147. event_logv_(EVENT_LOG_MSG, NULL, fmt, ap);
  148. va_end(ap);
  149. }
  150. void
  151. event_debugx_(const char *fmt, ...)
  152. {
  153. va_list ap;
  154. va_start(ap, fmt);
  155. event_logv_(EVENT_LOG_DEBUG, NULL, fmt, ap);
  156. va_end(ap);
  157. }
  158. void
  159. event_logv_(int severity, const char *errstr, const char *fmt, va_list ap)
  160. {
  161. char buf[1024];
  162. size_t len;
  163. if (severity == EVENT_LOG_DEBUG && !event_debug_get_logging_mask_())
  164. return;
  165. if (fmt != NULL)
  166. evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
  167. else
  168. buf[0] = '\0';
  169. if (errstr) {
  170. len = strlen(buf);
  171. if (len < sizeof(buf) - 3) {
  172. evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
  173. }
  174. }
  175. event_log(severity, buf);
  176. }
  177. static event_log_cb log_fn = NULL;
  178. void
  179. event_set_log_callback(event_log_cb cb)
  180. {
  181. log_fn = cb;
  182. }
  183. static void
  184. event_log(int severity, const char *msg)
  185. {
  186. if (log_fn)
  187. log_fn(severity, msg);
  188. else {
  189. const char *severity_str;
  190. switch (severity) {
  191. case EVENT_LOG_DEBUG:
  192. severity_str = "debug";
  193. break;
  194. case EVENT_LOG_MSG:
  195. severity_str = "msg";
  196. break;
  197. case EVENT_LOG_WARN:
  198. severity_str = "warn";
  199. break;
  200. case EVENT_LOG_ERR:
  201. severity_str = "err";
  202. break;
  203. default:
  204. severity_str = "???";
  205. break;
  206. }
  207. (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
  208. }
  209. }