Debian Codegraph @

crt/aws-c-common/source/android/logging.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 <android/log.h>
7
8 #include <aws/common/clock.h>
9 #include <aws/common/logging.h>
10 #include <aws/common/string.h>
11
12 #include <inttypes.h>
13 #include <stdarg.h>
14
15 #define LOGCAT_MAX_BUFFER_SIZE (4 * 1024)
16
17 struct logcat_format_data {
18 char *buffer;
19 size_t bytes_written;
20 size_t total_length;
21 const char *format;
22 };
23
24 static size_t s_advance_and_clamp_index(size_t current_index, int amount, size_t maximum) {
25 size_t next_index = current_index + amount;
26 if (next_index > maximum) {
27 next_index = maximum;
28 }
29
30 return next_index;
31 }
32
33 /* Override this for Android, as time and log level are taken care of by logcat */
34 static int s_logcat_format(struct logcat_format_data *formatting_data, va_list args) {
35 size_t current_index = 0;
36
37 if (formatting_data->total_length == 0) {
38 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
39 }
40
41 /*
42 * Use this length for all but the last write, so we guarantee room for the newline even if we get truncated
43 */
44 size_t fake_total_length = formatting_data->total_length - 1;
45
46 if (current_index < fake_total_length) {
47 /*
48 * Add thread id and user content separator (" - ")
49 */
50 aws_thread_id_t current_thread_id = aws_thread_current_thread_id();
51 char thread_id[AWS_THREAD_ID_T_REPR_BUFSZ];
52 if (aws_thread_id_t_to_string(current_thread_id, thread_id, AWS_THREAD_ID_T_REPR_BUFSZ)) {
53 return AWS_OP_ERR;
54 }
55 int thread_id_written =
56 snprintf(formatting_data->buffer + current_index, fake_total_length - current_index, "[%s] ", thread_id);
57 if (thread_id_written < 0) {
58 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
59 }
60 current_index = s_advance_and_clamp_index(current_index, thread_id_written, fake_total_length);
61 }
62
63 if (current_index < fake_total_length) {
64 uint64_t now = 0;
65 aws_high_res_clock_get_ticks(&now);
66
67 int current_time_written = snprintf(
68 formatting_data->buffer + current_index, fake_total_length - current_index, "(HRC:%" PRIu64 ") ", now);
69 if (current_time_written < 0) {
70 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
71 }
72 current_index = s_advance_and_clamp_index(current_index, current_time_written, fake_total_length);
73 }
74
75 if (current_index < fake_total_length) {
76 int separator_written =
77 snprintf(formatting_data->buffer + current_index, fake_total_length - current_index, " - ");
78 current_index = s_advance_and_clamp_index(current_index, separator_written, fake_total_length);
79 }
80
81 if (current_index < fake_total_length) {
82 /*
83 * Now write the actual data requested by the user
84 */
85
86 int written_count = vsnprintf(
87 formatting_data->buffer + current_index, fake_total_length - current_index, formatting_data->format, args);
88
89 if (written_count < 0) {
90 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
91 }
92
93 current_index = s_advance_and_clamp_index(current_index, written_count, fake_total_length);
94 }
95
96 /*
97 * End with a newline.
98 */
99 int newline_written_count =
100 snprintf(formatting_data->buffer + current_index, formatting_data->total_length - current_index, "\n");
101 if (newline_written_count < 0) {
102 return aws_raise_error(AWS_ERROR_UNKNOWN); /* we saved space, so this would be crazy */
103 }
104
105 formatting_data->bytes_written = current_index + newline_written_count;
106
107 return AWS_OP_SUCCESS;
108 }
109
110 static struct aws_logger_logcat {
111 enum aws_log_level level;
112 } s_logcat_impl;
113
114 static int s_logcat_log(
115 struct aws_logger *logger,
116 enum aws_log_level log_level,
117 aws_log_subject_t subject,
118 const char *format,
119 ...) {
120 (void)logger;
121
122 va_list format_args;
123 va_start(format_args, format);
124
125 char buffer[LOGCAT_MAX_BUFFER_SIZE];
126 struct logcat_format_data fmt = {
127 .buffer = buffer,
128 .total_length = AWS_ARRAY_SIZE(buffer),
129 .format = format,
130 };
131
132 int result = s_logcat_format(&fmt, format_args);
133
134 va_end(format_args);
135
136 if (result != AWS_OP_SUCCESS) {
137 return AWS_OP_ERR;
138 }
139
140 /* ANDROID_LOG_VERBOSE = 2, ANDROID_LOG_FATAL = 7 */
141 const int prio = 0x8 - log_level;
142 __android_log_write(prio, aws_log_subject_name(subject), buffer);
143
144 return AWS_OP_SUCCESS;
145 }
146
147 static enum aws_log_level s_logcat_get_log_level(struct aws_logger *logger, aws_log_subject_t subject) {
148 (void)subject;
149 struct aws_logger_logcat *impl = logger->p_impl;
150 return impl->level;
151 }
152
153 static void s_logcat_clean_up(struct aws_logger *logger) {
154 logger->p_impl = NULL;
155 }
156
157 static struct aws_logger_vtable s_logcat_vtable = {
158 .log = s_logcat_log,
159 .get_log_level = s_logcat_get_log_level,
160 .clean_up = s_logcat_clean_up,
161 };
162
163 int aws_logger_init_logcat(
164 struct aws_logger *logger,
165 struct aws_allocator *allocator,
166 struct aws_logger_standard_options *options) {
167
168 logger->allocator = allocator;
169 logger->vtable = &s_logcat_vtable;
170 logger->p_impl = &s_logcat_impl;
171 s_logcat_impl.level = options->level;
172
173 return AWS_OP_SUCCESS;
174 }