A clone of btpd with my configuration changes.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

220 рядки
4.8 KiB

  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. #include "misc.h"
  46. #endif
  47. #include <sys/types.h>
  48. #include <sys/tree.h>
  49. #ifdef HAVE_SYS_TIME_H
  50. #include <sys/time.h>
  51. #else
  52. #include <sys/_time.h>
  53. #endif
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <stdarg.h>
  57. #include <string.h>
  58. #include <errno.h>
  59. #include "event.h"
  60. #include "log.h"
  61. static void _warn_helper(int severity, int log_errno, const char *fmt,
  62. va_list ap);
  63. static void event_log(int severity, const char *msg);
  64. static int
  65. event_vsnprintf(char *str, size_t size, const char *format, va_list args)
  66. {
  67. int r;
  68. if (size == 0)
  69. return -1;
  70. #ifdef WIN32
  71. r = _vsnprintf(str, size, format, args);
  72. #else
  73. r = vsnprintf(str, size, format, args);
  74. #endif
  75. str[size-1] = '\0';
  76. if (r < 0 || ((size_t)r) >= size) {
  77. /* different platforms behave differently on overflow;
  78. * handle both kinds. */
  79. return -1;
  80. }
  81. return r;
  82. }
  83. static int
  84. event_snprintf(char *str, size_t size, const char *format, ...)
  85. {
  86. va_list ap;
  87. int r;
  88. va_start(ap, format);
  89. r = event_vsnprintf(str, size, format, ap);
  90. va_end(ap);
  91. return r;
  92. }
  93. void
  94. event_err(int eval, const char *fmt, ...)
  95. {
  96. va_list ap;
  97. va_start(ap, fmt);
  98. _warn_helper(_EVENT_LOG_ERR, errno, fmt, ap);
  99. va_end(ap);
  100. exit(eval);
  101. }
  102. void
  103. event_warn(const char *fmt, ...)
  104. {
  105. va_list ap;
  106. va_start(ap, fmt);
  107. _warn_helper(_EVENT_LOG_WARN, errno, fmt, ap);
  108. va_end(ap);
  109. }
  110. void
  111. event_errx(int eval, const char *fmt, ...)
  112. {
  113. va_list ap;
  114. va_start(ap, fmt);
  115. _warn_helper(_EVENT_LOG_ERR, -1, fmt, ap);
  116. va_end(ap);
  117. exit(eval);
  118. }
  119. void
  120. event_warnx(const char *fmt, ...)
  121. {
  122. va_list ap;
  123. va_start(ap, fmt);
  124. _warn_helper(_EVENT_LOG_WARN, -1, fmt, ap);
  125. va_end(ap);
  126. }
  127. void
  128. event_msgx(const char *fmt, ...)
  129. {
  130. va_list ap;
  131. va_start(ap, fmt);
  132. _warn_helper(_EVENT_LOG_MSG, -1, fmt, ap);
  133. va_end(ap);
  134. }
  135. void
  136. _event_debugx(const char *fmt, ...)
  137. {
  138. va_list ap;
  139. va_start(ap, fmt);
  140. _warn_helper(_EVENT_LOG_DEBUG, -1, fmt, ap);
  141. va_end(ap);
  142. }
  143. static void
  144. _warn_helper(int severity, int log_errno, const char *fmt, va_list ap)
  145. {
  146. char buf[1024];
  147. size_t len;
  148. if (fmt != NULL)
  149. event_vsnprintf(buf, sizeof(buf), fmt, ap);
  150. else
  151. buf[0] = '\0';
  152. if (log_errno >= 0) {
  153. len = strlen(buf);
  154. if (len < sizeof(buf) - 3) {
  155. event_snprintf(buf + len, sizeof(buf) - len, ": %s",
  156. strerror(log_errno));
  157. }
  158. }
  159. event_log(severity, buf);
  160. }
  161. static event_log_cb log_fn = NULL;
  162. void
  163. event_set_log_callback(event_log_cb cb)
  164. {
  165. log_fn = cb;
  166. }
  167. static void
  168. event_log(int severity, const char *msg)
  169. {
  170. if (log_fn)
  171. log_fn(severity, msg);
  172. else {
  173. const char *severity_str;
  174. switch (severity) {
  175. case _EVENT_LOG_DEBUG:
  176. severity_str = "debug";
  177. break;
  178. case _EVENT_LOG_MSG:
  179. severity_str = "msg";
  180. break;
  181. case _EVENT_LOG_WARN:
  182. severity_str = "warn";
  183. break;
  184. case _EVENT_LOG_ERR:
  185. severity_str = "err";
  186. break;
  187. default:
  188. severity_str = "???";
  189. break;
  190. }
  191. (void)fprintf(stderr, "[%s] %s\n", severity_str, msg);
  192. }
  193. }