| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/file.h> |
| 7 |
#include <aws/common/log_writer.h> |
| 8 |
|
| 9 |
#include <aws/common/string.h> |
| 10 |
|
| 11 |
#include <errno.h> |
| 12 |
#include <stdio.h> |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
struct aws_file_writer; |
| 19 |
|
| 20 |
struct aws_file_writer { |
| 21 |
FILE *log_file; |
| 22 |
bool close_file_on_cleanup; |
| 23 |
}; |
| 24 |
|
| 25 |
static int s_aws_file_writer_write(struct aws_log_writer *writer, const struct aws_string *output) { |
| 26 |
struct aws_file_writer *impl = (struct aws_file_writer *)writer->impl; |
| 27 |
|
| 28 |
size_t length = output->len; |
| 29 |
if (fwrite(output->bytes, 1, length, impl->log_file) < length) { |
| 30 |
int errno_value = ferror(impl->log_file) ? errno : 0; |
| 31 |
return aws_translate_and_raise_io_error_or(errno_value, AWS_ERROR_FILE_WRITE_FAILURE); |
| 32 |
} |
| 33 |
|
| 34 |
return AWS_OP_SUCCESS; |
| 35 |
} |
| 36 |
|
| 37 |
static void s_aws_file_writer_clean_up(struct aws_log_writer *writer) { |
| 38 |
struct aws_file_writer *impl = (struct aws_file_writer *)writer->impl; |
| 39 |
|
| 40 |
if (impl->close_file_on_cleanup) { |
| 41 |
fclose(impl->log_file); |
| 42 |
} |
| 43 |
|
| 44 |
aws_mem_release(writer->allocator, impl); |
| 45 |
} |
| 46 |
|
| 47 |
static struct aws_log_writer_vtable s_aws_file_writer_vtable = { |
| 48 |
.write = s_aws_file_writer_write, |
| 49 |
.clean_up = s_aws_file_writer_clean_up, |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
static int s_aws_file_writer_init_internal( |
| 56 |
struct aws_log_writer *writer, |
| 57 |
struct aws_allocator *allocator, |
| 58 |
const char *file_name_to_open, |
| 59 |
FILE *currently_open_file) { |
| 60 |
|
| 61 |
|
| 62 |
if (!((file_name_to_open != NULL) ^ (currently_open_file != NULL))) { |
| 63 |
return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
struct aws_file_writer *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_file_writer)); |
| 68 |
if (impl == NULL) { |
| 69 |
return AWS_OP_ERR; |
| 70 |
} |
| 71 |
|
| 72 |
impl->log_file = NULL; |
| 73 |
impl->close_file_on_cleanup = false; |
| 74 |
|
| 75 |
|
| 76 |
if (file_name_to_open != NULL) { |
| 77 |
impl->log_file = aws_fopen(file_name_to_open, "a+"); |
| 78 |
if (impl->log_file == NULL) { |
| 79 |
aws_mem_release(allocator, impl); |
| 80 |
return AWS_OP_ERR; |
| 81 |
} |
| 82 |
impl->close_file_on_cleanup = true; |
| 83 |
} else { |
| 84 |
impl->log_file = currently_open_file; |
| 85 |
} |
| 86 |
|
| 87 |
writer->vtable = &s_aws_file_writer_vtable; |
| 88 |
writer->allocator = allocator; |
| 89 |
writer->impl = impl; |
| 90 |
|
| 91 |
return AWS_OP_SUCCESS; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
int aws_log_writer_init_stdout(struct aws_log_writer *writer, struct aws_allocator *allocator) { |
| 98 |
return s_aws_file_writer_init_internal(writer, allocator, NULL, stdout); |
| 99 |
} |
| 100 |
|
| 101 |
int aws_log_writer_init_stderr(struct aws_log_writer *writer, struct aws_allocator *allocator) { |
| 102 |
return s_aws_file_writer_init_internal(writer, allocator, NULL, stderr); |
| 103 |
} |
| 104 |
|
| 105 |
int aws_log_writer_init_file( |
| 106 |
struct aws_log_writer *writer, |
| 107 |
struct aws_allocator *allocator, |
| 108 |
struct aws_log_writer_file_options *options) { |
| 109 |
return s_aws_file_writer_init_internal(writer, allocator, options->filename, options->file); |
| 110 |
} |
| 111 |
|
| 112 |
void aws_log_writer_clean_up(struct aws_log_writer *writer) { |
| 113 |
AWS_ASSERT(writer->vtable->clean_up); |
| 114 |
(writer->vtable->clean_up)(writer); |
| 115 |
} |