qdevice-log.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "qdevice-log.h"
  35. #include "qdevice-config.h"
  36. #include "utils.h"
  37. struct qdevice_log_syslog_names {
  38. const char *prio_name;
  39. int priority;
  40. };
  41. static struct qdevice_log_syslog_names qdevice_log_priority_names[] = {
  42. { "alert", LOG_ALERT },
  43. { "crit", LOG_CRIT },
  44. { "debug", LOG_DEBUG },
  45. { "emerg", LOG_EMERG },
  46. { "err", LOG_ERR },
  47. { "error", LOG_ERR },
  48. { "info", LOG_INFO },
  49. { "notice", LOG_NOTICE },
  50. { "warning", LOG_WARNING },
  51. { NULL, -1 }};
  52. static int
  53. qdevice_log_priority_str_to_int(const char *priority_str)
  54. {
  55. unsigned int i;
  56. for (i = 0; qdevice_log_priority_names[i].prio_name != NULL; i++) {
  57. if (strcasecmp(priority_str, qdevice_log_priority_names[i].prio_name) == 0) {
  58. return (qdevice_log_priority_names[i].priority);
  59. }
  60. }
  61. return (-1);
  62. }
  63. void
  64. qdevice_log_configure(struct qdevice_instance *instance)
  65. {
  66. int to_stderr;
  67. int to_syslog;
  68. int syslog_facility;
  69. int syslog_priority;
  70. int logfile_priority;
  71. int debug;
  72. char *str;
  73. int i;
  74. int fileline;
  75. int timestamp;
  76. int function_name;
  77. char log_format_syslog[64];
  78. char log_format_stderr[64];
  79. to_stderr = QDEVICE_LOG_DEFAULT_TO_STDERR;
  80. if (cmap_get_string(instance->cmap_handle, "logging.to_stderr", &str) == CS_OK) {
  81. if ((i = utils_parse_bool_str(str)) == -1) {
  82. qdevice_log(LOG_WARNING, "logging.to_stderr value is not valid");
  83. } else {
  84. to_stderr = i;
  85. }
  86. free(str);
  87. }
  88. if (cmap_get_string(instance->cmap_handle,
  89. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_stderr", &str) == CS_OK) {
  90. if ((i = utils_parse_bool_str(str)) == -1) {
  91. qdevice_log(LOG_WARNING,
  92. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_stderr value is not valid.");
  93. } else {
  94. to_stderr = i;
  95. }
  96. free(str);
  97. }
  98. to_syslog = QDEVICE_LOG_DEFAULT_TO_SYSLOG;
  99. if (cmap_get_string(instance->cmap_handle, "logging.to_syslog", &str) == CS_OK) {
  100. if ((i = utils_parse_bool_str(str)) == -1) {
  101. qdevice_log(LOG_WARNING, "logging.to_syslog value is not valid");
  102. } else {
  103. to_syslog = i;
  104. }
  105. free(str);
  106. }
  107. if (cmap_get_string(instance->cmap_handle,
  108. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_syslog", &str) == CS_OK) {
  109. if ((i = utils_parse_bool_str(str)) == -1) {
  110. qdevice_log(LOG_WARNING,
  111. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_syslog value is not valid.");
  112. } else {
  113. to_syslog = i;
  114. }
  115. free(str);
  116. }
  117. syslog_facility = QDEVICE_LOG_DEFAULT_SYSLOG_FACILITY;
  118. if (cmap_get_string(instance->cmap_handle, "logging.syslog_facility", &str) == CS_OK) {
  119. if ((i = qb_log_facility2int(str)) < 0) {
  120. qdevice_log(LOG_WARNING, "logging.syslog_facility value is not valid");
  121. } else {
  122. syslog_facility = i;
  123. }
  124. free(str);
  125. }
  126. if (cmap_get_string(instance->cmap_handle,
  127. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_facility", &str) == CS_OK) {
  128. if ((i = qb_log_facility2int(str)) < 0) {
  129. qdevice_log(LOG_WARNING,
  130. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_facility value is not valid.");
  131. } else {
  132. syslog_facility = i;
  133. }
  134. free(str);
  135. }
  136. syslog_priority = QDEVICE_LOG_DEFAULT_SYSLOG_PRIORITY;
  137. if (cmap_get_string(instance->cmap_handle, "logging.syslog_priority", &str) == CS_OK) {
  138. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  139. qdevice_log(LOG_WARNING, "logging.syslog_priority value is not valid");
  140. } else {
  141. syslog_priority = i;
  142. }
  143. free(str);
  144. }
  145. if (cmap_get_string(instance->cmap_handle,
  146. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_priority", &str) == CS_OK) {
  147. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  148. qdevice_log(LOG_WARNING,
  149. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_priority value is not valid.");
  150. } else {
  151. syslog_priority = i;
  152. }
  153. free(str);
  154. }
  155. logfile_priority = QDEVICE_LOG_DEFAULT_SYSLOG_PRIORITY;
  156. if (cmap_get_string(instance->cmap_handle, "logging.logfile_priority", &str) == CS_OK) {
  157. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  158. qdevice_log(LOG_WARNING, "logging.logfile_priority value is not valid");
  159. } else {
  160. logfile_priority = i;
  161. }
  162. free(str);
  163. }
  164. if (cmap_get_string(instance->cmap_handle,
  165. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".logfile_priority", &str) == CS_OK) {
  166. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  167. qdevice_log(LOG_WARNING,
  168. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".logfile_priority value is not valid.");
  169. } else {
  170. logfile_priority = i;
  171. }
  172. free(str);
  173. }
  174. debug = QDEVICE_LOG_DEFAULT_DEBUG;
  175. if (cmap_get_string(instance->cmap_handle, "logging.debug", &str) == CS_OK) {
  176. if ((i = utils_parse_bool_str(str)) == -1) {
  177. if (strcasecmp(str, "trace") == 0) {
  178. debug = 1;
  179. } else {
  180. qdevice_log(LOG_WARNING, "logging.debug value is not valid");
  181. }
  182. } else {
  183. debug = i;
  184. }
  185. free(str);
  186. }
  187. if (cmap_get_string(instance->cmap_handle,
  188. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".debug", &str) == CS_OK) {
  189. if ((i = utils_parse_bool_str(str)) == -1) {
  190. if (strcasecmp(str, "trace") == 0) {
  191. debug = 1;
  192. } else {
  193. qdevice_log(LOG_WARNING,
  194. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".debug value is not valid.");
  195. }
  196. } else {
  197. debug = i;
  198. }
  199. free(str);
  200. }
  201. fileline = QDEVICE_LOG_DEFAULT_FILELINE;
  202. if (cmap_get_string(instance->cmap_handle, "logging.fileline", &str) == CS_OK) {
  203. if ((i = utils_parse_bool_str(str)) == -1) {
  204. qdevice_log(LOG_WARNING, "logging.fileline value is not valid");
  205. } else {
  206. fileline = i;
  207. }
  208. free(str);
  209. }
  210. timestamp = QDEVICE_LOG_DEFAULT_TIMESTAMP;
  211. if (cmap_get_string(instance->cmap_handle, "logging.timestamp", &str) == CS_OK) {
  212. if ((i = utils_parse_bool_str(str)) == -1) {
  213. qdevice_log(LOG_WARNING, "logging.timestamp value is not valid");
  214. } else {
  215. timestamp = i;
  216. }
  217. free(str);
  218. }
  219. function_name = QDEVICE_LOG_DEFAULT_FUNCTION_NAME;
  220. if (cmap_get_string(instance->cmap_handle, "logging.function_name", &str) == CS_OK) {
  221. if ((i = utils_parse_bool_str(str)) == -1) {
  222. qdevice_log(LOG_WARNING, "logging.function_name value is not valid");
  223. } else {
  224. function_name = i;
  225. }
  226. free(str);
  227. }
  228. strcpy(log_format_syslog, "");
  229. if (fileline) {
  230. strcat(log_format_syslog, "%f:");
  231. if (function_name) {
  232. strcat(log_format_syslog, "%n:");
  233. }
  234. strcat(log_format_syslog, "%l ");
  235. }
  236. strcat(log_format_syslog, "%b");
  237. strcpy(log_format_stderr, "");
  238. if (timestamp) {
  239. strcpy(log_format_stderr, "%t %7p ");
  240. }
  241. strcat(log_format_stderr, log_format_syslog);
  242. /*
  243. * Finally reconfigure log system
  244. */
  245. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, to_stderr);
  246. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, to_syslog);
  247. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, syslog_facility);
  248. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  249. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*",
  250. (debug ? LOG_DEBUG : syslog_priority));
  251. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  252. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*",
  253. (debug ? LOG_DEBUG : logfile_priority));
  254. qb_log_format_set(QB_LOG_STDERR, log_format_stderr);
  255. qb_log_format_set(QB_LOG_SYSLOG, log_format_syslog);
  256. }
  257. void
  258. qdevice_log_init(struct qdevice_instance *instance)
  259. {
  260. qb_log_init(QDEVICE_PROGRAM_NAME, QDEVICE_LOG_DEFAULT_SYSLOG_FACILITY,
  261. QDEVICE_LOG_DEFAULT_SYSLOG_PRIORITY);
  262. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  263. qb_log_ctl(QB_LOG_STDOUT, QB_LOG_CONF_ENABLED, QB_FALSE);
  264. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
  265. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  266. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_INFO);
  267. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_INFO);
  268. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_PRIORITY_BUMP, LOG_INFO - LOG_DEBUG);
  269. qb_log_format_set(QB_LOG_STDERR, "%t %7p %b");
  270. qdevice_log_configure(instance);
  271. }
  272. void
  273. qdevice_log_close(struct qdevice_instance *instance)
  274. {
  275. qb_log_fini();
  276. }