print.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. *
  4. * Author: Steven Dake (sdake@mvista.com)
  5. *
  6. * Copyright (c) 2006 Ericsson AB.
  7. * Author: Hans Feldt
  8. * Description: Added support for runtime installed loggers, tags tracing,
  9. * and file & line printing.
  10. *
  11. * All rights reserved.
  12. *
  13. * This software licensed under BSD license, the text of which follows:
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * - Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  37. * THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include <assert.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdarg.h>
  43. #include <sys/time.h>
  44. #include <time.h>
  45. #include <errno.h>
  46. #include <sys/types.h>
  47. #include <sys/socket.h>
  48. #if defined(OPENAIS_LINUX)
  49. #include <linux/un.h>
  50. #endif
  51. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  52. #include <sys/un.h>
  53. #endif
  54. #include <syslog.h>
  55. #include "print.h"
  56. #include "totemip.h"
  57. #include "../include/saAis.h"
  58. static unsigned int logmode = LOG_MODE_STDERR | LOG_MODE_SYSLOG;
  59. static char *logfile = 0;
  60. #define MAX_LOGGERS 32
  61. struct logger loggers[MAX_LOGGERS];
  62. #define LOG_MODE_DEBUG 1
  63. #define LOG_MODE_TIMESTAMP 2
  64. #define LOG_MODE_FILE 4
  65. #define LOG_MODE_SYSLOG 8
  66. #define LOG_MODE_STDERR 16
  67. static FILE *log_file_fp = 0;
  68. struct sockaddr_un syslog_sockaddr = {
  69. sun_family: AF_UNIX,
  70. sun_path: "/dev/log"
  71. };
  72. static int logger_init (const char *ident, int tags, int level, int mode)
  73. {
  74. int i;
  75. for (i = 0; i < MAX_LOGGERS; i++) {
  76. if (strcmp (loggers[i].ident, ident) == 0) {
  77. break;
  78. }
  79. }
  80. if (i == MAX_LOGGERS) {
  81. for (i = 0; i < MAX_LOGGERS; i++) {
  82. if (strcmp (loggers[i].ident, "") == 0) {
  83. strncpy (loggers[i].ident, ident, sizeof(loggers[i].ident));
  84. loggers[i].tags = tags;
  85. loggers[i].level = level;
  86. if (logmode & LOG_MODE_DEBUG)
  87. loggers[i].level = LOG_LEVEL_DEBUG;
  88. loggers[i].mode = mode;
  89. break;
  90. }
  91. }
  92. }
  93. assert(i < MAX_LOGGERS);
  94. return i;
  95. }
  96. int _log_init (const char *ident)
  97. {
  98. assert (ident != NULL);
  99. return logger_init (ident, TAG_LOG, LOG_LEVEL_INFO, 0);
  100. }
  101. int log_setup (char **error_string, struct main_config *config)
  102. {
  103. int i;
  104. static char error_string_response[512];
  105. logmode = config->logmode;
  106. logfile = config->logfile;
  107. if (config->logmode & LOG_MODE_FILE) {
  108. log_file_fp = fopen (config->logfile, "a+");
  109. if (log_file_fp == 0) {
  110. sprintf (error_string_response,
  111. "Can't open logfile '%s' for reason (%s).\n",
  112. config->logfile, strerror (errno));
  113. *error_string = error_string_response;
  114. return (-1);
  115. }
  116. }
  117. for (i = 0; i < config->loggers; i++) {
  118. if (config->logger[i].level == 0)
  119. config->logger[i].level = LOG_LEVEL_INFO;
  120. config->logger[i].tags |= TAG_LOG;
  121. logger_init (config->logger[i].ident,
  122. config->logger[i].tags,
  123. config->logger[i].level,
  124. config->logger[i].mode);
  125. }
  126. return (0);
  127. }
  128. static void _log_printf (char *file, int line, int priority,
  129. char *format, va_list ap)
  130. {
  131. char newstring[4096];
  132. char log_string[4096];
  133. char char_time[512];
  134. struct timeval tv;
  135. int level = LOG_LEVEL(priority);
  136. int id = LOG_ID(priority);
  137. int i = 0;
  138. assert (id < MAX_LOGGERS);
  139. if (((logmode & LOG_MODE_FILE) || (logmode & LOG_MODE_STDERR)) &&
  140. (logmode & LOG_MODE_TIMESTAMP)) {
  141. gettimeofday (&tv, NULL);
  142. strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S",
  143. localtime (&tv.tv_sec));
  144. i = sprintf (newstring, "%s.%06ld ", char_time, (long)tv.tv_usec);
  145. }
  146. if ((level == LOG_LEVEL_DEBUG) || (logmode & LOG_MODE_FILELINE)) {
  147. sprintf (&newstring[i], "[%s:%u] %s", file, line, format);
  148. } else {
  149. sprintf (&newstring[i], "[%-5s] %s", loggers[id].ident, format);
  150. }
  151. vsprintf (log_string, newstring, ap);
  152. /*
  153. * Output the log data
  154. */
  155. if (logmode & LOG_MODE_FILE && log_file_fp != 0) {
  156. fprintf (log_file_fp, "%s", log_string);
  157. fflush (log_file_fp);
  158. }
  159. if (logmode & LOG_MODE_STDERR) {
  160. fprintf (stderr, "%s", log_string);
  161. }
  162. fflush (stdout);
  163. if (logmode & LOG_MODE_SYSLOG) {
  164. syslog (level, &log_string[i]);
  165. }
  166. }
  167. void internal_log_printf (char *file, int line, int priority,
  168. char *format, ...)
  169. {
  170. int id = LOG_ID(priority);
  171. va_list ap;
  172. assert (id < MAX_LOGGERS);
  173. if (LOG_LEVEL(priority) > loggers[id].level) {
  174. return;
  175. }
  176. va_start (ap, format);
  177. _log_printf (file, line, priority, format, ap);
  178. va_end(ap);
  179. }
  180. void internal_log_printf2 (char *file, int line, int priority,
  181. char *format, ...)
  182. {
  183. va_list ap;
  184. va_start (ap, format);
  185. _log_printf (file, line, priority, format, ap);
  186. va_end(ap);
  187. }