/contrib/ntp/libntp/msyslog.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 206 lines · 149 code · 25 blank · 32 comment · 24 complexity · 4406a2f9f0457bc924565932bf3f29d4 MD5 · raw file

  1. /*
  2. * msyslog - either send a message to the terminal or print it on
  3. * the standard output.
  4. *
  5. * Converted to use varargs, much better ... jks
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. # include <config.h>
  9. #endif
  10. #ifdef HAVE_SYS_TYPES_H
  11. # include <sys/types.h>
  12. #endif
  13. #ifdef HAVE_UNISTD_H
  14. # include <unistd.h>
  15. #endif
  16. #include <stdio.h>
  17. #include "ntp_types.h"
  18. #include "ntp_string.h"
  19. #include "ntp_syslog.h"
  20. #include "ntp_stdlib.h"
  21. #ifdef SYS_WINNT
  22. # include <stdarg.h>
  23. # include "..\ports\winnt\libntp\messages.h"
  24. #endif
  25. int syslogit = 1;
  26. FILE *syslog_file = NULL;
  27. u_long ntp_syslogmask = ~ (u_long) 0;
  28. #ifdef SYS_WINNT
  29. static char separator = '\\';
  30. #else
  31. static char separator = '/';
  32. #endif /* SYS_WINNT */
  33. extern char *progname;
  34. /* Declare the local functions */
  35. void addto_syslog P((int, char *));
  36. void format_errmsg P((char *, int, const char *, int));
  37. /*
  38. * This routine adds the contents of a buffer to the log
  39. */
  40. void
  41. addto_syslog(int level, char * buf)
  42. {
  43. char *prog;
  44. FILE *out_file = syslog_file;
  45. #if !defined(VMS) && !defined (SYS_VXWORKS)
  46. if (syslogit)
  47. syslog(level, "%s", buf);
  48. else
  49. #endif /* VMS && SYS_VXWORKS*/
  50. {
  51. out_file = syslog_file ? syslog_file: level <= LOG_ERR ? stderr : stdout;
  52. /* syslog() provides the timestamp, so if we're not using
  53. syslog, we must provide it. */
  54. prog = strrchr(progname, separator);
  55. if (prog == NULL)
  56. prog = progname;
  57. else
  58. prog++;
  59. (void) fprintf(out_file, "%s ", humanlogtime ());
  60. (void) fprintf(out_file, "%s[%d]: %s", prog, (int)getpid(), buf);
  61. fflush (out_file);
  62. }
  63. #if DEBUG
  64. if (debug && out_file != stdout && out_file != stderr)
  65. printf("addto_syslog: %s\n", buf);
  66. #endif
  67. }
  68. void
  69. format_errmsg(char *nfmt, int lennfmt, const char *fmt, int errval)
  70. {
  71. register char c;
  72. register char *n;
  73. register const char *f;
  74. char *err;
  75. n = nfmt;
  76. f = fmt;
  77. while ((c = *f++) != '\0' && n < (nfmt+lennfmt - 2)) {
  78. if (c != '%') {
  79. *n++ = c;
  80. continue;
  81. }
  82. if ((c = *f++) != 'm') {
  83. *n++ = '%';
  84. *n++ = c;
  85. continue;
  86. }
  87. err = 0;
  88. err = strerror(errval);
  89. /* Make sure we have enough space for the error message */
  90. if ((n + strlen(err)) < (nfmt + lennfmt -2)) {
  91. strcpy(n, err);
  92. n += strlen(err);
  93. }
  94. }
  95. #if !defined(VMS)
  96. if (!syslogit)
  97. #endif /* VMS */
  98. *n++ = '\n';
  99. *n = '\0';
  100. }
  101. /*
  102. * The externally called functions are defined here
  103. * but share the internal function above to fetch
  104. * any error message strings, This is done so that we can
  105. * have two different functions to perform the logging
  106. * since Windows gets it's error information from different
  107. * places depending on whether or not it's network I/O.
  108. * msyslog() is for general use while netsyslog() is for
  109. * network I/O functions. They are virtually identical
  110. * in implementation.
  111. */
  112. #if defined(__STDC__) || defined(HAVE_STDARG_H)
  113. void msyslog(int level, const char *fmt, ...)
  114. #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
  115. /*VARARGS*/
  116. void msyslog(va_alist)
  117. va_dcl
  118. #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
  119. {
  120. #if defined(__STDC__) || defined(HAVE_STDARG_H)
  121. #else
  122. int level;
  123. const char *fmt;
  124. #endif
  125. va_list ap;
  126. char buf[1025], nfmt[256];
  127. /*
  128. * Save the error value as soon as possible
  129. */
  130. #ifdef SYS_WINNT
  131. int errval = GetLastError();
  132. #else
  133. int errval = errno;
  134. #endif
  135. #if defined(__STDC__) || defined(HAVE_STDARG_H)
  136. va_start(ap, fmt);
  137. #else
  138. va_start(ap);
  139. level = va_arg(ap, int);
  140. fmt = va_arg(ap, char *);
  141. #endif
  142. format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
  143. vsnprintf(buf, sizeof(buf), nfmt, ap);
  144. addto_syslog(level, buf);
  145. va_end(ap);
  146. }
  147. #if defined(__STDC__) || defined(HAVE_STDARG_H)
  148. void netsyslog(int level, const char *fmt, ...)
  149. #else /* defined(__STDC__) || defined(HAVE_STDARG_H) */
  150. /*VARARGS*/
  151. void netsyslog(va_alist)
  152. va_dcl
  153. #endif /* defined(__STDC__) || defined(HAVE_STDARG_H) */
  154. {
  155. #if defined(__STDC__) || defined(HAVE_STDARG_H)
  156. #else
  157. int level;
  158. const char *fmt;
  159. #endif
  160. va_list ap;
  161. char buf[1025], nfmt[256];
  162. /*
  163. * Save the error value as soon as possible
  164. */
  165. #ifdef SYS_WINNT
  166. int errval = WSAGetLastError();
  167. #else
  168. int errval = errno;
  169. #endif
  170. #if defined(__STDC__) || defined(HAVE_STDARG_H)
  171. va_start(ap, fmt);
  172. #else
  173. va_start(ap);
  174. level = va_arg(ap, int);
  175. fmt = va_arg(ap, char *);
  176. #endif
  177. format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
  178. vsnprintf(buf, sizeof(buf), nfmt, ap);
  179. addto_syslog(level, buf);
  180. va_end(ap);
  181. }