log_test.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <gtest/gtest.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/gprpp/global_config.h"
  23. #include "test/core/util/test_config.h"
  24. static bool log_func_reached = false;
  25. static void test_callback(gpr_log_func_args* args) {
  26. GPR_ASSERT(0 == strcmp(__FILE__, args->file));
  27. GPR_ASSERT(args->severity == GPR_LOG_SEVERITY_INFO);
  28. GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3"));
  29. }
  30. static void test_should_log(gpr_log_func_args* /*args*/) {
  31. log_func_reached = true;
  32. }
  33. static void test_should_not_log(gpr_log_func_args* /*args*/) {
  34. GPR_ASSERT(false);
  35. }
  36. #define test_log_function_reached(SEVERITY) \
  37. gpr_set_log_function(test_should_log); \
  38. log_func_reached = false; \
  39. gpr_log_message(SEVERITY, "hello 1 2 3"); \
  40. GPR_ASSERT(log_func_reached); \
  41. log_func_reached = false; \
  42. gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
  43. GPR_ASSERT(log_func_reached); \
  44. gpr_set_log_function(nullptr);
  45. #define test_log_function_unreached(SEVERITY) \
  46. gpr_set_log_function(test_should_not_log); \
  47. gpr_log_message(SEVERITY, "hello 1 2 3"); \
  48. gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
  49. gpr_set_log_function(nullptr);
  50. TEST(LogTest, Basic) {
  51. /* test logging at various verbosity levels */
  52. gpr_log(GPR_DEBUG, "%s", "hello world");
  53. gpr_log(GPR_INFO, "%s", "hello world");
  54. gpr_log(GPR_ERROR, "%s", "hello world");
  55. /* should succeed */
  56. GPR_ASSERT(1);
  57. gpr_set_log_function(test_callback);
  58. gpr_log_message(GPR_INFO, "hello 1 2 3");
  59. gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3);
  60. gpr_set_log_function(nullptr);
  61. }
  62. TEST(LogTest, LogVerbosity) {
  63. gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
  64. test_log_function_reached(GPR_ERROR);
  65. test_log_function_reached(GPR_INFO);
  66. test_log_function_reached(GPR_DEBUG);
  67. gpr_set_log_verbosity(GPR_LOG_SEVERITY_INFO);
  68. test_log_function_reached(GPR_ERROR);
  69. test_log_function_reached(GPR_INFO);
  70. test_log_function_unreached(GPR_DEBUG);
  71. gpr_set_log_verbosity(GPR_LOG_SEVERITY_ERROR);
  72. test_log_function_reached(GPR_ERROR);
  73. test_log_function_unreached(GPR_INFO);
  74. test_log_function_unreached(GPR_DEBUG);
  75. }
  76. int main(int argc, char** argv) {
  77. grpc::testing::TestEnvironment env(argc, argv);
  78. ::testing::InitGoogleTest(&argc, argv);
  79. int ret = RUN_ALL_TESTS();
  80. return ret;
  81. }