ulog.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * ulog - simple logging functions
  3. *
  4. * Copyright (C) 2015 Jo-Philipp Wich <jow@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "ulog.h"
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. static int _ulog_channels = -1;
  25. static int _ulog_facility = -1;
  26. static int _ulog_threshold = LOG_DEBUG;
  27. static int _ulog_initialized = 0;
  28. static const char *_ulog_ident = NULL;
  29. static const char *ulog_default_ident(void)
  30. {
  31. FILE *self;
  32. static char line[64];
  33. char *p = NULL;
  34. char *sbuf;
  35. if ((self = fopen("/proc/self/status", "r")) != NULL) {
  36. while (fgets(line, sizeof(line), self)) {
  37. if (!strncmp(line, "Name:", 5)) {
  38. strtok_r(line, "\t\n", &sbuf);
  39. p = strtok_r(NULL, "\t\n", &sbuf);
  40. break;
  41. }
  42. }
  43. fclose(self);
  44. }
  45. return p;
  46. }
  47. static void ulog_defaults(void)
  48. {
  49. char *env;
  50. if (_ulog_initialized)
  51. return;
  52. env = getenv("PREINIT");
  53. if (_ulog_channels < 0) {
  54. if (env && !strcmp(env, "1"))
  55. _ulog_channels = ULOG_KMSG;
  56. else if (isatty(1))
  57. _ulog_channels = ULOG_STDIO;
  58. else
  59. _ulog_channels = ULOG_SYSLOG;
  60. }
  61. if (_ulog_facility < 0) {
  62. if (env && !strcmp(env, "1"))
  63. _ulog_facility = LOG_DAEMON;
  64. else if (isatty(1))
  65. _ulog_facility = LOG_USER;
  66. else
  67. _ulog_facility = LOG_DAEMON;
  68. }
  69. if (_ulog_ident == NULL && _ulog_channels != ULOG_STDIO)
  70. _ulog_ident = ulog_default_ident();
  71. if (_ulog_channels & ULOG_SYSLOG)
  72. openlog(_ulog_ident, 0, _ulog_facility);
  73. _ulog_initialized = 1;
  74. }
  75. __attribute__((format(printf, 2, 0)))
  76. static void ulog_kmsg(int priority, const char *fmt, va_list ap)
  77. {
  78. FILE *kmsg;
  79. if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
  80. fprintf(kmsg, "<%u>", priority);
  81. if (_ulog_ident)
  82. fprintf(kmsg, "%s: ", _ulog_ident);
  83. vfprintf(kmsg, fmt, ap);
  84. fclose(kmsg);
  85. }
  86. }
  87. __attribute__((format(printf, 2, 0)))
  88. static void ulog_stdio(int priority, const char *fmt, va_list ap)
  89. {
  90. FILE *out = stderr;
  91. if (_ulog_ident)
  92. fprintf(out, "%s: ", _ulog_ident);
  93. vfprintf(out, fmt, ap);
  94. }
  95. __attribute__((format(printf, 2, 0)))
  96. static void ulog_syslog(int priority, const char *fmt, va_list ap)
  97. {
  98. vsyslog(priority, fmt, ap);
  99. }
  100. void ulog_open(int channels, int facility, const char *ident)
  101. {
  102. ulog_close();
  103. _ulog_channels = channels;
  104. _ulog_facility = facility;
  105. _ulog_ident = ident;
  106. }
  107. void ulog_close(void)
  108. {
  109. if (!_ulog_initialized)
  110. return;
  111. if (_ulog_channels & ULOG_SYSLOG)
  112. closelog();
  113. _ulog_initialized = 0;
  114. }
  115. void ulog_threshold(int threshold)
  116. {
  117. _ulog_threshold = threshold;
  118. }
  119. void ulog(int priority, const char *fmt, ...)
  120. {
  121. va_list ap;
  122. if (priority > _ulog_threshold)
  123. return;
  124. ulog_defaults();
  125. if (_ulog_channels & ULOG_KMSG)
  126. {
  127. va_start(ap, fmt);
  128. ulog_kmsg(priority, fmt, ap);
  129. va_end(ap);
  130. }
  131. if (_ulog_channels & ULOG_STDIO)
  132. {
  133. va_start(ap, fmt);
  134. ulog_stdio(priority, fmt, ap);
  135. va_end(ap);
  136. }
  137. if (_ulog_channels & ULOG_SYSLOG)
  138. {
  139. va_start(ap, fmt);
  140. ulog_syslog(priority, fmt, ap);
  141. va_end(ap);
  142. }
  143. }