| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "logging_test_utilities.h" |
| 7 |
|
| 8 |
#include <aws/common/file.h> |
| 9 |
#include <aws/common/logging.h> |
| 10 |
#include <aws/common/string.h> |
| 11 |
|
| 12 |
#include <aws/testing/aws_test_harness.h> |
| 13 |
|
| 14 |
#include <errno.h> |
| 15 |
#include <stdio.h> |
| 16 |
|
| 17 |
#define TEST_PIPELINE_MAX_BUFFER_SIZE 4096 |
| 18 |
|
| 19 |
typedef void(log_test_fn)(void); |
| 20 |
|
| 21 |
int do_pipeline_logger_test( |
| 22 |
struct aws_allocator *allocator, |
| 23 |
log_test_fn *log_fn, |
| 24 |
const char **expected_user_content, |
| 25 |
size_t user_content_count) { |
| 26 |
|
| 27 |
struct aws_string *test_file_str = aws_string_new_log_writer_test_filename(allocator); |
| 28 |
const char *test_file_cstr = aws_string_c_str(test_file_str); |
| 29 |
|
| 30 |
remove(test_file_cstr); |
| 31 |
|
| 32 |
struct aws_logger_standard_options options = {.level = AWS_LL_TRACE, .filename = test_file_cstr}; |
| 33 |
|
| 34 |
struct aws_logger logger; |
| 35 |
if (aws_logger_init_standard(&logger, allocator, &options)) { |
| 36 |
return AWS_OP_ERR; |
| 37 |
} |
| 38 |
|
| 39 |
aws_logger_set(&logger); |
| 40 |
|
| 41 |
(*log_fn)(); |
| 42 |
|
| 43 |
aws_logger_set(NULL); |
| 44 |
|
| 45 |
aws_logger_clean_up(&logger); |
| 46 |
|
| 47 |
char buffer[TEST_PIPELINE_MAX_BUFFER_SIZE]; |
| 48 |
FILE *file = aws_fopen(test_file_cstr, "r"); |
| 49 |
int open_error = errno; |
| 50 |
size_t bytes_read = 0; |
| 51 |
|
| 52 |
if (file != NULL) { |
| 53 |
bytes_read = fread(buffer, 1, TEST_PIPELINE_MAX_BUFFER_SIZE - 1, file); |
| 54 |
fclose(file); |
| 55 |
} |
| 56 |
|
| 57 |
remove(test_file_cstr); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
ASSERT_TRUE( |
| 63 |
file != NULL, "Unable to open log file \"%s\" to verify contents. Error: %d", test_file_str, open_error); |
| 64 |
ASSERT_TRUE(bytes_read >= 0, "Failed to read log file \"%s\"", test_file_str); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
buffer[bytes_read] = 0; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
const char *buffer_ptr = buffer; |
| 78 |
for (size_t i = 0; i < user_content_count; ++i) { |
| 79 |
buffer_ptr = strstr(buffer_ptr, expected_user_content[i]); |
| 80 |
ASSERT_TRUE( |
| 81 |
buffer_ptr != NULL, |
| 82 |
"Expected to find \"%s\" in log file but could not. Content is either missing or out-of-order.", |
| 83 |
expected_user_content[i]); |
| 84 |
} |
| 85 |
|
| 86 |
aws_string_destroy(test_file_str); |
| 87 |
|
| 88 |
return AWS_OP_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
static void s_unformatted_pipeline_logger_test_callback(void) { |
| 92 |
AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "trace log call"); |
| 93 |
AWS_LOGF_DEBUG(AWS_LS_COMMON_GENERAL, "debug log call"); |
| 94 |
AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "info log call"); |
| 95 |
AWS_LOGF_WARN(AWS_LS_COMMON_GENERAL, "warn log call"); |
| 96 |
AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "error log call"); |
| 97 |
AWS_LOGF_FATAL(AWS_LS_COMMON_GENERAL, "fatal log call"); |
| 98 |
} |
| 99 |
|
| 100 |
static void s_formatted_pipeline_logger_test_callback(void) { |
| 101 |
AWS_LOGF_TRACE(AWS_LS_COMMON_GENERAL, "%s log call", "trace"); |
| 102 |
AWS_LOGF_DEBUG(AWS_LS_COMMON_GENERAL, "%s log call", "debug"); |
| 103 |
AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "%s log call", "info"); |
| 104 |
AWS_LOGF_WARN(AWS_LS_COMMON_GENERAL, "%s log call", "warn"); |
| 105 |
AWS_LOGF_ERROR(AWS_LS_COMMON_GENERAL, "%s log call", "error"); |
| 106 |
AWS_LOGF_FATAL(AWS_LS_COMMON_GENERAL, "%s log call", "fatal"); |
| 107 |
} |
| 108 |
|
| 109 |
static const char *expected_test_user_content[] = |
| 110 |
{"trace log call", "debug log call", "info log call", "warn log call", "error log call", "fatal log call"}; |
| 111 |
|
| 112 |
#define DEFINE_PIPELINE_LOGGER_TEST(test_name, callback_function) \ |
| 113 |
static int s_pipeline_logger_##test_name(struct aws_allocator *allocator, void *ctx) { \ |
| 114 |
(void)ctx; \ |
| 115 |
return do_pipeline_logger_test( \ |
| 116 |
allocator, \ |
| 117 |
callback_function, \ |
| 118 |
expected_test_user_content, \ |
| 119 |
sizeof(expected_test_user_content) / sizeof(expected_test_user_content[0])); \ |
| 120 |
} \ |
| 121 |
AWS_TEST_CASE(test_pipeline_logger_##test_name, s_pipeline_logger_##test_name); |
| 122 |
|
| 123 |
DEFINE_PIPELINE_LOGGER_TEST(unformatted_test, s_unformatted_pipeline_logger_test_callback) |
| 124 |
DEFINE_PIPELINE_LOGGER_TEST(formatted_test, s_formatted_pipeline_logger_test_callback) |