test-log.c 4.0 KB

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