| 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 |
|
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include <grpc/support/log.h> |
| 24 |
|
| 25 |
#include "test/core/util/test_config.h" |
| 26 |
|
| 27 |
static bool log_func_reached = false; |
| 28 |
|
| 29 |
static void test_callback(gpr_log_func_args* args) { |
| 30 |
GPR_ASSERT(0 == strcmp(__FILE__, args->file)); |
| 31 |
GPR_ASSERT(args->severity == GPR_LOG_SEVERITY_INFO); |
| 32 |
GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3")); |
| 33 |
} |
| 34 |
|
| 35 |
static void test_should_log(gpr_log_func_args* /*args*/) { |
| 36 |
log_func_reached = true; |
| 37 |
} |
| 38 |
|
| 39 |
static void test_should_not_log(gpr_log_func_args* /*args*/) { |
| 40 |
GPR_ASSERT(false); |
| 41 |
} |
| 42 |
|
| 43 |
#define test_log_function_reached(SEVERITY) \ |
| 44 |
gpr_set_log_function(test_should_log); \ |
| 45 |
log_func_reached = false; \ |
| 46 |
gpr_log_message(SEVERITY, "hello 1 2 3"); \ |
| 47 |
GPR_ASSERT(log_func_reached); \ |
| 48 |
log_func_reached = false; \ |
| 49 |
gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \ |
| 50 |
GPR_ASSERT(log_func_reached); \ |
| 51 |
gpr_set_log_function(nullptr); |
| 52 |
|
| 53 |
#define test_log_function_unreached(SEVERITY) \ |
| 54 |
gpr_set_log_function(test_should_not_log); \ |
| 55 |
gpr_log_message(SEVERITY, "hello 1 2 3"); \ |
| 56 |
gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \ |
| 57 |
gpr_set_log_function(nullptr); |
| 58 |
|
| 59 |
TEST(LogTest, Basic) { |
| 60 |
/* test logging at various verbosity levels */ |
| 61 |
gpr_log(GPR_DEBUG, "%s", "hello world"); |
| 62 |
gpr_log(GPR_INFO, "%s", "hello world"); |
| 63 |
gpr_log(GPR_ERROR, "%s", "hello world"); |
| 64 |
/* should succeed */ |
| 65 |
GPR_ASSERT(1); |
| 66 |
gpr_set_log_function(test_callback); |
| 67 |
gpr_log_message(GPR_INFO, "hello 1 2 3"); |
| 68 |
gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3); |
| 69 |
gpr_set_log_function(nullptr); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(LogTest, LogVerbosity) { |
| 73 |
gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG); |
| 74 |
test_log_function_reached(GPR_ERROR); |
| 75 |
test_log_function_reached(GPR_INFO); |
| 76 |
test_log_function_reached(GPR_DEBUG); |
| 77 |
|
| 78 |
gpr_set_log_verbosity(GPR_LOG_SEVERITY_INFO); |
| 79 |
test_log_function_reached(GPR_ERROR); |
| 80 |
test_log_function_reached(GPR_INFO); |
| 81 |
test_log_function_unreached(GPR_DEBUG); |
| 82 |
|
| 83 |
gpr_set_log_verbosity(GPR_LOG_SEVERITY_ERROR); |
| 84 |
test_log_function_reached(GPR_ERROR); |
| 85 |
test_log_function_unreached(GPR_INFO); |
| 86 |
test_log_function_unreached(GPR_DEBUG); |
| 87 |
} |
| 88 |
|
| 89 |
int main(int argc, char** argv) { |
| 90 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 91 |
::testing::InitGoogleTest(&argc, argv); |
| 92 |
int ret = RUN_ALL_TESTS(); |
| 93 |
return ret; |
| 94 |
} |