log_test.cc
Go to the documentation of this file.
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 <stdbool.h>
20 #include <string.h>
21 
22 #include <gtest/gtest.h>
23 
24 #include <grpc/support/log.h>
25 
28 
29 static bool log_func_reached = false;
30 
32  GPR_ASSERT(0 == strcmp(__FILE__, args->file));
34  GPR_ASSERT(0 == strcmp(args->message, "hello 1 2 3"));
35 }
36 
37 static void test_should_log(gpr_log_func_args* /*args*/) {
38  log_func_reached = true;
39 }
40 
41 static void test_should_not_log(gpr_log_func_args* /*args*/) {
42  GPR_ASSERT(false);
43 }
44 
45 #define test_log_function_reached(SEVERITY) \
46  gpr_set_log_function(test_should_log); \
47  log_func_reached = false; \
48  gpr_log_message(SEVERITY, "hello 1 2 3"); \
49  GPR_ASSERT(log_func_reached); \
50  log_func_reached = false; \
51  gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
52  GPR_ASSERT(log_func_reached); \
53  gpr_set_log_function(nullptr);
54 
55 #define test_log_function_unreached(SEVERITY) \
56  gpr_set_log_function(test_should_not_log); \
57  gpr_log_message(SEVERITY, "hello 1 2 3"); \
58  gpr_log(SEVERITY, "hello %d %d %d", 1, 2, 3); \
59  gpr_set_log_function(nullptr);
60 
61 TEST(LogTest, Basic) {
62  /* test logging at various verbosity levels */
63  gpr_log(GPR_DEBUG, "%s", "hello world");
64  gpr_log(GPR_INFO, "%s", "hello world");
65  gpr_log(GPR_ERROR, "%s", "hello world");
66  /* should succeed */
67  GPR_ASSERT(1);
69  gpr_log_message(GPR_INFO, "hello 1 2 3");
70  gpr_log(GPR_INFO, "hello %d %d %d", 1, 2, 3);
71  gpr_set_log_function(nullptr);
72 }
73 
74 TEST(LogTest, LogVerbosity) {
79 
84 
89 }
90 
91 int main(int argc, char** argv) {
93  ::testing::InitGoogleTest(&argc, argv);
94  int ret = RUN_ALL_TESTS();
95  return ret;
96 }
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
generate.env
env
Definition: generate.py:37
string.h
GPR_LOG_SEVERITY_DEBUG
@ GPR_LOG_SEVERITY_DEBUG
Definition: include/grpc/impl/codegen/log.h:46
gpr_log_func_args
Definition: include/grpc/impl/codegen/log.h:77
GPR_LOG_SEVERITY_INFO
@ GPR_LOG_SEVERITY_INFO
Definition: include/grpc/impl/codegen/log.h:47
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
main
int main(int argc, char **argv)
Definition: log_test.cc:91
GPR_LOG_SEVERITY_ERROR
@ GPR_LOG_SEVERITY_ERROR
Definition: include/grpc/impl/codegen/log.h:48
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
test_callback
static void test_callback(gpr_log_func_args *args)
Definition: log_test.cc:31
test_config.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
global_config.h
test_log_function_unreached
#define test_log_function_unreached(SEVERITY)
Definition: log_test.cc:55
test_log_function_reached
#define test_log_function_reached(SEVERITY)
Definition: log_test.cc:45
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
gpr_log_message
GPRAPI void gpr_log_message(const char *file, int line, gpr_log_severity severity, const char *message)
Definition: log.cc:81
test_should_log
static void test_should_log(gpr_log_func_args *)
Definition: log_test.cc:37
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
test_should_not_log
static void test_should_not_log(gpr_log_func_args *)
Definition: log_test.cc:41
gpr_set_log_function
GPRAPI void gpr_set_log_function(gpr_log_func func)
Definition: log.cc:143
log_func_reached
static bool log_func_reached
Definition: log_test.cc:29
TEST
TEST(LogTest, Basic)
Definition: log_test.cc:61
gpr_set_log_verbosity
GPRAPI void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print)
Definition: log.cc:96


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:29