print.c 7.9 KB

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