1
2#ifndef AWS_COMMON_LOG_WRITER_H
3#define AWS_COMMON_LOG_WRITER_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
22struct aws_allocator;
23struct aws_string;
24
25/*
26 * Log writer interface and default implementation(s)
27 *
28 * A log writer functions as a sink for formatted log lines. We provide
29 * default implementations that go to stdout, stderr, and a specified file.
30 */
31struct aws_log_writer;
32
33typedef int(aws_log_writer_write_fn)(struct aws_log_writer *writer, const struct aws_string *output);
34typedef void(aws_log_writer_clean_up_fn)(struct aws_log_writer *writer);
35
36struct aws_log_writer_vtable {
37 aws_log_writer_write_fn *write;
38 aws_log_writer_clean_up_fn *clean_up;
39};
40
41struct aws_log_writer {
42 struct aws_log_writer_vtable *vtable;
43 struct aws_allocator *allocator;
44 void *impl;
45};
46
47struct aws_log_writer_file_options {
48 const char *filename;
49 FILE *file;
50};
51
52AWS_EXTERN_C_BEGIN
53
54/*
55 * Initialize a log writer that sends log lines to stdout. Uses C library IO.
56 */
57AWS_COMMON_API
58int aws_log_writer_init_stdout(struct aws_log_writer *writer, struct aws_allocator *allocator);
59
60/*
61 * Initialize a log writer that sends log lines to stderr. Uses C library IO.
62 */
63AWS_COMMON_API
64int aws_log_writer_init_stderr(struct aws_log_writer *writer, struct aws_allocator *allocator);
65
66/*
67 * Initialize a log writer that sends log lines to a file. Uses C library IO.
68 */
69AWS_COMMON_API
70int aws_log_writer_init_file(
71 struct aws_log_writer *writer,
72 struct aws_allocator *allocator,
73 struct aws_log_writer_file_options *options);
74
75/*
76 * Frees all resources used by a log writer with the exception of the base structure memory
77 */
78AWS_COMMON_API
79void aws_log_writer_clean_up(struct aws_log_writer *writer);
80
81AWS_EXTERN_C_END
82
83#endif /* AWS_COMMON_LOG_WRITER_H */
84