logsys.c 21 KB

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