qnetd-log.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <syslog.h>
  35. #include <stdio.h>
  36. #include <time.h>
  37. #include "qnet-config.h"
  38. #include "qnetd-log.h"
  39. static int qnetd_log_config_target = 0;
  40. static int qnetd_log_config_debug = 0;
  41. static int qnetd_log_config_priority_bump = 0;
  42. static const char qnetd_log_month_str[][4] = {
  43. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  44. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  45. };
  46. struct qnetd_log_syslog_prio_to_str_item {
  47. int priority;
  48. const char *priority_str;
  49. };
  50. static struct qnetd_log_syslog_prio_to_str_item qnetd_syslog_prio_to_str_array[] = {
  51. {LOG_EMERG, "emerg"},
  52. {LOG_ALERT, "alert"},
  53. {LOG_CRIT, "crit"},
  54. {LOG_ERR, "error"},
  55. {LOG_WARNING, "warning"},
  56. {LOG_NOTICE, "notice"},
  57. {LOG_INFO, "info"},
  58. {LOG_DEBUG, "debug"},
  59. {-1, NULL}};
  60. void
  61. qnetd_log_init(int target)
  62. {
  63. qnetd_log_config_target = target;
  64. if (qnetd_log_config_target & QNETD_LOG_TARGET_SYSLOG) {
  65. openlog(QNETD_PROGRAM_NAME, LOG_PID, LOG_DAEMON);
  66. }
  67. }
  68. static const char *
  69. qnetd_log_syslog_prio_to_str(int priority)
  70. {
  71. if (priority >= LOG_EMERG && priority <= LOG_DEBUG) {
  72. return (qnetd_syslog_prio_to_str_array[priority].priority_str);
  73. } else {
  74. return ("none");
  75. }
  76. }
  77. void
  78. qnetd_log_vprintf(int priority, const char *format, va_list ap)
  79. {
  80. time_t current_time;
  81. struct tm tm_res;
  82. int final_priority;
  83. va_list ap_copy;
  84. if (priority != LOG_DEBUG || (qnetd_log_config_debug)) {
  85. if (qnetd_log_config_target & QNETD_LOG_TARGET_STDERR) {
  86. current_time = time(NULL);
  87. localtime_r(&current_time, &tm_res);
  88. fprintf(stderr, "%s %02d %02d:%02d:%02d ",
  89. qnetd_log_month_str[tm_res.tm_mon], tm_res.tm_mday, tm_res.tm_hour,
  90. tm_res.tm_min, tm_res.tm_sec);
  91. fprintf(stderr, "%-7s ", qnetd_log_syslog_prio_to_str(priority));
  92. va_copy(ap_copy, ap);
  93. vfprintf(stderr, format, ap_copy);
  94. va_end(ap_copy);
  95. fprintf(stderr, "\n");
  96. }
  97. if (qnetd_log_config_target & QNETD_LOG_TARGET_SYSLOG) {
  98. final_priority = priority;
  99. if (qnetd_log_config_priority_bump && priority > LOG_INFO) {
  100. final_priority = LOG_INFO;
  101. }
  102. va_copy(ap_copy, ap);
  103. vsyslog(final_priority, format, ap);
  104. va_end(ap_copy);
  105. }
  106. }
  107. }
  108. void
  109. qnetd_log_printf(int priority, const char *format, ...)
  110. {
  111. va_list ap;
  112. va_start(ap, format);
  113. qnetd_log_vprintf(priority, format, ap);
  114. va_end(ap);
  115. }
  116. void
  117. qnetd_log_close(void)
  118. {
  119. if (qnetd_log_config_target & QNETD_LOG_TARGET_SYSLOG) {
  120. closelog();
  121. }
  122. }
  123. void
  124. qnetd_log_set_debug(int enabled)
  125. {
  126. qnetd_log_config_debug = enabled;
  127. }
  128. void
  129. qnetd_log_set_priority_bump(int enabled)
  130. {
  131. qnetd_log_config_priority_bump = enabled;
  132. }
  133. void
  134. qnetd_log_msg_decode_error(int ret)
  135. {
  136. switch (ret) {
  137. case -1:
  138. qnetd_log(LOG_WARNING, "Received message with option with invalid length");
  139. break;
  140. case -2:
  141. qnetd_log(LOG_CRIT, "Can't allocate memory");
  142. break;
  143. case -3:
  144. qnetd_log(LOG_WARNING, "Received inconsistent msg (tlv len > msg size)");
  145. break;
  146. case -4:
  147. qnetd_log(LOG_WARNING, "Received message with option with invalid value");
  148. break;
  149. default:
  150. qnetd_log(LOG_ERR, "Unknown error occurred when decoding message");
  151. break;
  152. }
  153. }