print.c 9.8 KB

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