print.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "swab.h"
  59. #include "print.h"
  60. #include "totemip.h"
  61. #include "../include/saAis.h"
  62. #include "mainconfig.h"
  63. #include "wthread.h"
  64. static unsigned int logmode = LOG_MODE_BUFFER | LOG_MODE_STDERR | LOG_MODE_SYSLOG;
  65. static char *logfile = 0;
  66. static int log_setup_called;
  67. static pthread_mutex_t log_mode_mutex;
  68. static struct worker_thread_group log_thread_group;
  69. static unsigned int dropped_log_entries = 0;
  70. #ifndef MAX_LOGGERS
  71. #define MAX_LOGGERS 32
  72. #endif
  73. struct logger loggers[MAX_LOGGERS];
  74. static FILE *log_file_fp = 0;
  75. struct log_entry {
  76. char *file;
  77. int line;
  78. int level;
  79. char str[128];
  80. struct log_entry *next;
  81. };
  82. static struct log_entry *head;
  83. static struct log_entry *tail;
  84. struct log_data {
  85. unsigned int syslog_pos;
  86. unsigned int level;
  87. char *log_string;
  88. };
  89. static void log_atexit (void);
  90. static int logger_init (const char *ident, int tags, int level, int mode)
  91. {
  92. int i;
  93. for (i = 0; i < MAX_LOGGERS; i++) {
  94. if (strcmp (loggers[i].ident, ident) == 0) {
  95. loggers[i].tags |= tags;
  96. if (level > loggers[i].level) {
  97. loggers[i].level = level;
  98. }
  99. break;
  100. }
  101. }
  102. if (i == MAX_LOGGERS) {
  103. for (i = 0; i < MAX_LOGGERS; i++) {
  104. if (strcmp (loggers[i].ident, "") == 0) {
  105. strncpy (loggers[i].ident, ident, sizeof(loggers[i].ident));
  106. loggers[i].tags = tags;
  107. loggers[i].level = level;
  108. loggers[i].mode = mode;
  109. break;
  110. }
  111. }
  112. }
  113. assert(i < MAX_LOGGERS);
  114. return i;
  115. }
  116. static void buffered_log_printf (char *file, int line, int level,
  117. char *format, va_list ap)
  118. {
  119. struct log_entry *entry = malloc(sizeof(struct log_entry));
  120. entry->file = file;
  121. entry->line = line;
  122. entry->level = level;
  123. entry->next = NULL;
  124. if (head == NULL) {
  125. head = tail = entry;
  126. } else {
  127. tail->next = entry;
  128. tail = entry;
  129. }
  130. vsnprintf(entry->str, sizeof(entry->str), format, ap);
  131. }
  132. static void log_printf_worker_fn (void *thread_data, void *work_item)
  133. {
  134. struct log_data *log_data = (struct log_data *)work_item;
  135. /*
  136. * Output the log data
  137. */
  138. if (logmode & LOG_MODE_FILE && log_file_fp != 0) {
  139. fprintf (log_file_fp, "%s", log_data->log_string);
  140. fflush (log_file_fp);
  141. }
  142. if (logmode & LOG_MODE_STDERR) {
  143. fprintf (stderr, "%s", log_data->log_string);
  144. fflush (stdout);
  145. }
  146. if (logmode & LOG_MODE_SYSLOG) {
  147. syslog (log_data->level, &log_data->log_string[log_data->syslog_pos]);
  148. }
  149. free (log_data->log_string);
  150. }
  151. static void _log_printf (char *file, int line,
  152. int level, int id,
  153. char *format, va_list ap)
  154. {
  155. char newstring[4096];
  156. char log_string[4096];
  157. char char_time[512];
  158. struct timeval tv;
  159. int i = 0;
  160. int len;
  161. struct log_data log_data;
  162. unsigned int res = 0;
  163. assert (id < MAX_LOGGERS);
  164. pthread_mutex_lock (&log_mode_mutex);
  165. /*
  166. ** Buffer before log_setup() has been called.
  167. */
  168. if (logmode & LOG_MODE_BUFFER) {
  169. buffered_log_printf(file, line, level, format, ap);
  170. pthread_mutex_unlock (&log_mode_mutex);
  171. return;
  172. }
  173. if (((logmode & LOG_MODE_FILE) || (logmode & LOG_MODE_STDERR)) &&
  174. (logmode & LOG_MODE_TIMESTAMP)) {
  175. gettimeofday (&tv, NULL);
  176. strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S",
  177. localtime (&tv.tv_sec));
  178. i = sprintf (newstring, "%s.%06ld ", char_time, (long)tv.tv_usec);
  179. }
  180. if ((level == LOG_LEVEL_DEBUG) || (logmode & LOG_MODE_FILELINE)) {
  181. sprintf (&newstring[i], "[%s:%04u] %s", file, line, format);
  182. } else {
  183. sprintf (&newstring[i], "[%-5s] %s", loggers[id].ident, format);
  184. }
  185. if (dropped_log_entries) {
  186. /*
  187. * Get rid of \n if there is one
  188. */
  189. if (newstring[strlen (newstring) - 1] == '\n') {
  190. newstring[strlen (newstring) - 1] = '\0';
  191. }
  192. len = sprintf (log_string,
  193. "%s - prior to this log entry, openais logger dropped '%d' messages because of overflow.", newstring, dropped_log_entries + 1);
  194. } else {
  195. len = vsprintf (log_string, newstring, ap);
  196. }
  197. /*
  198. ** add line feed if not done yet
  199. */
  200. if (log_string[len - 1] != '\n') {
  201. log_string[len] = '\n';
  202. log_string[len + 1] = '\0';
  203. }
  204. /*
  205. * Create work thread data
  206. */
  207. log_data.syslog_pos = i;
  208. log_data.level = level;
  209. log_data.log_string = strdup (log_string);
  210. if (log_data.log_string == NULL) {
  211. goto drop_log_msg;
  212. }
  213. if (log_setup_called) {
  214. res = worker_thread_group_work_add (&log_thread_group, &log_data);
  215. if (res == 0) {
  216. dropped_log_entries = 0;
  217. } else {
  218. dropped_log_entries += 1;
  219. }
  220. } else {
  221. log_printf_worker_fn (NULL, &log_data);
  222. }
  223. pthread_mutex_unlock (&log_mode_mutex);
  224. return;
  225. drop_log_msg:
  226. dropped_log_entries++;
  227. pthread_mutex_unlock (&log_mode_mutex);
  228. }
  229. int _log_init (const char *ident)
  230. {
  231. assert (ident != NULL);
  232. /*
  233. ** do different things before and after log_setup() has been called
  234. */
  235. if (log_setup_called) {
  236. return logger_init (ident, TAG_LOG, LOG_LEVEL_INFO, 0);
  237. } else {
  238. return logger_init (ident, ~0, LOG_LEVEL_DEBUG, 0);
  239. }
  240. }
  241. int log_setup (char **error_string, struct main_config *config)
  242. {
  243. int i;
  244. static char error_string_response[512];
  245. if (config->logmode & LOG_MODE_FILE) {
  246. log_file_fp = fopen (config->logfile, "a+");
  247. if (log_file_fp == 0) {
  248. sprintf (error_string_response,
  249. "Can't open logfile '%s' for reason (%s).\n",
  250. config->logfile, strerror (errno));
  251. *error_string = error_string_response;
  252. return (-1);
  253. }
  254. }
  255. pthread_mutex_lock (&log_mode_mutex);
  256. logmode = config->logmode;
  257. pthread_mutex_unlock (&log_mode_mutex);
  258. logfile = config->logfile;
  259. if (config->logmode & LOG_MODE_SYSLOG) {
  260. openlog("openais", LOG_CONS|LOG_PID, config->syslog_facility);
  261. }
  262. /*
  263. ** reinit all loggers that has initialised before log_setup() was called.
  264. */
  265. for (i = 0; i < MAX_LOGGERS; i++) {
  266. loggers[i].tags = TAG_LOG;
  267. if (config->logmode & LOG_MODE_DEBUG) {
  268. loggers[i].level = LOG_LEVEL_DEBUG;
  269. } else {
  270. loggers[i].level = LOG_LEVEL_INFO;
  271. }
  272. }
  273. /*
  274. ** init all loggers that has configured level and tags
  275. */
  276. for (i = 0; i < config->loggers; i++) {
  277. if (config->logger[i].level == 0)
  278. config->logger[i].level = LOG_LEVEL_INFO;
  279. config->logger[i].tags |= TAG_LOG;
  280. logger_init (config->logger[i].ident,
  281. config->logger[i].tags,
  282. config->logger[i].level,
  283. config->logger[i].mode);
  284. }
  285. worker_thread_group_init (
  286. &log_thread_group,
  287. 1,
  288. 1024,
  289. sizeof (struct log_data),
  290. 0,
  291. NULL,
  292. log_printf_worker_fn);
  293. /*
  294. ** Flush what we have buffered
  295. */
  296. log_flush();
  297. internal_log_printf(__FILE__, __LINE__, LOG_LEVEL_DEBUG, "log setup\n");
  298. atexit (log_atexit);
  299. log_setup_called = 1;
  300. return (0);
  301. }
  302. void internal_log_printf (char *file, int line, int priority,
  303. char *format, ...)
  304. {
  305. int id = LOG_ID(priority);
  306. int level = LOG_LEVEL(priority);
  307. va_list ap;
  308. assert (id < MAX_LOGGERS);
  309. if (LOG_LEVEL(priority) > loggers[id].level) {
  310. return;
  311. }
  312. va_start (ap, format);
  313. _log_printf (file, line, level, id, format, ap);
  314. va_end(ap);
  315. }
  316. void internal_log_printf2 (char *file, int line, int level, int id,
  317. char *format, ...)
  318. {
  319. va_list ap;
  320. assert (id < MAX_LOGGERS);
  321. va_start (ap, format);
  322. _log_printf (file, line, level, id, format, ap);
  323. va_end(ap);
  324. }
  325. void trace (char *file, int line, int tag, int id, char *format, ...)
  326. {
  327. assert (id < MAX_LOGGERS);
  328. if (tag & loggers[id].tags) {
  329. va_list ap;
  330. va_start (ap, format);
  331. _log_printf (file, line, LOG_LEVEL_DEBUG, id, format, ap);
  332. va_end(ap);
  333. }
  334. }
  335. static void log_atexit (void)
  336. {
  337. if (log_setup_called) {
  338. worker_thread_group_wait (&log_thread_group);
  339. }
  340. }
  341. void log_flush (void)
  342. {
  343. if (log_setup_called) {
  344. log_setup_called = 0;
  345. worker_thread_group_exit (&log_thread_group);
  346. worker_thread_group_atsegv (&log_thread_group);
  347. } else {
  348. struct log_entry *entry = head;
  349. struct log_entry *tmp;
  350. /* do not buffer these printouts */
  351. logmode &= ~LOG_MODE_BUFFER;
  352. while (entry) {
  353. internal_log_printf(entry->file, entry->line,
  354. entry->level, entry->str);
  355. tmp = entry;
  356. entry = entry->next;
  357. free(tmp);
  358. }
  359. head = tail = NULL;
  360. }
  361. }