logsys.c 19 KB

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