log.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2015-2019 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 <syslog.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <time.h>
  38. #include "qnet-config.h"
  39. #include "log.h"
  40. static int log_config_target = 0;
  41. static int log_config_debug = 0;
  42. static int log_config_priority_bump = 0;
  43. static int log_config_syslog_facility = 0;
  44. static char *log_config_ident = NULL;
  45. static const char log_month_str[][4] = {
  46. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  47. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  48. };
  49. struct log_syslog_prio_to_str_item {
  50. int priority;
  51. const char *priority_str;
  52. };
  53. static struct log_syslog_prio_to_str_item syslog_prio_to_str_array[] = {
  54. {LOG_EMERG, "emerg"},
  55. {LOG_ALERT, "alert"},
  56. {LOG_CRIT, "crit"},
  57. {LOG_ERR, "error"},
  58. {LOG_WARNING, "warning"},
  59. {LOG_NOTICE, "notice"},
  60. {LOG_INFO, "info"},
  61. {LOG_DEBUG, "debug"},
  62. {-1, NULL}};
  63. int
  64. log_init(const char *ident, int target, int syslog_facility)
  65. {
  66. log_config_ident = strdup(ident);
  67. if (log_config_ident == NULL) {
  68. return (-1);
  69. }
  70. log_set_target(target, syslog_facility);
  71. return (0);
  72. }
  73. static const char *
  74. log_syslog_prio_to_str(int priority)
  75. {
  76. if (priority >= LOG_EMERG && priority <= LOG_DEBUG) {
  77. return (syslog_prio_to_str_array[priority].priority_str);
  78. } else {
  79. return ("none");
  80. }
  81. }
  82. void
  83. log_vprintf(int priority, const char *format, va_list ap)
  84. {
  85. time_t current_time;
  86. struct tm tm_res;
  87. int final_priority;
  88. va_list ap_copy;
  89. if (priority != LOG_DEBUG || (log_config_debug)) {
  90. if (log_config_target & LOG_TARGET_STDERR) {
  91. current_time = time(NULL);
  92. localtime_r(&current_time, &tm_res);
  93. fprintf(stderr, "%s %02d %02d:%02d:%02d ",
  94. log_month_str[tm_res.tm_mon], tm_res.tm_mday, tm_res.tm_hour,
  95. tm_res.tm_min, tm_res.tm_sec);
  96. fprintf(stderr, "%-7s ", log_syslog_prio_to_str(priority));
  97. va_copy(ap_copy, ap);
  98. vfprintf(stderr, format, ap_copy);
  99. va_end(ap_copy);
  100. fprintf(stderr, "\n");
  101. }
  102. if (log_config_target & LOG_TARGET_SYSLOG) {
  103. final_priority = priority;
  104. if (log_config_priority_bump && priority > LOG_INFO) {
  105. final_priority = LOG_INFO;
  106. }
  107. va_copy(ap_copy, ap);
  108. vsyslog(final_priority, format, ap);
  109. va_end(ap_copy);
  110. }
  111. }
  112. }
  113. void
  114. log_printf(int priority, const char *format, ...)
  115. {
  116. va_list ap;
  117. va_start(ap, format);
  118. log_vprintf(priority, format, ap);
  119. va_end(ap);
  120. }
  121. void
  122. log_close(void)
  123. {
  124. if (log_config_target & LOG_TARGET_SYSLOG) {
  125. closelog();
  126. }
  127. }
  128. void
  129. log_set_debug(int enabled)
  130. {
  131. log_config_debug = enabled;
  132. }
  133. void
  134. log_set_priority_bump(int enabled)
  135. {
  136. log_config_priority_bump = enabled;
  137. }
  138. void
  139. log_set_target(int target, int syslog_facility)
  140. {
  141. log_close();
  142. log_config_target = target;
  143. log_config_syslog_facility = syslog_facility;
  144. if (log_config_target & LOG_TARGET_SYSLOG) {
  145. openlog(log_config_ident, LOG_PID, log_config_syslog_facility);
  146. }
  147. }
  148. void
  149. log_msg_decode_error(int ret)
  150. {
  151. switch (ret) {
  152. case -1:
  153. log(LOG_WARNING, "Received message with option with invalid length");
  154. break;
  155. case -2:
  156. log(LOG_CRIT, "Can't allocate memory");
  157. break;
  158. case -3:
  159. log(LOG_WARNING, "Received inconsistent msg (tlv len > msg size)");
  160. break;
  161. case -4:
  162. log(LOG_WARNING, "Received message with option with invalid value");
  163. break;
  164. default:
  165. log(LOG_ERR, "Unknown error occurred when decoding message");
  166. break;
  167. }
  168. }