Skip to content

Commit 4c98208

Browse files
authored
Logging (#19)
1 parent 579825a commit 4c98208

File tree

7 files changed

+537
-78
lines changed

7 files changed

+537
-78
lines changed

bin/elasticurl/main.c

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ struct elasticurl_ctx {
3636
const char *verb;
3737
struct aws_uri uri;
3838
struct aws_condition_variable c_var;
39-
struct aws_http_request_options request_options;
4039
bool response_code_written;
4140
const char *cacert;
4241
const char *capath;
@@ -228,6 +227,7 @@ static void s_parse_options(int argc, char **argv, struct elasticurl_ctx *ctx) {
228227
static void s_on_incoming_body_fn(
229228
struct aws_http_stream *stream,
230229
const struct aws_byte_cursor *data,
230+
/* NOLINTNEXTLINE(readability-non-const-parameter) */
231231
size_t *out_window_update_size,
232232
void *user_data) {
233233

@@ -310,10 +310,9 @@ static void s_on_stream_complete_fn(struct aws_http_stream *stream, int error_co
310310
(void)error_code;
311311
(void)user_data;
312312
aws_http_stream_release(stream);
313-
aws_http_connection_release(aws_http_stream_get_connection(stream));
314313
}
315314

316-
static void s_onclient_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) {
315+
static void s_on_client_connection_setup(struct aws_http_connection *connection, int error_code, void *user_data) {
317316
struct elasticurl_ctx *app_ctx = user_data;
318317

319318
if (error_code) {
@@ -322,15 +321,16 @@ static void s_onclient_connection_setup(struct aws_http_connection *connection,
322321
return;
323322
}
324323

325-
app_ctx->request_options = (struct aws_http_request_options)AWS_HTTP_REQUEST_OPTIONS_INIT;
326-
app_ctx->request_options.uri = app_ctx->uri.path_and_query;
327-
app_ctx->request_options.user_data = app_ctx;
328-
app_ctx->request_options.client_connection = connection;
329-
app_ctx->request_options.method_str = aws_byte_cursor_from_c_str(app_ctx->verb);
330-
app_ctx->request_options.on_response_headers = s_on_incoming_headers_fn;
331-
app_ctx->request_options.on_response_header_block_done = s_on_incoming_header_block_done_fn;
332-
app_ctx->request_options.on_response_body = s_on_incoming_body_fn;
333-
app_ctx->request_options.on_complete = s_on_stream_complete_fn;
324+
struct aws_http_request_options request_options = AWS_HTTP_REQUEST_OPTIONS_INIT;
325+
request_options.uri = app_ctx->uri.path_and_query;
326+
request_options.user_data = app_ctx;
327+
request_options.client_connection = connection;
328+
request_options.method_str = aws_byte_cursor_from_c_str(app_ctx->verb);
329+
request_options.on_response_headers = s_on_incoming_headers_fn;
330+
request_options.on_response_header_block_done = s_on_incoming_header_block_done_fn;
331+
request_options.on_response_body = s_on_incoming_body_fn;
332+
request_options.on_complete = s_on_stream_complete_fn;
333+
334334
app_ctx->response_code_written = false;
335335

336336
/* only 10 custom header lines are supported, we send an additional 4 by default (hence 14). */
@@ -356,7 +356,7 @@ static void s_onclient_connection_setup(struct aws_http_connection *connection,
356356
headers[3].value = aws_byte_cursor_from_c_str(content_length);
357357
pre_header_count += 1;
358358
header_count += 1;
359-
app_ctx->request_options.stream_outgoing_body = s_stream_outgoing_body_fn;
359+
request_options.stream_outgoing_body = s_stream_outgoing_body_fn;
360360
} else if (app_ctx->data_file) {
361361
if (fseek(app_ctx->data_file, 0L, SEEK_END)) {
362362
fprintf(stderr, "failed to seek data file.\n");
@@ -372,7 +372,7 @@ static void s_onclient_connection_setup(struct aws_http_connection *connection,
372372
headers[3].value = aws_byte_cursor_from_c_str(content_length);
373373
pre_header_count += 1;
374374
header_count += 1;
375-
app_ctx->request_options.stream_outgoing_body = s_stream_outgoing_body_fn;
375+
request_options.stream_outgoing_body = s_stream_outgoing_body_fn;
376376
}
377377

378378
assert(app_ctx->header_line_count <= 10);
@@ -390,18 +390,24 @@ static void s_onclient_connection_setup(struct aws_http_connection *connection,
390390
header_count++;
391391
}
392392

393-
app_ctx->request_options.header_array = headers;
394-
app_ctx->request_options.num_headers = header_count;
393+
request_options.header_array = headers;
394+
request_options.num_headers = header_count;
395+
396+
struct aws_http_stream *stream = aws_http_stream_new_client_request(&request_options);
397+
if (!stream) {
398+
fprintf(stderr, "failed to create request.");
399+
exit(1);
400+
}
395401

396-
aws_http_stream_new_client_request(&app_ctx->request_options);
402+
/* Release hold on connection, it will clean itself up once stream completes */
403+
aws_http_connection_release(connection);
397404
}
398405

399406
static void s_on_client_connection_shutdown(struct aws_http_connection *connection, int error_code, void *user_data) {
400407
(void)error_code;
401408
(void)connection;
402409
struct elasticurl_ctx *app_ctx = user_data;
403410

404-
aws_http_connection_release(connection);
405411
aws_condition_variable_notify_all(&app_ctx->c_var);
406412
}
407413

@@ -435,48 +441,23 @@ int main(int argc, char **argv) {
435441
AWS_ZERO_STRUCT(log_channel);
436442
if (app_ctx.log_level) {
437443
aws_io_load_log_subject_strings();
444+
aws_http_load_log_subject_strings();
438445

439-
if (app_ctx.trace_file) {
440-
struct aws_logger_standard_options options = {
441-
.level = app_ctx.log_level,
442-
.filename = app_ctx.trace_file,
443-
};
446+
struct aws_logger_standard_options options = {
447+
.level = app_ctx.log_level,
448+
};
444449

445-
if (aws_logger_init_standard(&logger, allocator, &options)) {
446-
fprintf(stderr, "Failed to initialize logger with error %s\n", aws_error_debug_str(aws_last_error()));
447-
exit(1);
448-
}
450+
if (app_ctx.trace_file) {
451+
options.filename = app_ctx.trace_file;
449452
} else {
450-
if (aws_log_writer_init_stderr(&log_writer, allocator)) {
451-
fprintf(
452-
stderr, "Failed to initialize log writer with error %s\n", aws_error_debug_str(aws_last_error()));
453-
exit(1);
454-
}
455-
456-
struct aws_log_formatter_standard_options options = {
457-
.date_format = AWS_DATE_FORMAT_ISO_8601,
458-
};
459-
460-
if (aws_log_formatter_init_default(&log_formatter, allocator, &options)) {
461-
fprintf(
462-
stderr,
463-
"Failed to initialize log formatter with error %s\n",
464-
aws_error_debug_str(aws_last_error()));
465-
exit(1);
466-
}
467-
468-
if (aws_log_channel_init_background(&log_channel, allocator, &log_writer)) {
469-
fprintf(
470-
stderr, "Failed to initialize log channel with error %s\n", aws_error_debug_str(aws_last_error()));
471-
exit(1);
472-
}
453+
options.file = stderr;
454+
}
473455

474-
if (aws_logger_init_from_external(
475-
&logger, allocator, &log_formatter, &log_channel, &log_writer, app_ctx.log_level)) {
476-
fprintf(stderr, "Failed to initialize logger with error %s\n", aws_error_debug_str(aws_last_error()));
477-
exit(1);
478-
}
456+
if (aws_logger_init_standard(&logger, allocator, &options)) {
457+
fprintf(stderr, "Failed to initialize logger with error %s\n", aws_error_debug_str(aws_last_error()));
458+
exit(1);
479459
}
460+
480461
aws_logger_set(&logger);
481462
}
482463

@@ -592,7 +573,7 @@ int main(int argc, char **argv) {
592573
.initial_window_size = SIZE_MAX,
593574
.tls_options = tls_options,
594575
.user_data = &app_ctx,
595-
.on_setup = s_onclient_connection_setup,
576+
.on_setup = s_on_client_connection_setup,
596577
.on_shutdown = s_on_client_connection_shutdown,
597578
};
598579

@@ -613,7 +594,7 @@ int main(int argc, char **argv) {
613594
aws_tls_clean_up_static_state();
614595

615596
if (app_ctx.log_level) {
616-
aws_logger_cleanup(&logger);
597+
aws_logger_clean_up(&logger);
617598
}
618599

619600
aws_uri_clean_up(&app_ctx.uri);

include/aws/http/http.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,25 @@ struct aws_http_header {
200200
struct aws_byte_cursor value;
201201
};
202202

203+
enum aws_http_log_subject {
204+
AWS_LS_HTTP_GENERAL = 0x800,
205+
AWS_LS_HTTP_CONNECTION,
206+
AWS_LS_HTTP_SERVER,
207+
AWS_LS_HTTP_STREAM,
208+
};
209+
203210
AWS_EXTERN_C_BEGIN
204211

205212
/**
206213
* Loads error strings for this API so that aws_last_error_str etc. will return useful debug strings.
207214
*/
208215
AWS_HTTP_API void aws_http_load_error_strings(void);
209216

217+
/**
218+
* Load aws-c-http's log subject strings.
219+
*/
220+
AWS_HTTP_API void aws_http_load_log_subject_strings(void);
221+
210222
AWS_HTTP_API const char *aws_http_header_name_to_str(enum aws_http_header_name name);
211223
AWS_HTTP_API const char *aws_http_method_to_str(enum aws_http_method method);
212224
AWS_HTTP_API const char *aws_http_version_to_str(enum aws_http_version version);

0 commit comments

Comments
 (0)