Debian Codegraph @

crt/aws-c-common/source/log_formatter.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 <aws/common/log_formatter.h>
7
8 #include <aws/common/date_time.h>
9 #include <aws/common/string.h>
10 #include <aws/common/thread.h>
11
12 #include <inttypes.h>
13 #include <stdarg.h>
14
15 /*
16 * Default formatter implementation
17 */
18
19 #ifdef _MSC_VER
20 # pragma warning(disable : 4204) /* non-constant aggregate initializer */
21 #endif
22
23 enum {
24 /* (max) strlen of "[<LogLevel>]" */
25 LOG_LEVEL_PREFIX_PADDING = 7,
26
27 /* (max) strlen of "[<ThreadId>]" */
28 THREAD_ID_PREFIX_PADDING = 22,
29
30 /* strlen of (user-content separator) " - " + "\n" + spaces between prefix fields + brackets around timestamp + 1 +
31 subject_name padding */
32 MISC_PADDING = 15,
33 };
34
35 #define MAX_LOG_LINE_PREFIX_SIZE \
36 (LOG_LEVEL_PREFIX_PADDING + THREAD_ID_PREFIX_PADDING + MISC_PADDING + AWS_DATE_TIME_STR_MAX_LEN)
37
38 static size_t s_advance_and_clamp_index(size_t current_index, int amount, size_t maximum) {
39 size_t next_index = current_index + amount;
40 if (next_index > maximum) {
41 next_index = maximum;
42 }
43
44 return next_index;
45 }
46
47 /* Thread-local string representation of current thread id */
48 AWS_THREAD_LOCAL struct {
49 bool is_valid;
50 char repr[AWS_THREAD_ID_T_REPR_BUFSZ];
51 } tl_logging_thread_id = {.is_valid = false};
52
53 int aws_format_standard_log_line(struct aws_logging_standard_formatting_data *formatting_data, va_list args) {
54 size_t current_index = 0;
55
56 /*
57 * Begin the log line with "[<Log Level>] ["
58 */
59 const char *level_string = NULL;
60 if (aws_log_level_to_string(formatting_data->level, &level_string)) {
61 return AWS_OP_ERR;
62 }
63
64 if (formatting_data->total_length == 0) {
65 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
66 }
67
68 /*
69 * Use this length for all but the last write, so we guarantee room for the newline even if we get truncated
70 */
71 size_t fake_total_length = formatting_data->total_length - 1;
72
73 int log_level_length = snprintf(formatting_data->log_line_buffer, fake_total_length, "[%s] [", level_string);
74 if (log_level_length < 0) {
75 return AWS_OP_ERR;
76 }
77
78 current_index = s_advance_and_clamp_index(current_index, log_level_length, fake_total_length);
79
80 if (current_index < fake_total_length) {
81 /*
82 * Add the timestamp. To avoid copies and allocations, do some byte buffer tomfoolery.
83 *
84 * First, make a byte_buf that points to the current position in the output string
85 */
86 struct aws_byte_buf timestamp_buffer = {
87 .allocator = formatting_data->allocator,
88 .buffer = (uint8_t *)formatting_data->log_line_buffer + current_index,
89 .capacity = fake_total_length - current_index,
90 .len = 0,
91 };
92
93 /*
94 * Output the current time to the byte_buf
95 */
96 struct aws_date_time current_time;
97 aws_date_time_init_now(&current_time);
98
99 int result = aws_date_time_to_utc_time_str(&current_time, formatting_data->date_format, &timestamp_buffer);
100 if (result != AWS_OP_SUCCESS) {
101 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
102 }
103
104 current_index = s_advance_and_clamp_index(current_index, (int)timestamp_buffer.len, fake_total_length);
105 }
106
107 if (current_index < fake_total_length) {
108 /*
109 * Add thread id and user content separator (" - ")
110 */
111 if (!tl_logging_thread_id.is_valid) {
112 aws_thread_id_t current_thread_id = aws_thread_current_thread_id();
113 if (aws_thread_id_t_to_string(current_thread_id, tl_logging_thread_id.repr, AWS_THREAD_ID_T_REPR_BUFSZ)) {
114 return AWS_OP_ERR;
115 }
116 tl_logging_thread_id.is_valid = true;
117 }
118 int thread_id_written = snprintf(
119 formatting_data->log_line_buffer + current_index,
120 fake_total_length - current_index,
121 "] [%s] ",
122 tl_logging_thread_id.repr);
123 if (thread_id_written < 0) {
124 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
125 }
126 current_index = s_advance_and_clamp_index(current_index, thread_id_written, fake_total_length);
127 }
128
129 if (current_index < fake_total_length) {
130 /* output subject name */
131 if (formatting_data->subject_name) {
132 int subject_written = snprintf(
133 formatting_data->log_line_buffer + current_index,
134 fake_total_length - current_index,
135 "[%s]",
136 formatting_data->subject_name);
137
138 if (subject_written < 0) {
139 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
140 }
141
142 current_index = s_advance_and_clamp_index(current_index, subject_written, fake_total_length);
143 }
144 }
145
146 if (current_index < fake_total_length) {
147 int separator_written =
148 snprintf(formatting_data->log_line_buffer + current_index, fake_total_length - current_index, " - ");
149 current_index = s_advance_and_clamp_index(current_index, separator_written, fake_total_length);
150 }
151
152 if (current_index < fake_total_length) {
153 /*
154 * Now write the actual data requested by the user
155 */
156 #ifdef _WIN32
157 int written_count = vsnprintf_s(
158 formatting_data->log_line_buffer + current_index,
159 fake_total_length - current_index,
160 _TRUNCATE,
161 formatting_data->format,
162 args);
163 #else
164 int written_count = vsnprintf(
165 formatting_data->log_line_buffer + current_index,
166 fake_total_length - current_index,
167 formatting_data->format,
168 args);
169 #endif /* _WIN32 */
170 if (written_count < 0) {
171 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
172 }
173
174 current_index = s_advance_and_clamp_index(current_index, written_count, fake_total_length);
175 }
176
177 /*
178 * End with a newline.
179 */
180 int newline_written_count =
181 snprintf(formatting_data->log_line_buffer + current_index, formatting_data->total_length - current_index, "\n");
182 if (newline_written_count < 0) {
183 return aws_raise_error(AWS_ERROR_UNKNOWN); /* we saved space, so this would be crazy */
184 }
185
186 formatting_data->amount_written = current_index + newline_written_count;
187
188 return AWS_OP_SUCCESS;
189 }
190
191 struct aws_default_log_formatter_impl {
192 enum aws_date_format date_format;
193 };
194
195 static int s_default_aws_log_formatter_format(
196 struct aws_log_formatter *formatter,
197 struct aws_string **formatted_output,
198 enum aws_log_level level,
199 aws_log_subject_t subject,
200 const char *format,
201 va_list args) {
202
203 (void)subject;
204
205 struct aws_default_log_formatter_impl *impl = formatter->impl;
206
207 if (formatted_output == NULL) {
208 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
209 }
210
211 /*
212 * Calculate how much room we'll need to build the full log line.
213 * You cannot consume a va_list twice, so we have to copy it.
214 */
215 va_list tmp_args;
216 va_copy(tmp_args, args);
217 #ifdef _WIN32
218 int required_length = _vscprintf(format, tmp_args) + 1;
219 #else
220 int required_length = vsnprintf(NULL, 0, format, tmp_args) + 1;
221 #endif
222 va_end(tmp_args);
223
224 /*
225 * Allocate enough room to hold the line. Then we'll (unsafely) do formatted IO directly into the aws_string
226 * memory.
227 */
228 const char *subject_name = aws_log_subject_name(subject);
229 int subject_name_len = 0;
230
231 if (subject_name) {
232 subject_name_len = (int)strlen(subject_name);
233 }
234
235 int total_length = required_length + MAX_LOG_LINE_PREFIX_SIZE + subject_name_len;
236 struct aws_string *raw_string = aws_mem_calloc(formatter->allocator, 1, sizeof(struct aws_string) + total_length);
237 if (raw_string == NULL) {
238 goto error_clean_up;
239 }
240
241 struct aws_logging_standard_formatting_data format_data = {
242 .log_line_buffer = (char *)raw_string->bytes,
243 .total_length = total_length,
244 .level = level,
245 .subject_name = subject_name,
246 .format = format,
247 .date_format = impl->date_format,
248 .allocator = formatter->allocator,
249 .amount_written = 0,
250 };
251
252 if (aws_format_standard_log_line(&format_data, args)) {
253 goto error_clean_up;
254 }
255
256 *(struct aws_allocator **)(&raw_string->allocator) = formatter->allocator;
257 *(size_t *)(&raw_string->len) = format_data.amount_written;
258
259 *formatted_output = raw_string;
260
261 return AWS_OP_SUCCESS;
262
263 error_clean_up:
264
265 if (raw_string != NULL) {
266 aws_mem_release(formatter->allocator, raw_string);
267 }
268
269 return AWS_OP_ERR;
270 }
271
272 static void s_default_aws_log_formatter_clean_up(struct aws_log_formatter *formatter) {
273 aws_mem_release(formatter->allocator, formatter->impl);
274 }
275
276 static struct aws_log_formatter_vtable s_default_log_formatter_vtable = {
277 .format = s_default_aws_log_formatter_format,
278 .clean_up = s_default_aws_log_formatter_clean_up,
279 };
280
281 int aws_log_formatter_init_default(
282 struct aws_log_formatter *formatter,
283 struct aws_allocator *allocator,
284 struct aws_log_formatter_standard_options *options) {
285 struct aws_default_log_formatter_impl *impl =
286 aws_mem_calloc(allocator, 1, sizeof(struct aws_default_log_formatter_impl));
287 impl->date_format = options->date_format;
288
289 formatter->vtable = &s_default_log_formatter_vtable;
290 formatter->allocator = allocator;
291 formatter->impl = impl;
292
293 return AWS_OP_SUCCESS;
294 }
295
296 void aws_log_formatter_clean_up(struct aws_log_formatter *formatter) {
297 AWS_ASSERT(formatter->vtable->clean_up);
298 (formatter->vtable->clean_up)(formatter);
299 }