print.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <stdlib.h>
  56. #include "print.h"
  57. #include "totemip.h"
  58. #include "../include/saAis.h"
  59. static unsigned int logmode = LOG_MODE_BUFFER | LOG_MODE_STDERR | LOG_MODE_SYSLOG;
  60. static char *logfile = 0;
  61. static int log_setup_called;
  62. #ifndef MAX_LOGGERS
  63. #define MAX_LOGGERS 32
  64. #endif
  65. struct logger loggers[MAX_LOGGERS];
  66. static FILE *log_file_fp = 0;
  67. struct log_entry
  68. {
  69. char *file;
  70. int line;
  71. int level;
  72. char str[128];
  73. struct log_entry *next;
  74. };
  75. static struct log_entry *head;
  76. static struct log_entry *tail;
  77. static int logger_init (const char *ident, int tags, int level, int mode)
  78. {
  79. int i;
  80. for (i = 0; i < MAX_LOGGERS; i++) {
  81. if (strcmp (loggers[i].ident, ident) == 0) {
  82. loggers[i].tags |= tags;
  83. if (level > loggers[i].level) {
  84. loggers[i].level = level;
  85. }
  86. break;
  87. }
  88. }
  89. if (i == MAX_LOGGERS) {
  90. for (i = 0; i < MAX_LOGGERS; i++) {
  91. if (strcmp (loggers[i].ident, "") == 0) {
  92. strncpy (loggers[i].ident, ident, sizeof(loggers[i].ident));
  93. loggers[i].tags = tags;
  94. loggers[i].level = level;
  95. loggers[i].mode = mode;
  96. break;
  97. }
  98. }
  99. }
  100. assert(i < MAX_LOGGERS);
  101. return i;
  102. }
  103. static void buffered_log_printf (char *file, int line, int level,
  104. char *format, va_list ap)
  105. {
  106. struct log_entry *entry = malloc(sizeof(struct log_entry));
  107. entry->file = file;
  108. entry->line = line;
  109. entry->level = level;
  110. entry->next = NULL;
  111. if (head == NULL) {
  112. head = tail = entry;
  113. } else {
  114. tail->next = entry;
  115. tail = entry;
  116. }
  117. vsnprintf(entry->str, sizeof(entry->str), format, ap);
  118. }
  119. static void _log_printf (char *file, int line,
  120. int level, int id,
  121. char *format, va_list ap)
  122. {
  123. char newstring[4096];
  124. char log_string[4096];
  125. char char_time[512];
  126. struct timeval tv;
  127. int i = 0;
  128. int len;
  129. assert (id < MAX_LOGGERS);
  130. /*
  131. ** Buffer before log_setup() has been called.
  132. */
  133. if (logmode & LOG_MODE_BUFFER) {
  134. buffered_log_printf(file, line, level, format, ap);
  135. return;
  136. }
  137. if (((logmode & LOG_MODE_FILE) || (logmode & LOG_MODE_STDERR)) &&
  138. (logmode & LOG_MODE_TIMESTAMP)) {
  139. gettimeofday (&tv, NULL);
  140. strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S",
  141. localtime (&tv.tv_sec));
  142. i = sprintf (newstring, "%s.%06ld ", char_time, (long)tv.tv_usec);
  143. }
  144. if ((level == LOG_LEVEL_DEBUG) || (logmode & LOG_MODE_FILELINE)) {
  145. sprintf (&newstring[i], "[%s:%u] %s", file, line, format);
  146. } else {
  147. sprintf (&newstring[i], "[%-5s] %s", loggers[id].ident, format);
  148. }
  149. len = vsprintf (log_string, newstring, ap);
  150. /*
  151. ** add line feed if not done yet
  152. */
  153. if (log_string[len - 1] != '\n') {
  154. log_string[len] = '\n';
  155. log_string[len + 1] = '\0';
  156. }
  157. /*
  158. * Output the log data
  159. */
  160. if (logmode & LOG_MODE_FILE && log_file_fp != 0) {
  161. fprintf (log_file_fp, "%s", log_string);
  162. fflush (log_file_fp);
  163. }
  164. if (logmode & LOG_MODE_STDERR) {
  165. fprintf (stderr, "%s", log_string);
  166. }
  167. fflush (stdout);
  168. if (logmode & LOG_MODE_SYSLOG) {
  169. syslog (level, &log_string[i]);
  170. }
  171. }
  172. int _log_init (const char *ident)
  173. {
  174. assert (ident != NULL);
  175. /*
  176. ** do different things before and after log_setup() has been called
  177. */
  178. if (log_setup_called) {
  179. return logger_init (ident, TAG_LOG, LOG_LEVEL_INFO, 0);
  180. } else {
  181. return logger_init (ident, ~0, LOG_LEVEL_DEBUG, 0);
  182. }
  183. }
  184. int log_setup (char **error_string, struct main_config *config)
  185. {
  186. int i;
  187. static char error_string_response[512];
  188. logmode = config->logmode;
  189. logfile = config->logfile;
  190. if (config->logmode & LOG_MODE_FILE) {
  191. log_file_fp = fopen (config->logfile, "a+");
  192. if (log_file_fp == 0) {
  193. sprintf (error_string_response,
  194. "Can't open logfile '%s' for reason (%s).\n",
  195. config->logfile, strerror (errno));
  196. *error_string = error_string_response;
  197. return (-1);
  198. }
  199. }
  200. /*
  201. ** reinit all loggers that has initialised before log_setup() was called.
  202. */
  203. for (i = 0; i < MAX_LOGGERS; i++) {
  204. loggers[i].tags = TAG_LOG;
  205. if (config->logmode & LOG_MODE_DEBUG) {
  206. loggers[i].level = LOG_LEVEL_DEBUG;
  207. } else {
  208. loggers[i].level = LOG_LEVEL_INFO;
  209. }
  210. }
  211. /*
  212. ** init all loggers that has configured level and tags
  213. */
  214. for (i = 0; i < config->loggers; i++) {
  215. if (config->logger[i].level == 0)
  216. config->logger[i].level = LOG_LEVEL_INFO;
  217. config->logger[i].tags |= TAG_LOG;
  218. logger_init (config->logger[i].ident,
  219. config->logger[i].tags,
  220. config->logger[i].level,
  221. config->logger[i].mode);
  222. }
  223. log_setup_called = 1;
  224. /*
  225. ** Flush what we have buffered
  226. */
  227. log_flush();
  228. internal_log_printf(__FILE__, __LINE__, LOG_LEVEL_DEBUG, "log setup\n");
  229. return (0);
  230. }
  231. void internal_log_printf (char *file, int line, int priority,
  232. char *format, ...)
  233. {
  234. int id = LOG_ID(priority);
  235. int level = LOG_LEVEL(priority);
  236. va_list ap;
  237. assert (id < MAX_LOGGERS);
  238. if (LOG_LEVEL(priority) > loggers[id].level) {
  239. return;
  240. }
  241. va_start (ap, format);
  242. _log_printf (file, line, level, id, format, ap);
  243. va_end(ap);
  244. }
  245. void internal_log_printf2 (char *file, int line, int level, int id,
  246. char *format, ...)
  247. {
  248. va_list ap;
  249. assert (id < MAX_LOGGERS);
  250. va_start (ap, format);
  251. _log_printf (file, line, level, id, format, ap);
  252. va_end(ap);
  253. }
  254. void trace (char *file, int line, int tag, int id, char *format, ...)
  255. {
  256. assert (id < MAX_LOGGERS);
  257. if (tag & loggers[id].tags) {
  258. va_list ap;
  259. va_start (ap, format);
  260. _log_printf (file, line, LOG_LEVEL_DEBUG, id, format, ap);
  261. va_end(ap);
  262. }
  263. }
  264. void log_flush(void)
  265. {
  266. struct log_entry *entry = head;
  267. struct log_entry *tmp;
  268. /* do not buffer these printouts */
  269. logmode &= ~LOG_MODE_BUFFER;
  270. while (entry) {
  271. internal_log_printf(entry->file, entry->line,
  272. entry->level, entry->str);
  273. tmp = entry;
  274. entry = entry->next;
  275. free(tmp);
  276. }
  277. head = tail = NULL;
  278. }