Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
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.

202 lines
3.6 KiB

  1. /* $OpenBSD: log.c,v 1.19 2019/07/03 05:04:19 otto Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <string.h>
  21. #include <syslog.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. #include "log.h"
  25. static int dest;
  26. static int verbose;
  27. const char *log_procname;
  28. void
  29. log_init(int n_dest, int n_verbose, int facility)
  30. {
  31. extern char *__progname;
  32. dest = n_dest;
  33. verbose = n_verbose;
  34. log_procinit(__progname);
  35. if (dest & LOG_TO_SYSLOG)
  36. openlog(__progname, LOG_PID | LOG_NDELAY, facility);
  37. tzset();
  38. }
  39. void
  40. log_procinit(const char *procname)
  41. {
  42. if (procname != NULL)
  43. log_procname = procname;
  44. }
  45. void
  46. log_setverbose(int v)
  47. {
  48. verbose = v;
  49. }
  50. int
  51. log_getverbose(void)
  52. {
  53. return (verbose);
  54. }
  55. void
  56. logit(int pri, const char *fmt, ...)
  57. {
  58. va_list ap;
  59. va_start(ap, fmt);
  60. vlog(pri, fmt, ap);
  61. va_end(ap);
  62. }
  63. void
  64. vlog(int pri, const char *fmt, va_list ap)
  65. {
  66. char *nfmt;
  67. int saved_errno = errno;
  68. va_list ap2;
  69. va_copy(ap2, ap);
  70. if (dest & LOG_TO_STDERR) {
  71. /* best effort in out of mem situations */
  72. if (asprintf(&nfmt, "%s\n", fmt) == -1) {
  73. vfprintf(stderr, fmt, ap);
  74. fprintf(stderr, "\n");
  75. } else {
  76. vfprintf(stderr, nfmt, ap);
  77. free(nfmt);
  78. }
  79. fflush(stderr);
  80. }
  81. if (dest & LOG_TO_SYSLOG)
  82. vsyslog(pri, fmt, ap2);
  83. va_end(ap2);
  84. errno = saved_errno;
  85. }
  86. void
  87. log_warn(const char *emsg, ...)
  88. {
  89. char *nfmt;
  90. va_list ap;
  91. int saved_errno = errno;
  92. /* best effort to even work in out of memory situations */
  93. if (emsg == NULL)
  94. logit(LOG_ERR, "%s", strerror(saved_errno));
  95. else {
  96. va_start(ap, emsg);
  97. if (asprintf(&nfmt, "%s: %s", emsg,
  98. strerror(saved_errno)) == -1) {
  99. /* we tried it... */
  100. vlog(LOG_ERR, emsg, ap);
  101. logit(LOG_ERR, "%s", strerror(saved_errno));
  102. } else {
  103. vlog(LOG_ERR, nfmt, ap);
  104. free(nfmt);
  105. }
  106. va_end(ap);
  107. }
  108. errno = saved_errno;
  109. }
  110. void
  111. log_warnx(const char *emsg, ...)
  112. {
  113. va_list ap;
  114. va_start(ap, emsg);
  115. vlog(LOG_ERR, emsg, ap);
  116. va_end(ap);
  117. }
  118. void
  119. log_info(const char *emsg, ...)
  120. {
  121. va_list ap;
  122. va_start(ap, emsg);
  123. vlog(LOG_INFO, emsg, ap);
  124. va_end(ap);
  125. }
  126. void
  127. log_debug(const char *emsg, ...)
  128. {
  129. va_list ap;
  130. if (verbose > 1) {
  131. va_start(ap, emsg);
  132. vlog(LOG_DEBUG, emsg, ap);
  133. va_end(ap);
  134. }
  135. }
  136. static void
  137. vfatalc(int code, const char *emsg, va_list ap)
  138. {
  139. static char s[BUFSIZ];
  140. const char *sep;
  141. if (emsg != NULL) {
  142. (void)vsnprintf(s, sizeof(s), emsg, ap);
  143. sep = ": ";
  144. } else {
  145. s[0] = '\0';
  146. sep = "";
  147. }
  148. if (code)
  149. logit(LOG_CRIT, "%s: %s%s%s",
  150. log_procname, s, sep, strerror(code));
  151. else
  152. logit(LOG_CRIT, "%s%s%s", log_procname, sep, s);
  153. }
  154. void
  155. fatal(const char *emsg, ...)
  156. {
  157. va_list ap;
  158. va_start(ap, emsg);
  159. vfatalc(errno, emsg, ap);
  160. va_end(ap);
  161. exit(1);
  162. }
  163. void
  164. fatalx(const char *emsg, ...)
  165. {
  166. va_list ap;
  167. va_start(ap, emsg);
  168. vfatalc(0, emsg, ap);
  169. va_end(ap);
  170. exit(1);
  171. }