qdevice-log.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. static int qdevice_log_global_force_debug;
  38. struct qdevice_log_syslog_names {
  39. const char *prio_name;
  40. int priority;
  41. };
  42. static struct qdevice_log_syslog_names qdevice_log_priority_names[] = {
  43. { "alert", LOG_ALERT },
  44. { "crit", LOG_CRIT },
  45. { "debug", LOG_DEBUG },
  46. { "emerg", LOG_EMERG },
  47. { "err", LOG_ERR },
  48. { "error", LOG_ERR },
  49. { "info", LOG_INFO },
  50. { "notice", LOG_NOTICE },
  51. { "warning", LOG_WARNING },
  52. { NULL, -1 }};
  53. static int
  54. qdevice_log_priority_str_to_int(const char *priority_str)
  55. {
  56. unsigned int i;
  57. for (i = 0; qdevice_log_priority_names[i].prio_name != NULL; i++) {
  58. if (strcasecmp(priority_str, qdevice_log_priority_names[i].prio_name) == 0) {
  59. return (qdevice_log_priority_names[i].priority);
  60. }
  61. }
  62. return (-1);
  63. }
  64. void
  65. qdevice_log_configure(struct qdevice_instance *instance)
  66. {
  67. int to_stderr;
  68. int to_syslog;
  69. int syslog_facility;
  70. int syslog_priority;
  71. int logfile_priority;
  72. int debug;
  73. char *str;
  74. int i;
  75. int fileline;
  76. int timestamp;
  77. int function_name;
  78. char log_format_syslog[64];
  79. char log_format_stderr[64];
  80. to_stderr = QDEVICE_LOG_DEFAULT_TO_STDERR;
  81. if (cmap_get_string(instance->cmap_handle, "logging.to_stderr", &str) == CS_OK) {
  82. if ((i = utils_parse_bool_str(str)) == -1) {
  83. qdevice_log(LOG_WARNING, "logging.to_stderr value is not valid");
  84. } else {
  85. to_stderr = i;
  86. }
  87. free(str);
  88. }
  89. if (cmap_get_string(instance->cmap_handle,
  90. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_stderr", &str) == CS_OK) {
  91. if ((i = utils_parse_bool_str(str)) == -1) {
  92. qdevice_log(LOG_WARNING,
  93. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_stderr value is not valid.");
  94. } else {
  95. to_stderr = i;
  96. }
  97. free(str);
  98. }
  99. to_syslog = QDEVICE_LOG_DEFAULT_TO_SYSLOG;
  100. if (cmap_get_string(instance->cmap_handle, "logging.to_syslog", &str) == CS_OK) {
  101. if ((i = utils_parse_bool_str(str)) == -1) {
  102. qdevice_log(LOG_WARNING, "logging.to_syslog value is not valid");
  103. } else {
  104. to_syslog = i;
  105. }
  106. free(str);
  107. }
  108. if (cmap_get_string(instance->cmap_handle,
  109. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_syslog", &str) == CS_OK) {
  110. if ((i = utils_parse_bool_str(str)) == -1) {
  111. qdevice_log(LOG_WARNING,
  112. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".to_syslog value is not valid.");
  113. } else {
  114. to_syslog = i;
  115. }
  116. free(str);
  117. }
  118. syslog_facility = QDEVICE_LOG_DEFAULT_SYSLOG_FACILITY;
  119. if (cmap_get_string(instance->cmap_handle, "logging.syslog_facility", &str) == CS_OK) {
  120. if ((i = qb_log_facility2int(str)) < 0) {
  121. qdevice_log(LOG_WARNING, "logging.syslog_facility value is not valid");
  122. } else {
  123. syslog_facility = i;
  124. }
  125. free(str);
  126. }
  127. if (cmap_get_string(instance->cmap_handle,
  128. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_facility", &str) == CS_OK) {
  129. if ((i = qb_log_facility2int(str)) < 0) {
  130. qdevice_log(LOG_WARNING,
  131. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_facility value is not valid.");
  132. } else {
  133. syslog_facility = i;
  134. }
  135. free(str);
  136. }
  137. syslog_priority = QDEVICE_LOG_DEFAULT_SYSLOG_PRIORITY;
  138. if (cmap_get_string(instance->cmap_handle, "logging.syslog_priority", &str) == CS_OK) {
  139. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  140. qdevice_log(LOG_WARNING, "logging.syslog_priority value is not valid");
  141. } else {
  142. syslog_priority = i;
  143. }
  144. free(str);
  145. }
  146. if (cmap_get_string(instance->cmap_handle,
  147. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_priority", &str) == CS_OK) {
  148. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  149. qdevice_log(LOG_WARNING,
  150. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".syslog_priority value is not valid.");
  151. } else {
  152. syslog_priority = i;
  153. }
  154. free(str);
  155. }
  156. logfile_priority = QDEVICE_LOG_DEFAULT_SYSLOG_PRIORITY;
  157. if (cmap_get_string(instance->cmap_handle, "logging.logfile_priority", &str) == CS_OK) {
  158. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  159. qdevice_log(LOG_WARNING, "logging.logfile_priority value is not valid");
  160. } else {
  161. logfile_priority = i;
  162. }
  163. free(str);
  164. }
  165. if (cmap_get_string(instance->cmap_handle,
  166. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".logfile_priority", &str) == CS_OK) {
  167. if ((i = qdevice_log_priority_str_to_int(str)) < 0) {
  168. qdevice_log(LOG_WARNING,
  169. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".logfile_priority value is not valid.");
  170. } else {
  171. logfile_priority = i;
  172. }
  173. free(str);
  174. }
  175. debug = QDEVICE_LOG_DEFAULT_DEBUG;
  176. if (cmap_get_string(instance->cmap_handle, "logging.debug", &str) == CS_OK) {
  177. if ((i = utils_parse_bool_str(str)) == -1) {
  178. if (strcasecmp(str, "trace") == 0) {
  179. debug = 1;
  180. } else {
  181. qdevice_log(LOG_WARNING, "logging.debug value is not valid");
  182. }
  183. } else {
  184. debug = i;
  185. }
  186. free(str);
  187. }
  188. if (cmap_get_string(instance->cmap_handle,
  189. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".debug", &str) == CS_OK) {
  190. if ((i = utils_parse_bool_str(str)) == -1) {
  191. if (strcasecmp(str, "trace") == 0) {
  192. debug = 1;
  193. } else {
  194. qdevice_log(LOG_WARNING,
  195. "logging.logger_subsys." QDEVICE_LOG_SUBSYS ".debug value is not valid.");
  196. }
  197. } else {
  198. debug = i;
  199. }
  200. free(str);
  201. }
  202. fileline = QDEVICE_LOG_DEFAULT_FILELINE;
  203. if (cmap_get_string(instance->cmap_handle, "logging.fileline", &str) == CS_OK) {
  204. if ((i = utils_parse_bool_str(str)) == -1) {
  205. qdevice_log(LOG_WARNING, "logging.fileline value is not valid");
  206. } else {
  207. fileline = i;
  208. }
  209. free(str);
  210. }
  211. timestamp = QDEVICE_LOG_DEFAULT_TIMESTAMP;
  212. if (cmap_get_string(instance->cmap_handle, "logging.timestamp", &str) == CS_OK) {
  213. if ((i = utils_parse_bool_str(str)) == -1) {
  214. qdevice_log(LOG_WARNING, "logging.timestamp value is not valid");
  215. } else {
  216. timestamp = i;
  217. }
  218. free(str);
  219. }
  220. function_name = QDEVICE_LOG_DEFAULT_FUNCTION_NAME;
  221. if (cmap_get_string(instance->cmap_handle, "logging.function_name", &str) == CS_OK) {
  222. if ((i = utils_parse_bool_str(str)) == -1) {
  223. qdevice_log(LOG_WARNING, "logging.function_name value is not valid");
  224. } else {
  225. function_name = i;
  226. }
  227. free(str);
  228. }
  229. strcpy(log_format_syslog, "");
  230. if (fileline) {
  231. strcat(log_format_syslog, "%f:");
  232. if (function_name) {
  233. strcat(log_format_syslog, "%n:");
  234. }
  235. strcat(log_format_syslog, "%l ");
  236. }
  237. strcat(log_format_syslog, "%b");
  238. strcpy(log_format_stderr, "");
  239. if (timestamp) {
  240. strcpy(log_format_stderr, "%t %7p ");
  241. }
  242. strcat(log_format_stderr, log_format_syslog);
  243. if (qdevice_log_global_force_debug) {
  244. debug = 1;
  245. }
  246. /*
  247. * Finally reconfigure log system
  248. */
  249. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, to_stderr);
  250. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, to_syslog);
  251. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, syslog_facility);
  252. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  253. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*",
  254. (debug ? LOG_DEBUG : syslog_priority));
  255. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  256. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*",
  257. (debug ? LOG_DEBUG : logfile_priority));
  258. qb_log_format_set(QB_LOG_STDERR, log_format_stderr);
  259. qb_log_format_set(QB_LOG_SYSLOG, log_format_syslog);
  260. }
  261. void
  262. qdevice_log_init(struct qdevice_instance *instance, int force_debug)
  263. {
  264. qdevice_log_global_force_debug = force_debug;
  265. qb_log_init(QDEVICE_PROGRAM_NAME, QDEVICE_LOG_DEFAULT_SYSLOG_FACILITY,
  266. QDEVICE_LOG_DEFAULT_SYSLOG_PRIORITY);
  267. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  268. qb_log_ctl(QB_LOG_STDOUT, QB_LOG_CONF_ENABLED, QB_FALSE);
  269. qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
  270. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  271. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_INFO);
  272. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_INFO);
  273. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_PRIORITY_BUMP, LOG_INFO - LOG_DEBUG);
  274. qb_log_format_set(QB_LOG_STDERR, "%t %7p %b");
  275. qdevice_log_configure(instance);
  276. }
  277. void
  278. qdevice_log_close(struct qdevice_instance *instance)
  279. {
  280. qb_log_fini();
  281. }