Debian Codegraph @

crt/aws-c-common/tests/logging/logging_general_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include "logging_test_utilities.h"
7 #include "test_logger.h"
8
9 DECLARE_LOGF_ALL_LEVELS_FUNCTION(s_logf_all_levels)
10
11 /**
12 * These tests check the dynamic (run-time) filtering capabilities of the logging
13 * system.
14 *
15 * In each case, we use the test logger and invoke a log operation at each supported level.
16 * Using the level (integer value) itself as the log text, we can easily check to see
17 * what got filtered and what got through.
18 *
19 * For example, when the log level is AWS_LL_INFO, we expect
20 * {AWS_LL_FATAL, AWS_LL_ERROR, AWS_LL_WARN, AWS_LL_INFO} calls to all go through ("1234")
21 * but
22 * {AWS_LL_DEBUG, AWS_LL_TRACE} to be filtered out ("56")
23 */
24 TEST_LEVEL_FILTER(AWS_LL_TRACE, "123456", s_logf_all_levels)
25 TEST_LEVEL_FILTER(AWS_LL_DEBUG, "12345", s_logf_all_levels)
26 TEST_LEVEL_FILTER(AWS_LL_INFO, "1234", s_logf_all_levels)
27 TEST_LEVEL_FILTER(AWS_LL_WARN, "123", s_logf_all_levels)
28 TEST_LEVEL_FILTER(AWS_LL_ERROR, "12", s_logf_all_levels)
29 TEST_LEVEL_FILTER(AWS_LL_FATAL, "1", s_logf_all_levels)
30 TEST_LEVEL_FILTER(AWS_LL_NONE, "", s_logf_all_levels)
31
32 /*
33 * Dynamic level change testing
34 */
35
36 #define TEST_LOGGER_MAX_BUFFER_SIZE 4096
37
38 static int s_dynamic_log_level_change_test(struct aws_allocator *allocator, void *ctx) {
39 (void)ctx;
40 (void)allocator;
41
42 /* Create and attach a logger for testing*/
43 struct aws_logger test_logger;
44 test_logger_init(&test_logger, allocator, AWS_LOG_LEVEL_ERROR, TEST_LOGGER_MAX_BUFFER_SIZE);
45 aws_logger_set(&test_logger);
46
47 /* Perform logging operations */
48 s_logf_all_levels(AWS_LOG_LEVEL_DEBUG);
49
50 aws_logger_set_log_level(&test_logger, AWS_LOG_LEVEL_DEBUG);
51
52 s_logf_all_levels(AWS_LOG_LEVEL_DEBUG);
53
54 aws_logger_set_log_level(&test_logger, AWS_LOG_LEVEL_WARN);
55
56 s_logf_all_levels(AWS_LOG_LEVEL_DEBUG);
57
58 /* Pull out what was logged before clean up */
59 char buffer[TEST_LOGGER_MAX_BUFFER_SIZE];
60 test_logger_get_contents(&test_logger, buffer, TEST_LOGGER_MAX_BUFFER_SIZE);
61
62 /* clean up */
63 aws_logger_set(NULL);
64 aws_logger_clean_up(&test_logger);
65
66 /* Check the test results last */
67 static const char *expected_result = "1212345123";
68 ASSERT_SUCCESS(strcmp(buffer, expected_result), "Expected \"%s\" but received \"%s\"", expected_result, buffer);
69
70 return 0;
71 }
72 AWS_TEST_CASE(dynamic_log_level_change_test, s_dynamic_log_level_change_test)