ulog.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 struct udebug_buf *udb = NULL;
  30. static const char *ulog_default_ident(void)
  31. {
  32. FILE *self;
  33. static char line[64];
  34. char *p = NULL;
  35. char *sbuf;
  36. if ((self = fopen("/proc/self/status", "r")) != NULL) {
  37. while (fgets(line, sizeof(line), self)) {
  38. if (!strncmp(line, "Name:", 5)) {
  39. strtok_r(line, "\t\n", &sbuf);
  40. p = strtok_r(NULL, "\t\n", &sbuf);
  41. break;
  42. }
  43. }
  44. fclose(self);
  45. }
  46. return p;
  47. }
  48. static void ulog_defaults(void)
  49. {
  50. char *env;
  51. if (_ulog_initialized)
  52. return;
  53. env = getenv("PREINIT");
  54. if (_ulog_channels < 0) {
  55. if (env && !strcmp(env, "1"))
  56. _ulog_channels = ULOG_KMSG;
  57. else if (isatty(1))
  58. _ulog_channels = ULOG_STDIO;
  59. else
  60. _ulog_channels = ULOG_SYSLOG;
  61. }
  62. if (_ulog_facility < 0) {
  63. if (env && !strcmp(env, "1"))
  64. _ulog_facility = LOG_DAEMON;
  65. else if (isatty(1))
  66. _ulog_facility = LOG_USER;
  67. else
  68. _ulog_facility = LOG_DAEMON;
  69. }
  70. if (_ulog_ident == NULL && _ulog_channels != ULOG_STDIO)
  71. _ulog_ident = ulog_default_ident();
  72. if (_ulog_channels & ULOG_SYSLOG)
  73. openlog(_ulog_ident, 0, _ulog_facility);
  74. _ulog_initialized = 1;
  75. }
  76. __attribute__((format(printf, 2, 0)))
  77. static void ulog_kmsg(int priority, const char *fmt, va_list ap)
  78. {
  79. FILE *kmsg;
  80. if ((kmsg = fopen("/dev/kmsg", "r+")) != NULL) {
  81. fprintf(kmsg, "<%u>", priority);
  82. if (_ulog_ident)
  83. fprintf(kmsg, "%s: ", _ulog_ident);
  84. vfprintf(kmsg, fmt, ap);
  85. fclose(kmsg);
  86. }
  87. }
  88. __attribute__((format(printf, 2, 0)))
  89. static void ulog_stdio(int priority, const char *fmt, va_list ap)
  90. {
  91. FILE *out = stderr;
  92. if (_ulog_ident)
  93. fprintf(out, "%s: ", _ulog_ident);
  94. vfprintf(out, fmt, ap);
  95. }
  96. __attribute__((format(printf, 2, 0)))
  97. static void ulog_syslog(int priority, const char *fmt, va_list ap)
  98. {
  99. vsyslog(priority, fmt, ap);
  100. }
  101. void ulog_udebug(struct udebug_buf *_udb)
  102. {
  103. udb = _udb;
  104. }
  105. void ulog_open(int channels, int facility, const char *ident)
  106. {
  107. ulog_close();
  108. _ulog_channels = channels;
  109. _ulog_facility = facility;
  110. _ulog_ident = ident;
  111. }
  112. void ulog_close(void)
  113. {
  114. if (!_ulog_initialized)
  115. return;
  116. if (_ulog_channels & ULOG_SYSLOG)
  117. closelog();
  118. _ulog_initialized = 0;
  119. }
  120. void ulog_threshold(int threshold)
  121. {
  122. _ulog_threshold = threshold;
  123. }
  124. void ulog(int priority, const char *fmt, ...)
  125. {
  126. va_list ap;
  127. if (udb) {
  128. va_start(ap, fmt);
  129. udebug_entry_init(udb);
  130. udebug_entry_vprintf(udb, fmt, ap);
  131. udebug_entry_add(udb);
  132. va_end(ap);
  133. }
  134. if (priority > _ulog_threshold)
  135. return;
  136. ulog_defaults();
  137. if (_ulog_channels & ULOG_KMSG)
  138. {
  139. va_start(ap, fmt);
  140. ulog_kmsg(priority, fmt, ap);
  141. va_end(ap);
  142. }
  143. if (_ulog_channels & ULOG_STDIO)
  144. {
  145. va_start(ap, fmt);
  146. ulog_stdio(priority, fmt, ap);
  147. va_end(ap);
  148. }
  149. if (_ulog_channels & ULOG_SYSLOG)
  150. {
  151. va_start(ap, fmt);
  152. ulog_syslog(priority, fmt, ap);
  153. va_end(ap);
  154. }
  155. }