logsys.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2012 Red Hat, Inc.
  4. *
  5. * Author: Steven Dake (sdake@redhat.com)
  6. * Author: Lon Hohberger (lhh@redhat.com)
  7. * Author: Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * All rights reserved.
  10. *
  11. * This software licensed under BSD license, the text of which follows:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * - Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  22. * contributors may be used to endorse or promote products derived from this
  23. * software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. * THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include <config.h>
  38. #include <stdint.h>
  39. #include <ctype.h>
  40. #include <assert.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <qb/qbdefs.h>
  44. #include <qb/qbutil.h>
  45. #include <qb/qblog.h>
  46. #include <corosync/list.h>
  47. #include <corosync/logsys.h>
  48. /*
  49. * syslog prioritynames, facility names to value mapping
  50. * Some C libraries build this in to their headers, but it is non-portable
  51. * so logsys supplies its own version.
  52. */
  53. struct syslog_names {
  54. const char *c_name;
  55. int c_val;
  56. };
  57. static struct syslog_names prioritynames[] =
  58. {
  59. { "alert", LOG_ALERT },
  60. { "crit", LOG_CRIT },
  61. { "debug", LOG_DEBUG },
  62. { "emerg", LOG_EMERG },
  63. { "err", LOG_ERR },
  64. { "error", LOG_ERR },
  65. { "info", LOG_INFO },
  66. { "notice", LOG_NOTICE },
  67. { "warning", LOG_WARNING },
  68. { NULL, -1 }
  69. };
  70. #define MAX_FILES_PER_SUBSYS 16
  71. #ifdef HAVE_SMALL_MEMORY_FOOTPRINT
  72. #define IPC_LOGSYS_SIZE 8192*64
  73. #else
  74. #define IPC_LOGSYS_SIZE 8192*1024
  75. #endif
  76. /*
  77. * need unlogical order to preserve 64bit alignment
  78. */
  79. struct logsys_logger {
  80. char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */
  81. char *logfile; /* log to file */
  82. unsigned int mode; /* subsystem mode */
  83. unsigned int debug; /* debug on|off */
  84. int syslog_priority; /* priority */
  85. int logfile_priority; /* priority to file */
  86. int init_status; /* internal field to handle init queues
  87. for subsystems */
  88. int32_t target_id;
  89. char *files[MAX_FILES_PER_SUBSYS];
  90. int32_t file_idx;
  91. int32_t dirty;
  92. };
  93. /* values for logsys_logger init_status */
  94. #define LOGSYS_LOGGER_INIT_DONE 0
  95. #define LOGSYS_LOGGER_NEEDS_INIT 1
  96. static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
  97. static struct logsys_logger logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT + 1];
  98. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  99. static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode);
  100. static void _logsys_config_apply_per_file(int32_t s, const char *filename);
  101. static void _logsys_config_apply_per_subsys(int32_t s);
  102. static void _logsys_subsys_filename_add (int32_t s, const char *filename);
  103. static void logsys_file_format_get(char* file_format, int buf_len);
  104. static char *format_buffer=NULL;
  105. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  106. {
  107. unsigned int i;
  108. if (!subsys) {
  109. return LOGSYS_MAX_SUBSYS_COUNT;
  110. }
  111. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  112. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  113. return i;
  114. }
  115. }
  116. return (-1);
  117. }
  118. /*
  119. * we need a version that can work when somebody else is already
  120. * holding a config mutex lock or we will never get out of here
  121. */
  122. static int logsys_config_file_set_unlocked (
  123. int subsysid,
  124. const char **error_string,
  125. const char *file)
  126. {
  127. static char error_string_response[512];
  128. int i;
  129. char file_format[128];
  130. if (logsys_loggers[subsysid].target_id > 0) {
  131. /* TODO close file
  132. logsys_filter_apply(subsysid,
  133. QB_LOG_FILTER_REMOVE,
  134. logsys_loggers[subsysid].target_id);
  135. */
  136. }
  137. logsys_loggers[subsysid].dirty = QB_TRUE;
  138. if (file == NULL) {
  139. return (0);
  140. }
  141. if (strlen(file) >= PATH_MAX) {
  142. snprintf (error_string_response,
  143. sizeof(error_string_response),
  144. "%s: logfile name exceed maximum system filename lenght\n",
  145. logsys_loggers[subsysid].subsys);
  146. *error_string = error_string_response;
  147. return (-1);
  148. }
  149. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  150. if ((logsys_loggers[i].logfile != NULL) &&
  151. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  152. (i != subsysid)) {
  153. /* we have found another subsys with this config file
  154. * so add a filter
  155. */
  156. logsys_loggers[subsysid].target_id = logsys_loggers[i].target_id;
  157. return (0);
  158. }
  159. }
  160. logsys_loggers[subsysid].logfile = strdup(file);
  161. if (logsys_loggers[subsysid].logfile == NULL) {
  162. snprintf (error_string_response,
  163. sizeof(error_string_response),
  164. "Unable to allocate memory for logfile '%s'\n",
  165. file);
  166. *error_string = error_string_response;
  167. return (-1);
  168. }
  169. if (logsys_loggers[subsysid].target_id > 0) {
  170. /* no one else is using this close it */
  171. qb_log_file_close(logsys_loggers[subsysid].target_id);
  172. }
  173. logsys_loggers[subsysid].target_id = qb_log_file_open(file);
  174. if (logsys_loggers[subsysid].target_id < 0) {
  175. int err = -logsys_loggers[subsysid].target_id;
  176. char error_str[LOGSYS_MAX_PERROR_MSG_LEN];
  177. const char *error_ptr;
  178. error_ptr = qb_strerror_r(err, error_str, sizeof(error_str));
  179. free(logsys_loggers[subsysid].logfile);
  180. logsys_loggers[subsysid].logfile = NULL;
  181. snprintf (error_string_response,
  182. sizeof(error_string_response),
  183. "Can't open logfile '%s' for reason: %s (%d).\n",
  184. file, error_ptr, err);
  185. *error_string = error_string_response;
  186. return (-1);
  187. }
  188. logsys_file_format_get(file_format, 128);
  189. qb_log_format_set(logsys_loggers[subsysid].target_id, file_format);
  190. return (0);
  191. }
  192. static void logsys_subsys_init (
  193. const char *subsys,
  194. int subsysid)
  195. {
  196. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  197. logsys_loggers[subsysid].init_status =
  198. LOGSYS_LOGGER_NEEDS_INIT;
  199. } else {
  200. logsys_loggers[subsysid].mode = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode;
  201. logsys_loggers[subsysid].debug = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].debug;
  202. logsys_loggers[subsysid].syslog_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_priority;
  203. logsys_loggers[subsysid].logfile_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].logfile_priority;
  204. logsys_loggers[subsysid].init_status = LOGSYS_LOGGER_INIT_DONE;
  205. }
  206. strncpy (logsys_loggers[subsysid].subsys, subsys,
  207. sizeof (logsys_loggers[subsysid].subsys));
  208. logsys_loggers[subsysid].subsys[
  209. sizeof (logsys_loggers[subsysid].subsys) - 1] = '\0';
  210. logsys_loggers[subsysid].file_idx = 0;
  211. }
  212. static const char *_logsys_tags_stringify(uint32_t tags)
  213. {
  214. if (tags == QB_LOG_TAG_LIBQB_MSG) {
  215. return "QB";
  216. } else {
  217. return logsys_loggers[tags].subsys;
  218. }
  219. }
  220. void logsys_system_fini (void)
  221. {
  222. int i;
  223. int f;
  224. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  225. free(logsys_loggers[i].logfile);
  226. for (f = 0; f < logsys_loggers[i].file_idx; f++) {
  227. free(logsys_loggers[i].files[f]);
  228. }
  229. }
  230. qb_log_fini ();
  231. }
  232. /*
  233. * Internal API - exported
  234. */
  235. int _logsys_system_setup(
  236. const char *mainsystem,
  237. unsigned int mode,
  238. int syslog_facility,
  239. int syslog_priority)
  240. {
  241. int i;
  242. int32_t fidx;
  243. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  244. if ((mainsystem == NULL) ||
  245. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  246. return -1;
  247. }
  248. /*
  249. * Setup libqb as a subsys
  250. */
  251. i = _logsys_subsys_create ("QB", "loop");
  252. _logsys_subsys_filename_add (i, "ipc");
  253. _logsys_subsys_filename_add (i, "log");
  254. _logsys_subsys_filename_add (i, "trie.c");
  255. _logsys_subsys_filename_add (i, "map.c");
  256. _logsys_subsys_filename_add (i, "ringbuffer");
  257. i = LOGSYS_MAX_SUBSYS_COUNT;
  258. pthread_mutex_lock (&logsys_config_mutex);
  259. snprintf(logsys_loggers[i].subsys,
  260. LOGSYS_MAX_SUBSYS_NAMELEN,
  261. "%s", mainsystem);
  262. logsys_loggers[i].mode = mode;
  263. logsys_loggers[i].debug = 0;
  264. logsys_loggers[i].file_idx = 0;
  265. logsys_loggers[i].logfile_priority = syslog_priority;
  266. logsys_loggers[i].syslog_priority = syslog_priority;
  267. qb_log_init(mainsystem, syslog_facility, syslog_priority);
  268. if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_STDERR) {
  269. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  270. } else {
  271. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
  272. }
  273. if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_SYSLOG) {
  274. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
  275. } else {
  276. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  277. }
  278. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_PRIORITY_BUMP, LOG_INFO - LOG_DEBUG);
  279. qb_log_filter_ctl(QB_LOG_BLACKBOX, QB_LOG_FILTER_ADD,
  280. QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  281. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, IPC_LOGSYS_SIZE);
  282. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_THREADED, QB_FALSE);
  283. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
  284. logsys_format_set(NULL);
  285. qb_log_tags_stringify_fn_set(_logsys_tags_stringify);
  286. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  287. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  288. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  289. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  290. (logsys_loggers[i].init_status ==
  291. LOGSYS_LOGGER_NEEDS_INIT)) {
  292. fidx = logsys_loggers[i].file_idx;
  293. strncpy (tempsubsys, logsys_loggers[i].subsys,
  294. sizeof (tempsubsys));
  295. tempsubsys[sizeof (tempsubsys) - 1] = '\0';
  296. logsys_subsys_init(tempsubsys, i);
  297. logsys_loggers[i].file_idx = fidx;
  298. _logsys_config_mode_set_unlocked(i, logsys_loggers[i].mode);
  299. _logsys_config_apply_per_subsys(i);
  300. }
  301. }
  302. pthread_mutex_unlock (&logsys_config_mutex);
  303. return (0);
  304. }
  305. static void _logsys_subsys_filename_add (int32_t s, const char *filename)
  306. {
  307. int i;
  308. if (filename == NULL) {
  309. return;
  310. }
  311. assert(logsys_loggers[s].file_idx < MAX_FILES_PER_SUBSYS);
  312. assert(logsys_loggers[s].file_idx >= 0);
  313. for (i = 0; i < logsys_loggers[s].file_idx; i++) {
  314. if (strcmp(logsys_loggers[s].files[i], filename) == 0) {
  315. return;
  316. }
  317. }
  318. logsys_loggers[s].files[logsys_loggers[s].file_idx++] = strdup(filename);
  319. if (logsys_system_needs_init == LOGSYS_LOGGER_INIT_DONE) {
  320. _logsys_config_apply_per_file(s, filename);
  321. }
  322. }
  323. int _logsys_subsys_create (const char *subsys, const char *filename)
  324. {
  325. int i;
  326. if ((subsys == NULL) ||
  327. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  328. return -1;
  329. }
  330. pthread_mutex_lock (&logsys_config_mutex);
  331. i = _logsys_config_subsys_get_unlocked (subsys);
  332. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  333. _logsys_subsys_filename_add(i, filename);
  334. pthread_mutex_unlock (&logsys_config_mutex);
  335. return i;
  336. }
  337. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  338. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  339. logsys_subsys_init(subsys, i);
  340. _logsys_subsys_filename_add(i, filename);
  341. break;
  342. }
  343. }
  344. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  345. i = -1;
  346. }
  347. pthread_mutex_unlock (&logsys_config_mutex);
  348. return i;
  349. }
  350. int _logsys_config_subsys_get (const char *subsys)
  351. {
  352. unsigned int i;
  353. pthread_mutex_lock (&logsys_config_mutex);
  354. i = _logsys_config_subsys_get_unlocked (subsys);
  355. pthread_mutex_unlock (&logsys_config_mutex);
  356. return i;
  357. }
  358. static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode)
  359. {
  360. if ( logsys_loggers[subsysid].mode == new_mode) {
  361. return 0;
  362. }
  363. if (logsys_loggers[subsysid].target_id > 0) {
  364. qb_log_ctl(logsys_loggers[subsysid].target_id,
  365. QB_LOG_CONF_ENABLED,
  366. (new_mode & LOGSYS_MODE_OUTPUT_FILE));
  367. }
  368. if (subsysid == LOGSYS_MAX_SUBSYS_COUNT) {
  369. qb_log_ctl(QB_LOG_STDERR,
  370. QB_LOG_CONF_ENABLED,
  371. (new_mode & LOGSYS_MODE_OUTPUT_STDERR));
  372. qb_log_ctl(QB_LOG_SYSLOG,
  373. QB_LOG_CONF_ENABLED,
  374. (new_mode & LOGSYS_MODE_OUTPUT_SYSLOG));
  375. }
  376. logsys_loggers[subsysid].mode = new_mode;
  377. return 0;
  378. }
  379. int logsys_config_mode_set (const char *subsys, unsigned int mode)
  380. {
  381. int i;
  382. pthread_mutex_lock (&logsys_config_mutex);
  383. if (subsys != NULL) {
  384. i = _logsys_config_subsys_get_unlocked (subsys);
  385. if (i >= 0) {
  386. i = _logsys_config_mode_set_unlocked(i, mode);
  387. }
  388. } else {
  389. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  390. _logsys_config_mode_set_unlocked(i, mode);
  391. }
  392. i = 0;
  393. }
  394. pthread_mutex_unlock (&logsys_config_mutex);
  395. return i;
  396. }
  397. unsigned int logsys_config_mode_get (const char *subsys)
  398. {
  399. int i;
  400. i = _logsys_config_subsys_get (subsys);
  401. if (i < 0) {
  402. return i;
  403. }
  404. return logsys_loggers[i].mode;
  405. }
  406. int logsys_config_file_set (
  407. const char *subsys,
  408. const char **error_string,
  409. const char *file)
  410. {
  411. int i;
  412. int res;
  413. pthread_mutex_lock (&logsys_config_mutex);
  414. if (subsys != NULL) {
  415. i = _logsys_config_subsys_get_unlocked (subsys);
  416. if (i < 0) {
  417. res = i;
  418. } else {
  419. res = logsys_config_file_set_unlocked(i, error_string, file);
  420. }
  421. } else {
  422. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  423. res = logsys_config_file_set_unlocked(i, error_string, file);
  424. if (res < 0) {
  425. break;
  426. }
  427. }
  428. }
  429. pthread_mutex_unlock (&logsys_config_mutex);
  430. return res;
  431. }
  432. static void
  433. logsys_file_format_get(char* file_format, int buf_len)
  434. {
  435. char *per_t;
  436. file_format[0] = '\0';
  437. per_t = strstr(format_buffer, "%t");
  438. if (per_t) {
  439. strcpy(file_format, "%t [%P] %H %N");
  440. per_t += 2;
  441. strncat(file_format, per_t, buf_len - strlen("%t [%P] %H %N"));
  442. } else {
  443. strcpy(file_format, "[%P] %H %N");
  444. strncat(file_format, format_buffer, buf_len - strlen("[%P] %H %N"));
  445. }
  446. }
  447. int logsys_format_set (const char *format)
  448. {
  449. int ret = 0;
  450. int i;
  451. int c;
  452. int w;
  453. int reminder;
  454. char syslog_format[128];
  455. char file_format[128];
  456. if (format_buffer) {
  457. free(format_buffer);
  458. format_buffer = NULL;
  459. }
  460. format_buffer = strdup(format ? format : "%7p [%6g] %b");
  461. if (format_buffer == NULL) {
  462. ret = -1;
  463. }
  464. qb_log_format_set(QB_LOG_STDERR, format_buffer);
  465. logsys_file_format_get(file_format, 128);
  466. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  467. if (logsys_loggers[i].target_id > 0) {
  468. qb_log_format_set(logsys_loggers[i].target_id, file_format);
  469. }
  470. }
  471. /*
  472. * This just goes through and remove %t and %p from
  473. * the format string for syslog.
  474. */
  475. w = 0;
  476. memset(syslog_format, '\0', sizeof(syslog_format));
  477. for (c = 0; c < strlen(format_buffer); c++) {
  478. if (format_buffer[c] == '%') {
  479. reminder = c;
  480. for (c++; c < strlen(format_buffer); c++) {
  481. if (isdigit(format_buffer[c])) {
  482. continue;
  483. }
  484. if (format_buffer[c] == 't' ||
  485. format_buffer[c] == 'p') {
  486. c++;
  487. } else {
  488. c = reminder;
  489. }
  490. break;
  491. }
  492. }
  493. syslog_format[w] = format_buffer[c];
  494. w++;
  495. }
  496. // printf("normal_format: %s\n", format_buffer);
  497. // printf("syslog_format: %s\n", syslog_format);
  498. qb_log_format_set(QB_LOG_SYSLOG, syslog_format);
  499. return ret;
  500. }
  501. char *logsys_format_get (void)
  502. {
  503. return format_buffer;
  504. }
  505. int logsys_config_syslog_facility_set (
  506. const char *subsys,
  507. unsigned int facility)
  508. {
  509. return qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, facility);
  510. }
  511. int logsys_config_syslog_priority_set (
  512. const char *subsys,
  513. unsigned int priority)
  514. {
  515. int i;
  516. pthread_mutex_lock (&logsys_config_mutex);
  517. if (subsys != NULL) {
  518. i = _logsys_config_subsys_get_unlocked (subsys);
  519. if (i >= 0) {
  520. logsys_loggers[i].syslog_priority = priority;
  521. logsys_loggers[i].dirty = QB_TRUE;
  522. i = 0;
  523. }
  524. } else {
  525. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  526. logsys_loggers[i].syslog_priority = priority;
  527. logsys_loggers[i].dirty = QB_TRUE;
  528. }
  529. i = 0;
  530. }
  531. pthread_mutex_unlock (&logsys_config_mutex);
  532. return i;
  533. }
  534. int logsys_config_logfile_priority_set (
  535. const char *subsys,
  536. unsigned int priority)
  537. {
  538. int i;
  539. pthread_mutex_lock (&logsys_config_mutex);
  540. if (subsys != NULL) {
  541. i = _logsys_config_subsys_get_unlocked (subsys);
  542. if (i >= 0) {
  543. logsys_loggers[i].logfile_priority = priority;
  544. logsys_loggers[i].dirty = QB_TRUE;
  545. i = 0;
  546. }
  547. } else {
  548. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  549. logsys_loggers[i].logfile_priority = priority;
  550. logsys_loggers[i].dirty = QB_TRUE;
  551. }
  552. i = 0;
  553. }
  554. pthread_mutex_unlock (&logsys_config_mutex);
  555. return i;
  556. }
  557. static void _logsys_config_apply_per_file(int32_t s, const char *filename)
  558. {
  559. uint32_t syslog_priority = logsys_loggers[s].syslog_priority;
  560. uint32_t logfile_priority = logsys_loggers[s].logfile_priority;
  561. qb_log_filter_ctl(s, QB_LOG_TAG_SET, QB_LOG_FILTER_FILE,
  562. filename, LOG_TRACE);
  563. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE,
  564. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  565. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_REMOVE,
  566. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  567. if (logsys_loggers[s].target_id > 0) {
  568. qb_log_filter_ctl(logsys_loggers[s].target_id,
  569. QB_LOG_FILTER_REMOVE,
  570. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  571. }
  572. if (logsys_loggers[s].debug) {
  573. syslog_priority = LOG_DEBUG;
  574. logfile_priority = LOG_DEBUG;
  575. }
  576. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD,
  577. QB_LOG_FILTER_FILE, filename,
  578. syslog_priority);
  579. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  580. QB_LOG_FILTER_FILE, filename,
  581. logfile_priority);
  582. if (logsys_loggers[s].target_id > 0) {
  583. qb_log_filter_ctl(logsys_loggers[s].target_id,
  584. QB_LOG_FILTER_ADD,
  585. QB_LOG_FILTER_FILE, filename,
  586. logfile_priority);
  587. }
  588. }
  589. static void _logsys_config_apply_per_subsys(int32_t s)
  590. {
  591. int32_t f;
  592. for (f = 0; f < logsys_loggers[s].file_idx; f++) {
  593. _logsys_config_apply_per_file(s, logsys_loggers[s].files[f]);
  594. }
  595. logsys_loggers[s].dirty = QB_FALSE;
  596. }
  597. void logsys_config_apply(void)
  598. {
  599. int32_t s;
  600. for (s = 0; s <= LOGSYS_MAX_SUBSYS_COUNT; s++) {
  601. if (strcmp(logsys_loggers[s].subsys, "") == 0) {
  602. continue;
  603. }
  604. _logsys_config_apply_per_subsys(s);
  605. }
  606. }
  607. int logsys_config_debug_set (
  608. const char *subsys,
  609. unsigned int debug)
  610. {
  611. int i;
  612. pthread_mutex_lock (&logsys_config_mutex);
  613. if (subsys != NULL) {
  614. i = _logsys_config_subsys_get_unlocked (subsys);
  615. if (i >= 0) {
  616. logsys_loggers[i].dirty = QB_TRUE;
  617. logsys_loggers[i].debug = debug;
  618. i = 0;
  619. }
  620. } else {
  621. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  622. logsys_loggers[i].debug = debug;
  623. logsys_loggers[i].dirty = QB_TRUE;
  624. }
  625. i = 0;
  626. }
  627. pthread_mutex_unlock (&logsys_config_mutex);
  628. return i;
  629. }
  630. int logsys_priority_id_get (const char *name)
  631. {
  632. unsigned int i;
  633. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  634. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  635. return (prioritynames[i].c_val);
  636. }
  637. }
  638. return (-1);
  639. }