print.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Ericsson AB.
  4. *
  5. * Author: Steven Dake (sdake@mvista.com)
  6. * Hans Feldt
  7. *
  8. * All rights reserved.
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <assert.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdarg.h>
  40. #include <sys/time.h>
  41. #include <time.h>
  42. #include <errno.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #if defined(OPENAIS_LINUX)
  46. #include <linux/un.h>
  47. #endif
  48. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  49. #include <sys/un.h>
  50. #endif
  51. #include <syslog.h>
  52. #include <stdlib.h>
  53. #include "print.h"
  54. #include "totemip.h"
  55. #include "../include/saAis.h"
  56. #include "mainconfig.h"
  57. static unsigned int logmode = LOG_MODE_BUFFER | LOG_MODE_STDERR | LOG_MODE_SYSLOG;
  58. static char *logfile = 0;
  59. static int log_setup_called;
  60. #ifndef MAX_LOGGERS
  61. #define MAX_LOGGERS 32
  62. #endif
  63. struct logger loggers[MAX_LOGGERS];
  64. static FILE *log_file_fp = 0;
  65. struct log_entry
  66. {
  67. char *file;
  68. int line;
  69. int level;
  70. char str[128];
  71. struct log_entry *next;
  72. };
  73. static struct log_entry *head;
  74. static struct log_entry *tail;
  75. static int logger_init (const char *ident, int tags, int level, int mode)
  76. {
  77. int i;
  78. for (i = 0; i < MAX_LOGGERS; i++) {
  79. if (strcmp (loggers[i].ident, ident) == 0) {
  80. loggers[i].tags |= tags;
  81. if (level > loggers[i].level) {
  82. loggers[i].level = level;
  83. }
  84. break;
  85. }
  86. }
  87. if (i == MAX_LOGGERS) {
  88. for (i = 0; i < MAX_LOGGERS; i++) {
  89. if (strcmp (loggers[i].ident, "") == 0) {
  90. strncpy (loggers[i].ident, ident, sizeof(loggers[i].ident));
  91. loggers[i].tags = tags;
  92. loggers[i].level = level;
  93. loggers[i].mode = mode;
  94. break;
  95. }
  96. }
  97. }
  98. assert(i < MAX_LOGGERS);
  99. return i;
  100. }
  101. static void buffered_log_printf (char *file, int line, int level,
  102. char *format, va_list ap)
  103. {
  104. struct log_entry *entry = malloc(sizeof(struct log_entry));
  105. entry->file = file;
  106. entry->line = line;
  107. entry->level = level;
  108. entry->next = NULL;
  109. if (head == NULL) {
  110. head = tail = entry;
  111. } else {
  112. tail->next = entry;
  113. tail = entry;
  114. }
  115. vsnprintf(entry->str, sizeof(entry->str), format, ap);
  116. }
  117. static void _log_printf (char *file, int line,
  118. int level, int id,
  119. char *format, va_list ap)
  120. {
  121. char newstring[4096];
  122. char log_string[4096];
  123. char char_time[512];
  124. struct timeval tv;
  125. int i = 0;
  126. int len;
  127. assert (id < MAX_LOGGERS);
  128. /*
  129. ** Buffer before log_setup() has been called.
  130. */
  131. if (logmode & LOG_MODE_BUFFER) {
  132. buffered_log_printf(file, line, level, format, ap);
  133. return;
  134. }
  135. if (((logmode & LOG_MODE_FILE) || (logmode & LOG_MODE_STDERR)) &&
  136. (logmode & LOG_MODE_TIMESTAMP)) {
  137. gettimeofday (&tv, NULL);
  138. strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S",
  139. localtime (&tv.tv_sec));
  140. i = sprintf (newstring, "%s.%06ld ", char_time, (long)tv.tv_usec);
  141. }
  142. if ((level == LOG_LEVEL_DEBUG) || (logmode & LOG_MODE_FILELINE)) {
  143. sprintf (&newstring[i], "[%s:%04u] %s", file, line, format);
  144. } else {
  145. sprintf (&newstring[i], "[%-5s] %s", loggers[id].ident, format);
  146. }
  147. len = vsprintf (log_string, newstring, ap);
  148. /*
  149. ** add line feed if not done yet
  150. */
  151. if (log_string[len - 1] != '\n') {
  152. log_string[len] = '\n';
  153. log_string[len + 1] = '\0';
  154. }
  155. /*
  156. * Output the log data
  157. */
  158. if (logmode & LOG_MODE_FILE && log_file_fp != 0) {
  159. fprintf (log_file_fp, "%s", log_string);
  160. fflush (log_file_fp);
  161. }
  162. if (logmode & LOG_MODE_STDERR) {
  163. fprintf (stderr, "%s", log_string);
  164. }
  165. fflush (stdout);
  166. if (logmode & LOG_MODE_SYSLOG) {
  167. syslog (level, &log_string[i]);
  168. }
  169. }
  170. int _log_init (const char *ident)
  171. {
  172. assert (ident != NULL);
  173. /*
  174. ** do different things before and after log_setup() has been called
  175. */
  176. if (log_setup_called) {
  177. return logger_init (ident, TAG_LOG, LOG_LEVEL_INFO, 0);
  178. } else {
  179. return logger_init (ident, ~0, LOG_LEVEL_DEBUG, 0);
  180. }
  181. }
  182. int log_setup (char **error_string, struct main_config *config)
  183. {
  184. int i;
  185. static char error_string_response[512];
  186. if (config->logmode & LOG_MODE_FILE) {
  187. log_file_fp = fopen (config->logfile, "a+");
  188. if (log_file_fp == 0) {
  189. sprintf (error_string_response,
  190. "Can't open logfile '%s' for reason (%s).\n",
  191. config->logfile, strerror (errno));
  192. *error_string = error_string_response;
  193. return (-1);
  194. }
  195. }
  196. logmode = config->logmode;
  197. logfile = config->logfile;
  198. if (config->logmode & LOG_MODE_SYSLOG) {
  199. openlog("openais", LOG_CONS|LOG_PID, config->syslog_facility);
  200. }
  201. /*
  202. ** reinit all loggers that has initialised before log_setup() was called.
  203. */
  204. for (i = 0; i < MAX_LOGGERS; i++) {
  205. loggers[i].tags = TAG_LOG;
  206. if (config->logmode & LOG_MODE_DEBUG) {
  207. loggers[i].level = LOG_LEVEL_DEBUG;
  208. } else {
  209. loggers[i].level = LOG_LEVEL_INFO;
  210. }
  211. }
  212. /*
  213. ** init all loggers that has configured level and tags
  214. */
  215. for (i = 0; i < config->loggers; i++) {
  216. if (config->logger[i].level == 0)
  217. config->logger[i].level = LOG_LEVEL_INFO;
  218. config->logger[i].tags |= TAG_LOG;
  219. logger_init (config->logger[i].ident,
  220. config->logger[i].tags,
  221. config->logger[i].level,
  222. config->logger[i].mode);
  223. }
  224. log_setup_called = 1;
  225. /*
  226. ** Flush what we have buffered
  227. */
  228. log_flush();
  229. internal_log_printf(__FILE__, __LINE__, LOG_LEVEL_DEBUG, "log setup\n");
  230. return (0);
  231. }
  232. void internal_log_printf (char *file, int line, int priority,
  233. char *format, ...)
  234. {
  235. int id = LOG_ID(priority);
  236. int level = LOG_LEVEL(priority);
  237. va_list ap;
  238. assert (id < MAX_LOGGERS);
  239. if (LOG_LEVEL(priority) > loggers[id].level) {
  240. return;
  241. }
  242. va_start (ap, format);
  243. _log_printf (file, line, level, id, format, ap);
  244. va_end(ap);
  245. }
  246. void internal_log_printf2 (char *file, int line, int level, int id,
  247. char *format, ...)
  248. {
  249. va_list ap;
  250. assert (id < MAX_LOGGERS);
  251. va_start (ap, format);
  252. _log_printf (file, line, level, id, format, ap);
  253. va_end(ap);
  254. }
  255. void trace (char *file, int line, int tag, int id, char *format, ...)
  256. {
  257. assert (id < MAX_LOGGERS);
  258. if (tag & loggers[id].tags) {
  259. va_list ap;
  260. va_start (ap, format);
  261. _log_printf (file, line, LOG_LEVEL_DEBUG, id, format, ap);
  262. va_end(ap);
  263. }
  264. }
  265. void log_flush(void)
  266. {
  267. struct log_entry *entry = head;
  268. struct log_entry *tmp;
  269. /* do not buffer these printouts */
  270. logmode &= ~LOG_MODE_BUFFER;
  271. while (entry) {
  272. internal_log_printf(entry->file, entry->line,
  273. entry->level, entry->str);
  274. tmp = entry;
  275. entry = entry->next;
  276. free(tmp);
  277. }
  278. head = tail = NULL;
  279. }