log.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2012 The FreeBSD Foundation
  5. *
  6. * This software was developed by Edward Tomasz Napierala under sponsorship
  7. * from the FreeBSD Foundation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <errno.h>
  31. #include <stdarg.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <syslog.h>
  36. #include <vis.h>
  37. #include "libiscsiutil.h"
  38. static int log_level = 0;
  39. static char *peer_name = NULL;
  40. static char *peer_addr = NULL;
  41. #define MSGBUF_LEN 1024
  42. void
  43. log_init(int level)
  44. {
  45. log_level = level;
  46. openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
  47. }
  48. void
  49. log_set_peer_name(const char *name)
  50. {
  51. /*
  52. * XXX: Turn it into assertion?
  53. */
  54. if (peer_name != NULL)
  55. log_errx(1, "%s called twice", __func__);
  56. if (peer_addr == NULL)
  57. log_errx(1, "%s called before log_set_peer_addr", __func__);
  58. peer_name = checked_strdup(name);
  59. }
  60. void
  61. log_set_peer_addr(const char *addr)
  62. {
  63. /*
  64. * XXX: Turn it into assertion?
  65. */
  66. if (peer_addr != NULL)
  67. log_errx(1, "%s called twice", __func__);
  68. peer_addr = checked_strdup(addr);
  69. }
  70. static void
  71. log_common(int priority, int log_errno, const char *fmt, va_list ap)
  72. {
  73. static char msgbuf[MSGBUF_LEN];
  74. static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
  75. char *errstr;
  76. int ret;
  77. ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
  78. if (ret < 0) {
  79. fprintf(stderr, "%s: snprintf failed", getprogname());
  80. syslog(LOG_CRIT, "snprintf failed");
  81. exit(1);
  82. }
  83. ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
  84. if (ret < 0) {
  85. fprintf(stderr, "%s: strnvis failed", getprogname());
  86. syslog(LOG_CRIT, "strnvis failed");
  87. exit(1);
  88. }
  89. if (log_errno == -1) {
  90. if (peer_name != NULL) {
  91. fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
  92. peer_addr, peer_name, msgbuf_strvised);
  93. syslog(priority, "%s (%s): %s",
  94. peer_addr, peer_name, msgbuf_strvised);
  95. } else if (peer_addr != NULL) {
  96. fprintf(stderr, "%s: %s: %s\n", getprogname(),
  97. peer_addr, msgbuf_strvised);
  98. syslog(priority, "%s: %s",
  99. peer_addr, msgbuf_strvised);
  100. } else {
  101. fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
  102. syslog(priority, "%s", msgbuf_strvised);
  103. }
  104. } else {
  105. errstr = strerror(log_errno);
  106. if (peer_name != NULL) {
  107. fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
  108. peer_addr, peer_name, msgbuf_strvised, errstr);
  109. syslog(priority, "%s (%s): %s: %s",
  110. peer_addr, peer_name, msgbuf_strvised, errstr);
  111. } else if (peer_addr != NULL) {
  112. fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
  113. peer_addr, msgbuf_strvised, errstr);
  114. syslog(priority, "%s: %s: %s",
  115. peer_addr, msgbuf_strvised, errstr);
  116. } else {
  117. fprintf(stderr, "%s: %s: %s\n", getprogname(),
  118. msgbuf_strvised, errstr);
  119. syslog(priority, "%s: %s",
  120. msgbuf_strvised, errstr);
  121. }
  122. }
  123. }
  124. void
  125. log_err(int eval, const char *fmt, ...)
  126. {
  127. va_list ap;
  128. va_start(ap, fmt);
  129. log_common(LOG_CRIT, errno, fmt, ap);
  130. va_end(ap);
  131. exit(eval);
  132. }
  133. void
  134. log_errx(int eval, const char *fmt, ...)
  135. {
  136. va_list ap;
  137. va_start(ap, fmt);
  138. log_common(LOG_CRIT, -1, fmt, ap);
  139. va_end(ap);
  140. exit(eval);
  141. }
  142. void
  143. log_warn(const char *fmt, ...)
  144. {
  145. va_list ap;
  146. va_start(ap, fmt);
  147. log_common(LOG_WARNING, errno, fmt, ap);
  148. va_end(ap);
  149. }
  150. void
  151. log_warnx(const char *fmt, ...)
  152. {
  153. va_list ap;
  154. va_start(ap, fmt);
  155. log_common(LOG_WARNING, -1, fmt, ap);
  156. va_end(ap);
  157. }
  158. void
  159. log_debugx(const char *fmt, ...)
  160. {
  161. va_list ap;
  162. if (log_level == 0)
  163. return;
  164. va_start(ap, fmt);
  165. log_common(LOG_DEBUG, -1, fmt, ap);
  166. va_end(ap);
  167. }