print.c 9.9 KB

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