log.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Nick Mathewson <nickm@freehaven.net>
  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. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #ifdef WIN32
  42. #define WIN32_LEAN_AND_MEAN
  43. #include <windows.h>
  44. #undef WIN32_LEAN_AND_MEAN
  45. #endif
  46. #include <sys/types.h>
  47. #ifdef HAVE_SYS_TIME_H
  48. #include <sys/time.h>
  49. #else
  50. #include <sys/_libevent_time.h>
  51. #endif
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <stdarg.h>
  55. #include <string.h>
  56. #include <errno.h>
  57. #include "event.h"
  58. #include "log.h"
  59. #include "evutil.h"
  60. static void _warn_helper(int severity, int log_errno, const char *fmt,
  61. va_list ap);
  62. static void event_log(int severity, const char *msg);
  63. void
  64. event_err(int eval, const char *fmt, ...)
  65. {
  66. va_list ap;
  67. va_start(ap, fmt);
  68. _warn_helper(_EVENT_LOG_ERR, errno, fmt, ap);
  69. va_end(ap);
  70. exit(eval);
  71. }
  72. void
  73. event_warn(const char *fmt, ...)
  74. {
  75. va_list ap;
  76. va_start(ap, fmt);
  77. _warn_helper(_EVENT_LOG_WARN, errno, fmt, ap);
  78. va_end(ap);
  79. }
  80. void
  81. event_errx(int eval, const char *fmt, ...)
  82. {
  83. va_list ap;
  84. va_start(ap, fmt);
  85. _warn_helper(_EVENT_LOG_ERR, -1, fmt, ap);
  86. va_end(ap);
  87. exit(eval);
  88. }
  89. void
  90. event_warnx(const char *fmt, ...)
  91. {
  92. va_list ap;
  93. va_start(ap, fmt);
  94. _warn_helper(_EVENT_LOG_WARN, -1, fmt, ap);
  95. va_end(ap);
  96. }
  97. void
  98. event_msgx(const char *fmt, ...)
  99. {
  100. va_list ap;
  101. va_start(ap, fmt);
  102. _warn_helper(_EVENT_LOG_MSG, -1, fmt, ap);
  103. va_end(ap);
  104. }
  105. void
  106. _event_debugx(const char *fmt, ...)
  107. {
  108. va_list ap;
  109. va_start(ap, fmt);
  110. _warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap);
  111. va_end(ap);
  112. }
  113. static void
  114. _warn_helper(int severity, int log_errno, const char *fmt, va_list ap)
  115. {
  116. char buf[1024];
  117. size_t len;
  118. if (fmt != NULL)
  119. evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
  120. else
  121. buf[0] = '\0';
  122. if (log_errno >= 0) {
  123. len = strlen(buf);
  124. if (len < sizeof(buf) - 3) {
  125. evutil_snprintf(buf + len, sizeof(buf) - len, ": %s",
  126. strerror(log_errno));
  127. }
  128. }
  129. event_log(severity, buf);
  130. }
  131. static event_log_cb log_fn = NULL;
  132. void
  133. event_set_log_callback(event_log_cb cb)
  134. {
  135. log_fn = cb;
  136. }
  137. static void
  138. event_log(int severity, const char *msg)
  139. {
  140. if (log_fn)
  141. log_fn(severity, msg);
  142. else {
  143. const char *severity_str;
  144. switch (severity) {
  145. case _EVENT_LOG_DEBUG:
  146. severity_str = "debug";
  147. break;
  148. case _EVENT_LOG_MSG:
  149. severity_str = "msg";
  150. break;
  151. case _EVENT_LOG_WARN:
  152. severity_str = "warn";
  153. break;
  154. case _EVENT_LOG_ERR:
  155. severity_str = "err";
  156. break;
  157. default:
  158. severity_str = "???";
  159. break;
  160. }
  161. (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
  162. }
  163. }