logsys.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007 Red Hat, Inc.
  4. *
  5. * Author: Steven Dake (sdake@redhat.com)
  6. * Author: Lon Hohberger (lhh@redhat.com)
  7. *
  8. * All rights reserved.
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <assert.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdarg.h>
  40. #include <sys/time.h>
  41. #include <time.h>
  42. #include <errno.h>
  43. #include <sys/types.h>
  44. #include <sys/socket.h>
  45. #if defined(OPENAIS_LINUX)
  46. #include <linux/un.h>
  47. #endif
  48. #if defined(OPENAIS_BSD) || defined(OPENAIS_DARWIN)
  49. #include <sys/un.h>
  50. #endif
  51. #define SYSLOG_NAMES
  52. #include <syslog.h>
  53. #include <stdlib.h>
  54. #include <pthread.h>
  55. #include "swab.h"
  56. #include "logsys.h"
  57. #include "totemip.h"
  58. //#include "../include/saAis.h"
  59. #include "mainconfig.h"
  60. #include "wthread.h"
  61. /*
  62. * Configuration parameters for logging system
  63. */
  64. static char *logsys_name = NULL;
  65. static unsigned int logsys_mode = 0;
  66. static char *logsys_file = NULL;
  67. static FILE *logsys_file_fp = NULL;
  68. static int logsys_facility = LOG_DAEMON;
  69. static int logsys_nosubsys = 0;
  70. static int logsys_wthread_active = 0;
  71. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  72. static pthread_mutex_t logsys_new_log_mutex = PTHREAD_MUTEX_INITIALIZER;
  73. static struct worker_thread_group log_thread_group;
  74. static unsigned int dropped_log_entries = 0;
  75. #ifndef MAX_LOGGERS
  76. #define MAX_LOGGERS 32
  77. #endif
  78. struct logsys_logger logsys_loggers[MAX_LOGGERS];
  79. struct log_entry {
  80. char *file;
  81. int line;
  82. int priority;
  83. char str[128];
  84. struct log_entry *next;
  85. };
  86. static struct log_entry *head;
  87. static struct log_entry *tail;
  88. struct log_data {
  89. unsigned int syslog_pos;
  90. unsigned int priority;
  91. char *log_string;
  92. };
  93. enum logsys_config_mutex_state {
  94. LOGSYS_CONFIG_MUTEX_LOCKED,
  95. LOGSYS_CONFIG_MUTEX_UNLOCKED
  96. };
  97. static void logsys_atexit (void);
  98. #define LEVELMASK 0x07 /* 3 bits */
  99. #define LOG_LEVEL(p) ((p) & LEVELMASK)
  100. #define LOGSYS_IDMASK (0x3f << 3) /* 6 bits */
  101. #define LOG_ID(p) (((p) & LOGSYS_IDMASK) >> 3)
  102. static void logsys_buffer_flush (void);
  103. void _logsys_nosubsys_set (void)
  104. {
  105. logsys_nosubsys = 1;
  106. }
  107. int logsys_facility_id_get (const char *name)
  108. {
  109. unsigned int i;
  110. for (i = 0; facilitynames[i].c_name != NULL; i++) {
  111. if (strcasecmp(name, facilitynames[i].c_name) == 0) {
  112. return (facilitynames[i].c_val);
  113. }
  114. }
  115. return (-1);
  116. }
  117. int logsys_priority_id_get (const char *name)
  118. {
  119. unsigned int i;
  120. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  121. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  122. return (prioritynames[i].c_val);
  123. }
  124. }
  125. return (-1);
  126. }
  127. static inline char *logsys_priority_name_get (unsigned int priority)
  128. {
  129. unsigned int i;
  130. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  131. if (priority == prioritynames[i].c_val) {
  132. return (prioritynames[i].c_name);
  133. }
  134. }
  135. return (NULL);
  136. }
  137. unsigned int logsys_config_subsys_set (
  138. const char *subsys,
  139. unsigned int tags,
  140. unsigned int priority)
  141. {
  142. int i;
  143. pthread_mutex_lock (&logsys_config_mutex);
  144. for (i = 0; i < MAX_LOGGERS; i++) {
  145. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  146. logsys_loggers[i].tags = tags;
  147. logsys_loggers[i].priority = priority;
  148. if (priority > logsys_loggers[i].priority) {
  149. logsys_loggers[i].priority = priority;
  150. }
  151. break;
  152. }
  153. }
  154. if (i == MAX_LOGGERS) {
  155. for (i = 0; i < MAX_LOGGERS; i++) {
  156. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  157. strncpy (logsys_loggers[i].subsys, subsys,
  158. sizeof(logsys_loggers[i].subsys));
  159. logsys_loggers[i].tags = tags;
  160. logsys_loggers[i].priority = priority;
  161. break;
  162. }
  163. }
  164. }
  165. assert(i < MAX_LOGGERS);
  166. pthread_mutex_unlock (&logsys_config_mutex);
  167. return i;
  168. }
  169. inline int logsys_mkpri (int priority, int id)
  170. {
  171. return (((id) << 3) | (priority));
  172. }
  173. int logsys_config_subsys_get (
  174. const char *subsys,
  175. unsigned int *tags,
  176. unsigned int *priority)
  177. {
  178. unsigned int i;
  179. pthread_mutex_lock (&logsys_config_mutex);
  180. for (i = 0; i < MAX_LOGGERS; i++) {
  181. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  182. *tags = logsys_loggers[i].tags;
  183. *priority = logsys_loggers[i].priority;
  184. pthread_mutex_unlock (&logsys_config_mutex);
  185. return (0);
  186. }
  187. }
  188. pthread_mutex_unlock (&logsys_config_mutex);
  189. return (-1);
  190. }
  191. static void buffered_log_printf (
  192. char *file,
  193. int line,
  194. int priority,
  195. char *format,
  196. va_list ap)
  197. {
  198. struct log_entry *entry = malloc(sizeof(struct log_entry));
  199. entry->file = file;
  200. entry->line = line;
  201. entry->priority = priority;
  202. entry->next = NULL;
  203. if (head == NULL) {
  204. head = tail = entry;
  205. } else {
  206. tail->next = entry;
  207. tail = entry;
  208. }
  209. vsnprintf(entry->str, sizeof(entry->str), format, ap);
  210. }
  211. static void log_printf_worker_fn (void *thread_data, void *work_item)
  212. {
  213. struct log_data *log_data = (struct log_data *)work_item;
  214. pthread_mutex_lock (&logsys_config_mutex);
  215. /*
  216. * Output the log data
  217. */
  218. if (logsys_mode & LOG_MODE_OUTPUT_FILE && logsys_file_fp != 0) {
  219. fprintf (logsys_file_fp, "%s", log_data->log_string);
  220. fflush (logsys_file_fp);
  221. }
  222. if (logsys_mode & LOG_MODE_OUTPUT_STDERR) {
  223. fprintf (stderr, "%s", log_data->log_string);
  224. fflush (stdout);
  225. }
  226. if (logsys_mode & LOG_MODE_OUTPUT_SYSLOG_THREADED) {
  227. syslog (log_data->priority,
  228. &log_data->log_string[log_data->syslog_pos]);
  229. }
  230. free (log_data->log_string);
  231. pthread_mutex_unlock (&logsys_config_mutex);
  232. }
  233. static void _log_printf (
  234. enum logsys_config_mutex_state config_mutex_state,
  235. char *file,
  236. int line,
  237. int priority,
  238. int id,
  239. char *format,
  240. va_list ap)
  241. {
  242. char newstring[4096];
  243. char log_string[4096];
  244. char char_time[512];
  245. struct timeval tv;
  246. int i = 0;
  247. int len;
  248. struct log_data log_data;
  249. unsigned int res = 0;
  250. assert (id < MAX_LOGGERS);
  251. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  252. pthread_mutex_lock (&logsys_config_mutex);
  253. }
  254. pthread_mutex_lock (&logsys_new_log_mutex);
  255. /*
  256. ** Buffer before log has been configured has been called.
  257. */
  258. if (logsys_mode & LOG_MODE_BUFFER_BEFORE_CONFIG) {
  259. buffered_log_printf(file, line, logsys_mkpri(priority, id), format, ap);
  260. pthread_mutex_unlock (&logsys_new_log_mutex);
  261. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  262. pthread_mutex_unlock (&logsys_config_mutex);
  263. }
  264. return;
  265. }
  266. if (((logsys_mode & LOG_MODE_OUTPUT_FILE) || (logsys_mode & LOG_MODE_OUTPUT_STDERR)) &&
  267. (logsys_mode & LOG_MODE_DISPLAY_TIMESTAMP)) {
  268. gettimeofday (&tv, NULL);
  269. strftime (char_time, sizeof (char_time), "%b %e %k:%M:%S",
  270. localtime (&tv.tv_sec));
  271. i = sprintf (newstring, "%s.%06ld ", char_time, (long)tv.tv_usec);
  272. }
  273. if ((priority == LOG_LEVEL_DEBUG) || (logsys_mode & LOG_MODE_DISPLAY_FILELINE)) {
  274. sprintf (&newstring[i], "[%s:%04u] %s", file, line, format);
  275. } else {
  276. if (logsys_nosubsys == 1) {
  277. sprintf (&newstring[i], "%s", format);
  278. } else {
  279. sprintf (&newstring[i], "[%-5s] %s", logsys_loggers[id].subsys, format);
  280. }
  281. }
  282. if (dropped_log_entries) {
  283. /*
  284. * Get rid of \n if there is one
  285. */
  286. if (newstring[strlen (newstring) - 1] == '\n') {
  287. newstring[strlen (newstring) - 1] = '\0';
  288. }
  289. len = sprintf (log_string,
  290. "%s - prior to this log entry, openais logger dropped '%d' messages because of overflow.", newstring, dropped_log_entries + 1);
  291. } else {
  292. len = vsprintf (log_string, newstring, ap);
  293. }
  294. /*
  295. ** add line feed if not done yet
  296. */
  297. if (log_string[len - 1] != '\n') {
  298. log_string[len] = '\n';
  299. log_string[len + 1] = '\0';
  300. }
  301. /*
  302. * Create work thread data
  303. */
  304. log_data.syslog_pos = i;
  305. log_data.priority = priority;
  306. log_data.log_string = strdup (log_string);
  307. if (log_data.log_string == NULL) {
  308. goto drop_log_msg;
  309. }
  310. if (logsys_wthread_active) {
  311. res = worker_thread_group_work_add (&log_thread_group, &log_data);
  312. if (res == 0) {
  313. dropped_log_entries = 0;
  314. } else {
  315. dropped_log_entries += 1;
  316. }
  317. } else {
  318. log_printf_worker_fn (NULL, &log_data);
  319. }
  320. pthread_mutex_unlock (&logsys_new_log_mutex);
  321. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  322. pthread_mutex_unlock (&logsys_config_mutex);
  323. }
  324. return;
  325. drop_log_msg:
  326. dropped_log_entries++;
  327. pthread_mutex_unlock (&logsys_new_log_mutex);
  328. if (config_mutex_state == LOGSYS_CONFIG_MUTEX_UNLOCKED) {
  329. pthread_mutex_unlock (&logsys_config_mutex);
  330. }
  331. }
  332. unsigned int _logsys_subsys_create (
  333. const char *subsys,
  334. unsigned int priority)
  335. {
  336. assert (subsys != NULL);
  337. return logsys_config_subsys_set (
  338. subsys,
  339. LOGSYS_TAG_LOG,
  340. priority);
  341. }
  342. void logsys_config_mode_set (unsigned int mode)
  343. {
  344. pthread_mutex_lock (&logsys_config_mutex);
  345. logsys_mode = mode;
  346. if (mode & LOG_MODE_FLUSH_AFTER_CONFIG) {
  347. _logsys_wthread_create ();
  348. logsys_buffer_flush ();
  349. }
  350. pthread_mutex_unlock (&logsys_config_mutex);
  351. }
  352. int logsys_config_file_set (char **error_string, char *file)
  353. {
  354. static char error_string_response[512];
  355. if (file == NULL) {
  356. return (0);
  357. }
  358. pthread_mutex_lock (&logsys_new_log_mutex);
  359. pthread_mutex_lock (&logsys_config_mutex);
  360. if (logsys_mode & LOG_MODE_OUTPUT_FILE) {
  361. logsys_file = file;
  362. if (logsys_file_fp != NULL) {
  363. fclose (logsys_file_fp);
  364. }
  365. logsys_file_fp = fopen (file, "a+");
  366. if (logsys_file_fp == 0) {
  367. sprintf (error_string_response,
  368. "Can't open logfile '%s' for reason (%s).\n",
  369. file, strerror (errno));
  370. *error_string = error_string_response;
  371. pthread_mutex_unlock (&logsys_config_mutex);
  372. pthread_mutex_unlock (&logsys_new_log_mutex);
  373. return (-1);
  374. }
  375. }
  376. pthread_mutex_unlock (&logsys_config_mutex);
  377. pthread_mutex_unlock (&logsys_new_log_mutex);
  378. return (0);
  379. }
  380. void logsys_config_facility_set (char *name, unsigned int facility)
  381. {
  382. pthread_mutex_lock (&logsys_new_log_mutex);
  383. pthread_mutex_lock (&logsys_config_mutex);
  384. logsys_name = name;
  385. logsys_facility = facility;
  386. pthread_mutex_unlock (&logsys_config_mutex);
  387. pthread_mutex_unlock (&logsys_new_log_mutex);
  388. }
  389. void logsys_config_priority_set (unsigned int priority)
  390. {
  391. unsigned int tags;
  392. unsigned int dummy_priority;
  393. pthread_mutex_lock (&logsys_new_log_mutex);
  394. logsys_config_subsys_get ("MAIN", &tags, &dummy_priority);
  395. logsys_config_subsys_set ("MAIN", tags, priority);
  396. pthread_mutex_unlock (&logsys_new_log_mutex);
  397. }
  398. int _logsys_wthread_create (void)
  399. {
  400. worker_thread_group_init (
  401. &log_thread_group,
  402. 1,
  403. 1024,
  404. sizeof (struct log_data),
  405. 0,
  406. NULL,
  407. log_printf_worker_fn);
  408. logsys_flush();
  409. atexit (logsys_atexit);
  410. if (logsys_mode & LOG_MODE_OUTPUT_SYSLOG_THREADED && logsys_name != NULL) {
  411. openlog (logsys_name, LOG_CONS|LOG_PID, logsys_facility);
  412. }
  413. logsys_wthread_active = 1;
  414. return (0);
  415. }
  416. void logsys_log_printf (
  417. char *file,
  418. int line,
  419. int priority,
  420. char *format,
  421. ...)
  422. {
  423. int id = LOG_ID(priority);
  424. int level = LOG_LEVEL(priority);
  425. va_list ap;
  426. assert (id < MAX_LOGGERS);
  427. if (LOG_LEVEL(priority) > logsys_loggers[id].priority) {
  428. return;
  429. }
  430. va_start (ap, format);
  431. _log_printf (LOGSYS_CONFIG_MUTEX_UNLOCKED, file, line, level, id,
  432. format, ap);
  433. va_end(ap);
  434. }
  435. static void logsys_log_printf_locked (
  436. char *file,
  437. int line,
  438. int priority,
  439. char *format,
  440. ...)
  441. {
  442. int id = LOG_ID(priority);
  443. int level = LOG_LEVEL(priority);
  444. va_list ap;
  445. assert (id < MAX_LOGGERS);
  446. if (LOG_LEVEL(priority) > logsys_loggers[id].priority) {
  447. return;
  448. }
  449. va_start (ap, format);
  450. _log_printf (LOGSYS_CONFIG_MUTEX_LOCKED, file, line, level, id,
  451. format, ap);
  452. va_end(ap);
  453. }
  454. void _logsys_log_printf2 (
  455. char *file,
  456. int line,
  457. int priority,
  458. int id,
  459. char *format, ...)
  460. {
  461. va_list ap;
  462. assert (id < MAX_LOGGERS);
  463. va_start (ap, format);
  464. _log_printf (LOGSYS_CONFIG_MUTEX_UNLOCKED, file, line, priority, id,
  465. format, ap);
  466. va_end(ap);
  467. }
  468. void _logsys_trace (char *file, int line, int tag, int id, char *format, ...)
  469. {
  470. assert (id < MAX_LOGGERS);
  471. pthread_mutex_lock (&logsys_config_mutex);
  472. if (tag & logsys_loggers[id].tags) {
  473. va_list ap;
  474. va_start (ap, format);
  475. _log_printf (LOGSYS_CONFIG_MUTEX_LOCKED, file, line,
  476. LOG_LEVEL_DEBUG, id, format, ap);
  477. va_end(ap);
  478. }
  479. pthread_mutex_unlock (&logsys_config_mutex);
  480. }
  481. static void logsys_atexit (void)
  482. {
  483. if (logsys_wthread_active) {
  484. worker_thread_group_wait (&log_thread_group);
  485. }
  486. if (logsys_mode & LOG_MODE_OUTPUT_SYSLOG_THREADED) {
  487. closelog ();
  488. }
  489. }
  490. static void logsys_buffer_flush (void)
  491. {
  492. struct log_entry *entry = head;
  493. struct log_entry *tmp;
  494. if (logsys_mode & LOG_MODE_FLUSH_AFTER_CONFIG) {
  495. logsys_mode &= ~LOG_MODE_FLUSH_AFTER_CONFIG;
  496. while (entry) {
  497. logsys_log_printf_locked (
  498. entry->file,
  499. entry->line,
  500. entry->priority,
  501. entry->str);
  502. tmp = entry;
  503. entry = entry->next;
  504. free (tmp);
  505. }
  506. }
  507. head = tail = NULL;
  508. }
  509. void logsys_flush (void)
  510. {
  511. worker_thread_group_wait (&log_thread_group);
  512. }