logsys.c 18 KB

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