test-log.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 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 <stdio.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include "log.h"
  39. #define MAX_LINE_LEN 512
  40. static int openlog_called;
  41. static int openlog_facility;
  42. static char vsyslog_buf[MAX_LINE_LEN];
  43. static int vsyslog_priority;
  44. static int vsyslog_called;
  45. static int closelog_called;
  46. void
  47. openlog(const char *ident, int option, int facility)
  48. {
  49. openlog_called = 1;
  50. openlog_facility = facility;
  51. }
  52. void
  53. vsyslog(int priority, const char *format, va_list ap)
  54. {
  55. va_list ap_copy;
  56. int res;
  57. vsyslog_called = 1;
  58. vsyslog_priority = priority;
  59. va_copy(ap_copy, ap);
  60. res = vsnprintf(vsyslog_buf, MAX_LINE_LEN, format, ap_copy);
  61. assert(res < MAX_LINE_LEN && res != -1);
  62. va_end(ap_copy);
  63. }
  64. void
  65. __vsyslog_chk(int priority, int flag, const char *format, va_list ap)
  66. {
  67. vsyslog(priority, format, ap);
  68. }
  69. void
  70. closelog(void)
  71. {
  72. closelog_called = 1;
  73. }
  74. static void
  75. log_check(int priority, const char *msg, int should_be_called, int expected_priority)
  76. {
  77. vsyslog_called = 0;
  78. vsyslog_priority = -1;
  79. vsyslog_buf[0] = '\0';
  80. log(priority, "%s", msg);
  81. if (should_be_called) {
  82. assert(vsyslog_called);
  83. assert(vsyslog_priority == expected_priority);
  84. assert(strcmp(vsyslog_buf, msg) == 0);
  85. } else {
  86. assert(!vsyslog_called);
  87. }
  88. }
  89. int
  90. main(void)
  91. {
  92. openlog_called = 0;
  93. assert(log_init("test", LOG_TARGET_SYSLOG, LOG_DAEMON) == 0);
  94. assert(openlog_called);
  95. assert(openlog_facility == LOG_DAEMON);
  96. log_check(LOG_INFO, "test log info", 1, LOG_INFO);
  97. log_check(LOG_ERR, "test log err", 1, LOG_ERR);
  98. log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG);
  99. log_set_debug(1);
  100. log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG);
  101. log_set_debug(0);
  102. log_check(LOG_DEBUG, "test log debug", 0, LOG_DEBUG);
  103. log_set_debug(1);
  104. log_set_priority_bump(1);
  105. log_check(LOG_ERR, "test log err", 1, LOG_ERR);
  106. log_check(LOG_DEBUG, "test log debug", 1, LOG_INFO);
  107. log_set_priority_bump(0);
  108. log_check(LOG_ERR, "test log err", 1, LOG_ERR);
  109. log_check(LOG_DEBUG, "test log debug", 1, LOG_DEBUG);
  110. closelog_called = 0;
  111. openlog_called = 0;
  112. log_set_target(LOG_TARGET_SYSLOG, LOG_USER);
  113. assert(openlog_called);
  114. assert(closelog_called);
  115. assert(openlog_facility == LOG_USER);
  116. closelog_called = 0;
  117. log_close();
  118. assert(closelog_called);
  119. return (0);
  120. }