| 1 | |
| 2 | #ifndef AWS_COMMON_LOG_FORMATTER_H |
| 3 | #define AWS_COMMON_LOG_FORMATTER_H |
| 4 | |
| 5 | /* |
| 6 | * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * A copy of the License is located at |
| 11 | * |
| 12 | * http://aws.amazon.com/apache2.0 |
| 13 | * |
| 14 | * or in the "license" file accompanying this file. This file is distributed |
| 15 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 16 | * express or implied. See the License for the specific language governing |
| 17 | * permissions and limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #include <aws/common/common.h> |
| 21 | |
| 22 | #include <aws/common/date_time.h> |
| 23 | #include <aws/common/logging.h> |
| 24 | |
| 25 | #include <stdarg.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | struct aws_allocator; |
| 29 | struct aws_string; |
| 30 | |
| 31 | /* |
| 32 | * Log formatter interface and default implementation |
| 33 | * |
| 34 | * Log formatters are invoked by the LOGF_* macros to transform a set of arguments into |
| 35 | * one or more lines of text to be output to a logging sink (writer). |
| 36 | */ |
| 37 | struct aws_log_formatter; |
| 38 | |
| 39 | typedef int(aws_log_formatter_format_fn)( |
| 40 | struct aws_log_formatter *formatter, |
| 41 | struct aws_string **formatted_output, |
| 42 | enum aws_log_level level, |
| 43 | aws_log_subject_t subject, |
| 44 | const char *format, |
| 45 | va_list args); |
| 46 | |
| 47 | typedef void(aws_log_formatter_clean_up_fn)(struct aws_log_formatter *logger); |
| 48 | |
| 49 | struct aws_log_formatter_vtable { |
| 50 | aws_log_formatter_format_fn *format; |
| 51 | aws_log_formatter_clean_up_fn *clean_up; |
| 52 | }; |
| 53 | |
| 54 | struct aws_log_formatter { |
| 55 | struct aws_log_formatter_vtable *vtable; |
| 56 | struct aws_allocator *allocator; |
| 57 | void *impl; |
| 58 | }; |
| 59 | |
| 60 | struct aws_log_formatter_standard_options { |
| 61 | enum aws_date_format date_format; |
| 62 | }; |
| 63 | |
| 64 | struct aws_logging_standard_formatting_data { |
| 65 | char *log_line_buffer; |
| 66 | size_t total_length; |
| 67 | enum aws_log_level level; |
| 68 | const char *subject_name; |
| 69 | const char *format; |
| 70 | enum aws_date_format date_format; |
| 71 | struct aws_allocator *allocator; /* not used, just there to make byte_bufs valid */ |
| 72 | |
| 73 | size_t amount_written; |
| 74 | }; |
| 75 | |
| 76 | AWS_EXTERN_C_BEGIN |
| 77 | |
| 78 | /* |
| 79 | * Initializes the default log formatter which outputs lines in the format: |
| 80 | * |
| 81 | * [<LogLevel>] [<Timestamp>] [<ThreadId>] - <User content>\n |
| 82 | */ |
| 83 | AWS_COMMON_API |
| 84 | int aws_log_formatter_init_default( |
| 85 | struct aws_log_formatter *formatter, |
| 86 | struct aws_allocator *allocator, |
| 87 | struct aws_log_formatter_standard_options *options); |
| 88 | |
| 89 | /* |
| 90 | * Cleans up a log formatter (minus the base structure memory) by calling the formatter's clean_up function |
| 91 | * via the vtable. |
| 92 | */ |
| 93 | AWS_COMMON_API |
| 94 | void aws_log_formatter_clean_up(struct aws_log_formatter *formatter); |
| 95 | |
| 96 | /* |
| 97 | * Formats a single log line based on the input + the var args list. Output is written to a fixed-size |
| 98 | * buffer supplied in the data struct. |
| 99 | */ |
| 100 | AWS_COMMON_API |
| 101 | int aws_format_standard_log_line(struct aws_logging_standard_formatting_data *formatting_data, va_list args); |
| 102 | |
| 103 | AWS_EXTERN_C_END |
| 104 | |
| 105 | #endif /* AWS_COMMON_LOG_FORMATTER_H */ |
| 106 | |