print.c 9.6 KB

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