logsys.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * Copyright (c) 2002-2004 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2010 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/engine/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. /*
  72. * need unlogical order to preserve 64bit alignment
  73. */
  74. struct logsys_logger {
  75. char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */
  76. char *logfile; /* log to file */
  77. unsigned int mode; /* subsystem mode */
  78. unsigned int debug; /* debug on|off */
  79. int syslog_priority; /* priority */
  80. int logfile_priority; /* priority to file */
  81. int init_status; /* internal field to handle init queues
  82. for subsystems */
  83. int32_t target_id;
  84. char *files[MAX_FILES_PER_SUBSYS];
  85. int32_t file_idx;
  86. int32_t dirty;
  87. };
  88. /* values for logsys_logger init_status */
  89. #define LOGSYS_LOGGER_INIT_DONE 0
  90. #define LOGSYS_LOGGER_NEEDS_INIT 1
  91. static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT;
  92. static struct logsys_logger logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT + 1];
  93. static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER;
  94. static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode);
  95. static void _logsys_config_apply_per_file(int32_t s, const char *filename);
  96. static void _logsys_config_apply_per_subsys(int32_t s);
  97. static char *format_buffer=NULL;
  98. static int _logsys_config_subsys_get_unlocked (const char *subsys)
  99. {
  100. unsigned int i;
  101. if (!subsys) {
  102. return LOGSYS_MAX_SUBSYS_COUNT;
  103. }
  104. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  105. if (strcmp (logsys_loggers[i].subsys, subsys) == 0) {
  106. return i;
  107. }
  108. }
  109. return (-1);
  110. }
  111. /*
  112. * we need a version that can work when somebody else is already
  113. * holding a config mutex lock or we will never get out of here
  114. */
  115. static int logsys_config_file_set_unlocked (
  116. int subsysid,
  117. const char **error_string,
  118. const char *file)
  119. {
  120. static char error_string_response[512];
  121. int i;
  122. if (logsys_loggers[subsysid].target_id > 0) {
  123. /* TODO close file
  124. logsys_filter_apply(subsysid,
  125. QB_LOG_FILTER_REMOVE,
  126. logsys_loggers[subsysid].target_id);
  127. */
  128. }
  129. logsys_loggers[subsysid].dirty = QB_TRUE;
  130. if (file == NULL) {
  131. return (0);
  132. }
  133. if (strlen(file) >= PATH_MAX) {
  134. snprintf (error_string_response,
  135. sizeof(error_string_response),
  136. "%s: logfile name exceed maximum system filename lenght\n",
  137. logsys_loggers[subsysid].subsys);
  138. *error_string = error_string_response;
  139. return (-1);
  140. }
  141. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  142. if ((logsys_loggers[i].logfile != NULL) &&
  143. (strcmp (logsys_loggers[i].logfile, file) == 0) &&
  144. (i != subsysid)) {
  145. /* we have found another subsys with this config file
  146. * so add a filter
  147. */
  148. logsys_loggers[subsysid].target_id = logsys_loggers[i].target_id;
  149. return (0);
  150. }
  151. }
  152. logsys_loggers[subsysid].logfile = strdup(file);
  153. if (logsys_loggers[subsysid].logfile == NULL) {
  154. snprintf (error_string_response,
  155. sizeof(error_string_response),
  156. "Unable to allocate memory for logfile '%s'\n",
  157. file);
  158. *error_string = error_string_response;
  159. return (-1);
  160. }
  161. if (logsys_loggers[subsysid].target_id > 0) {
  162. /* no one else is using this close it */
  163. qb_log_file_close(logsys_loggers[subsysid].target_id);
  164. }
  165. logsys_loggers[subsysid].target_id = qb_log_file_open(file);
  166. if (logsys_loggers[subsysid].target_id < 0) {
  167. int err = logsys_loggers[subsysid].target_id;
  168. char error_str[LOGSYS_MAX_PERROR_MSG_LEN];
  169. const char *error_ptr;
  170. error_ptr = qb_strerror_r(err, error_str, sizeof(error_str));
  171. free(logsys_loggers[subsysid].logfile);
  172. logsys_loggers[subsysid].logfile = NULL;
  173. snprintf (error_string_response,
  174. sizeof(error_string_response),
  175. "Can't open logfile '%s' for reason: %s (%d).\n",
  176. file, error_ptr, err);
  177. *error_string = error_string_response;
  178. return (-1);
  179. }
  180. qb_log_format_set(logsys_loggers[subsysid].target_id, format_buffer);
  181. return (0);
  182. }
  183. static void logsys_subsys_init (
  184. const char *subsys,
  185. int subsysid)
  186. {
  187. if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) {
  188. logsys_loggers[subsysid].init_status =
  189. LOGSYS_LOGGER_NEEDS_INIT;
  190. } else {
  191. logsys_loggers[subsysid].mode = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode;
  192. logsys_loggers[subsysid].debug = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].debug;
  193. logsys_loggers[subsysid].syslog_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_priority;
  194. logsys_loggers[subsysid].logfile_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].logfile_priority;
  195. logsys_loggers[subsysid].init_status = LOGSYS_LOGGER_INIT_DONE;
  196. }
  197. strncpy (logsys_loggers[subsysid].subsys, subsys,
  198. sizeof (logsys_loggers[subsysid].subsys));
  199. logsys_loggers[subsysid].subsys[
  200. sizeof (logsys_loggers[subsysid].subsys) - 1] = '\0';
  201. logsys_loggers[subsysid].file_idx = 0;
  202. }
  203. static const char *_logsys_tags_stringify(uint32_t tags)
  204. {
  205. if (tags == QB_LOG_TAG_LIBQB_MSG) {
  206. return "QB";
  207. } else {
  208. return logsys_loggers[tags].subsys;
  209. }
  210. }
  211. /*
  212. * Internal API - exported
  213. */
  214. int _logsys_system_setup(
  215. const char *mainsystem,
  216. unsigned int mode,
  217. int syslog_facility,
  218. int syslog_priority)
  219. {
  220. int i;
  221. int32_t fidx;
  222. char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN];
  223. if ((mainsystem == NULL) ||
  224. (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  225. return -1;
  226. }
  227. i = LOGSYS_MAX_SUBSYS_COUNT;
  228. pthread_mutex_lock (&logsys_config_mutex);
  229. snprintf(logsys_loggers[i].subsys,
  230. LOGSYS_MAX_SUBSYS_NAMELEN,
  231. "%s", mainsystem);
  232. logsys_loggers[i].mode = mode;
  233. logsys_loggers[i].debug = 0;
  234. logsys_loggers[i].file_idx = 0;
  235. logsys_loggers[i].logfile_priority = syslog_priority;
  236. logsys_loggers[i].syslog_priority = syslog_priority;
  237. qb_log_init(mainsystem, syslog_facility, syslog_priority);
  238. if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_STDERR) {
  239. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  240. } else {
  241. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
  242. }
  243. if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_SYSLOG) {
  244. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
  245. } else {
  246. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  247. }
  248. qb_log_filter_ctl(QB_LOG_BLACKBOX, QB_LOG_FILTER_ADD,
  249. QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  250. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 4096);
  251. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_THREADED, QB_FALSE);
  252. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
  253. logsys_format_set(NULL);
  254. qb_log_tags_stringify_fn_set(_logsys_tags_stringify);
  255. logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE;
  256. logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE;
  257. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  258. if ((strcmp (logsys_loggers[i].subsys, "") != 0) &&
  259. (logsys_loggers[i].init_status ==
  260. LOGSYS_LOGGER_NEEDS_INIT)) {
  261. fidx = logsys_loggers[i].file_idx;
  262. strncpy (tempsubsys, logsys_loggers[i].subsys,
  263. sizeof (tempsubsys));
  264. tempsubsys[sizeof (tempsubsys) - 1] = '\0';
  265. logsys_subsys_init(tempsubsys, i);
  266. logsys_loggers[i].file_idx = fidx;
  267. _logsys_config_mode_set_unlocked(i, logsys_loggers[i].mode);
  268. _logsys_config_apply_per_subsys(i);
  269. }
  270. }
  271. pthread_mutex_unlock (&logsys_config_mutex);
  272. return (0);
  273. }
  274. static void _logsys_subsys_filename_add (int32_t s, const char *filename)
  275. {
  276. int i;
  277. if (filename == NULL) {
  278. return;
  279. }
  280. assert(logsys_loggers[s].file_idx < MAX_FILES_PER_SUBSYS);
  281. assert(logsys_loggers[s].file_idx >= 0);
  282. for (i = 0; i < logsys_loggers[s].file_idx; i++) {
  283. if (strcmp(logsys_loggers[s].files[i], filename) == 0) {
  284. return;
  285. }
  286. }
  287. logsys_loggers[s].files[logsys_loggers[s].file_idx++] = strdup(filename);
  288. if (logsys_system_needs_init == LOGSYS_LOGGER_INIT_DONE) {
  289. _logsys_config_apply_per_file(s, filename);
  290. }
  291. }
  292. int _logsys_subsys_create (const char *subsys, const char *filename)
  293. {
  294. int i;
  295. if ((subsys == NULL) ||
  296. (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) {
  297. return -1;
  298. }
  299. pthread_mutex_lock (&logsys_config_mutex);
  300. i = _logsys_config_subsys_get_unlocked (subsys);
  301. if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) {
  302. _logsys_subsys_filename_add(i, filename);
  303. pthread_mutex_unlock (&logsys_config_mutex);
  304. return i;
  305. }
  306. for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) {
  307. if (strcmp (logsys_loggers[i].subsys, "") == 0) {
  308. logsys_subsys_init(subsys, i);
  309. _logsys_subsys_filename_add(i, filename);
  310. break;
  311. }
  312. }
  313. if (i >= LOGSYS_MAX_SUBSYS_COUNT) {
  314. i = -1;
  315. }
  316. pthread_mutex_unlock (&logsys_config_mutex);
  317. return i;
  318. }
  319. int _logsys_config_subsys_get (const char *subsys)
  320. {
  321. unsigned int i;
  322. pthread_mutex_lock (&logsys_config_mutex);
  323. i = _logsys_config_subsys_get_unlocked (subsys);
  324. pthread_mutex_unlock (&logsys_config_mutex);
  325. return i;
  326. }
  327. static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode)
  328. {
  329. if ( logsys_loggers[subsysid].mode == new_mode) {
  330. return 0;
  331. }
  332. if (logsys_loggers[subsysid].target_id > 0) {
  333. qb_log_ctl(logsys_loggers[subsysid].target_id,
  334. QB_LOG_CONF_ENABLED,
  335. (new_mode & LOGSYS_MODE_OUTPUT_FILE));
  336. }
  337. if (subsysid == LOGSYS_MAX_SUBSYS_COUNT) {
  338. qb_log_ctl(QB_LOG_STDERR,
  339. QB_LOG_CONF_ENABLED,
  340. (new_mode & LOGSYS_MODE_OUTPUT_STDERR));
  341. qb_log_ctl(QB_LOG_SYSLOG,
  342. QB_LOG_CONF_ENABLED,
  343. (new_mode & LOGSYS_MODE_OUTPUT_SYSLOG));
  344. }
  345. logsys_loggers[subsysid].mode = new_mode;
  346. return 0;
  347. }
  348. int logsys_config_mode_set (const char *subsys, unsigned int mode)
  349. {
  350. int i;
  351. pthread_mutex_lock (&logsys_config_mutex);
  352. if (subsys != NULL) {
  353. i = _logsys_config_subsys_get_unlocked (subsys);
  354. if (i >= 0) {
  355. i = _logsys_config_mode_set_unlocked(i, mode);
  356. }
  357. } else {
  358. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  359. _logsys_config_mode_set_unlocked(i, mode);
  360. }
  361. i = 0;
  362. }
  363. pthread_mutex_unlock (&logsys_config_mutex);
  364. return i;
  365. }
  366. unsigned int logsys_config_mode_get (const char *subsys)
  367. {
  368. int i;
  369. i = _logsys_config_subsys_get (subsys);
  370. if (i < 0) {
  371. return i;
  372. }
  373. return logsys_loggers[i].mode;
  374. }
  375. int logsys_config_file_set (
  376. const char *subsys,
  377. const char **error_string,
  378. const char *file)
  379. {
  380. int i;
  381. int res;
  382. pthread_mutex_lock (&logsys_config_mutex);
  383. if (subsys != NULL) {
  384. i = _logsys_config_subsys_get_unlocked (subsys);
  385. if (i < 0) {
  386. res = i;
  387. } else {
  388. res = logsys_config_file_set_unlocked(i, error_string, file);
  389. }
  390. } else {
  391. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  392. res = logsys_config_file_set_unlocked(i, error_string, file);
  393. if (res < 0) {
  394. break;
  395. }
  396. }
  397. }
  398. pthread_mutex_unlock (&logsys_config_mutex);
  399. return res;
  400. }
  401. int logsys_format_set (const char *format)
  402. {
  403. int ret = 0;
  404. int i;
  405. int c;
  406. int w;
  407. int reminder;
  408. char syslog_format[128];
  409. if (format_buffer) {
  410. free(format_buffer);
  411. format_buffer = NULL;
  412. }
  413. format_buffer = strdup(format ? format : "%7p [%6g] %b");
  414. if (format_buffer == NULL) {
  415. ret = -1;
  416. }
  417. qb_log_format_set(QB_LOG_STDERR, format_buffer);
  418. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  419. if (logsys_loggers[i].target_id > 0) {
  420. qb_log_format_set(logsys_loggers[i].target_id, format_buffer);
  421. }
  422. }
  423. /*
  424. * This just goes through and remove %t and %p from
  425. * the format string for syslog.
  426. */
  427. w = 0;
  428. memset(syslog_format, '\0', sizeof(syslog_format));
  429. for (c = 0; c < strlen(format_buffer); c++) {
  430. if (format_buffer[c] == '%') {
  431. reminder = c;
  432. for (c++; c < strlen(format_buffer); c++) {
  433. if (isdigit(format_buffer[c])) {
  434. continue;
  435. }
  436. if (format_buffer[c] == 't' ||
  437. format_buffer[c] == 'p') {
  438. c++;
  439. } else {
  440. c = reminder;
  441. }
  442. break;
  443. }
  444. }
  445. syslog_format[w] = format_buffer[c];
  446. w++;
  447. }
  448. // printf("normal_format: %s\n", format_buffer);
  449. // printf("syslog_format: %s\n", syslog_format);
  450. qb_log_format_set(QB_LOG_SYSLOG, syslog_format);
  451. return ret;
  452. }
  453. char *logsys_format_get (void)
  454. {
  455. return format_buffer;
  456. }
  457. int logsys_config_syslog_facility_set (
  458. const char *subsys,
  459. unsigned int facility)
  460. {
  461. return qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, facility);
  462. }
  463. int logsys_config_syslog_priority_set (
  464. const char *subsys,
  465. unsigned int priority)
  466. {
  467. int i;
  468. pthread_mutex_lock (&logsys_config_mutex);
  469. if (subsys != NULL) {
  470. i = _logsys_config_subsys_get_unlocked (subsys);
  471. if (i >= 0) {
  472. logsys_loggers[i].syslog_priority = priority;
  473. logsys_loggers[i].dirty = QB_TRUE;
  474. i = 0;
  475. }
  476. } else {
  477. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  478. logsys_loggers[i].syslog_priority = priority;
  479. logsys_loggers[i].dirty = QB_TRUE;
  480. }
  481. i = 0;
  482. }
  483. pthread_mutex_unlock (&logsys_config_mutex);
  484. return i;
  485. }
  486. int logsys_config_logfile_priority_set (
  487. const char *subsys,
  488. unsigned int priority)
  489. {
  490. int i;
  491. pthread_mutex_lock (&logsys_config_mutex);
  492. if (subsys != NULL) {
  493. i = _logsys_config_subsys_get_unlocked (subsys);
  494. if (i >= 0) {
  495. logsys_loggers[i].logfile_priority = priority;
  496. logsys_loggers[i].dirty = QB_TRUE;
  497. i = 0;
  498. }
  499. } else {
  500. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  501. logsys_loggers[i].logfile_priority = priority;
  502. logsys_loggers[i].dirty = QB_TRUE;
  503. }
  504. i = 0;
  505. }
  506. pthread_mutex_unlock (&logsys_config_mutex);
  507. return i;
  508. }
  509. static void _logsys_config_apply_per_file(int32_t s, const char *filename)
  510. {
  511. uint32_t syslog_priority = logsys_loggers[s].syslog_priority;
  512. uint32_t logfile_priority = logsys_loggers[s].logfile_priority;
  513. qb_log_filter_ctl(s, QB_LOG_TAG_SET, QB_LOG_FILTER_FILE,
  514. filename, LOG_TRACE);
  515. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE,
  516. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  517. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_REMOVE,
  518. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  519. if (logsys_loggers[s].target_id > 0) {
  520. qb_log_filter_ctl(logsys_loggers[s].target_id,
  521. QB_LOG_FILTER_REMOVE,
  522. QB_LOG_FILTER_FILE, filename, LOG_TRACE);
  523. }
  524. if (logsys_loggers[s].debug) {
  525. syslog_priority = LOG_DEBUG;
  526. logfile_priority = LOG_DEBUG;
  527. }
  528. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD,
  529. QB_LOG_FILTER_FILE, filename,
  530. syslog_priority);
  531. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  532. QB_LOG_FILTER_FILE, filename,
  533. logfile_priority);
  534. if (logsys_loggers[s].target_id > 0) {
  535. qb_log_filter_ctl(logsys_loggers[s].target_id,
  536. QB_LOG_FILTER_ADD,
  537. QB_LOG_FILTER_FILE, filename,
  538. logfile_priority);
  539. }
  540. }
  541. static void _logsys_config_apply_per_subsys(int32_t s)
  542. {
  543. int32_t f;
  544. for (f = 0; f < logsys_loggers[s].file_idx; f++) {
  545. _logsys_config_apply_per_file(s, logsys_loggers[s].files[f]);
  546. }
  547. logsys_loggers[s].dirty = QB_FALSE;
  548. }
  549. void logsys_config_apply(void)
  550. {
  551. int32_t s;
  552. for (s = 0; s <= LOGSYS_MAX_SUBSYS_COUNT; s++) {
  553. if (strcmp(logsys_loggers[s].subsys, "") == 0) {
  554. continue;
  555. }
  556. _logsys_config_apply_per_subsys(s);
  557. }
  558. }
  559. int logsys_config_debug_set (
  560. const char *subsys,
  561. unsigned int debug)
  562. {
  563. int i;
  564. pthread_mutex_lock (&logsys_config_mutex);
  565. if (subsys != NULL) {
  566. i = _logsys_config_subsys_get_unlocked (subsys);
  567. if (i >= 0) {
  568. logsys_loggers[i].dirty = QB_TRUE;
  569. logsys_loggers[i].debug = debug;
  570. i = 0;
  571. }
  572. } else {
  573. for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) {
  574. logsys_loggers[i].debug = debug;
  575. logsys_loggers[i].dirty = QB_TRUE;
  576. }
  577. i = 0;
  578. }
  579. pthread_mutex_unlock (&logsys_config_mutex);
  580. return i;
  581. }
  582. int logsys_priority_id_get (const char *name)
  583. {
  584. unsigned int i;
  585. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  586. if (strcasecmp(name, prioritynames[i].c_name) == 0) {
  587. return (prioritynames[i].c_val);
  588. }
  589. }
  590. return (-1);
  591. }
  592. const char *logsys_priority_name_get (unsigned int priority)
  593. {
  594. unsigned int i;
  595. for (i = 0; prioritynames[i].c_name != NULL; i++) {
  596. if (priority == prioritynames[i].c_val) {
  597. return (prioritynames[i].c_name);
  598. }
  599. }
  600. return (NULL);
  601. }