1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <inttypes.h>
20 
21 #include <android-base/logging.h>
22 #include <android-base/stringprintf.h>
23 
24 #define LOGGER_ENTRY_MAX_PAYLOAD 4068  // This constant is not in the NDK.
25 
26 namespace android {
27 namespace base {
28 
29 // This splits the message up line by line, by calling log_function with a pointer to the start of
30 // each line and the size up to the newline character.  It sends size = -1 for the final line.
31 template <typename F, typename... Args>
SplitByLines(const char * msg,const F & log_function,Args &&...args)32 static void SplitByLines(const char* msg, const F& log_function, Args&&... args) {
33   const char* newline = strchr(msg, '\n');
34   while (newline != nullptr) {
35     log_function(msg, newline - msg, args...);
36     msg = newline + 1;
37     newline = strchr(msg, '\n');
38   }
39 
40   log_function(msg, -1, args...);
41 }
42 
43 // This splits the message up into chunks that logs can process delimited by new lines.  It calls
44 // log_function with the exact null terminated message that should be sent to logd.
45 // Note, despite the loops and snprintf's, if severity is not fatal and there are no new lines,
46 // this function simply calls log_function with msg without any extra overhead.
47 template <typename F>
SplitByLogdChunks(LogId log_id,LogSeverity severity,const char * tag,const char * file,unsigned int line,const char * msg,const F & log_function)48 static void SplitByLogdChunks(LogId log_id, LogSeverity severity, const char* tag, const char* file,
49                               unsigned int line, const char* msg, const F& log_function) {
50   // The maximum size of a payload, after the log header that logd will accept is
51   // LOGGER_ENTRY_MAX_PAYLOAD, so subtract the other elements in the payload to find the size of
52   // the string that we can log in each pass.
53   // The protocol is documented in liblog/README.protocol.md.
54   // Specifically we subtract a byte for the priority, the length of the tag + its null terminator,
55   // and an additional byte for the null terminator on the payload.  We subtract an additional 32
56   // bytes for slack, similar to java/android/util/Log.java.
57   ptrdiff_t max_size = LOGGER_ENTRY_MAX_PAYLOAD - strlen(tag) - 35;
58   if (max_size <= 0) {
59     abort();
60   }
61   // If we're logging a fatal message, we'll append the file and line numbers.
62   bool add_file = file != nullptr && (severity == FATAL || severity == FATAL_WITHOUT_ABORT);
63 
64   std::string file_header;
65   if (add_file) {
66     file_header = StringPrintf("%s:%u] ", file, line);
67   }
68   int file_header_size = file_header.size();
69 
70   __attribute__((uninitialized)) char logd_chunk[max_size + 1];
71   ptrdiff_t chunk_position = 0;
72 
73   auto call_log_function = [&]() {
74     log_function(log_id, severity, tag, logd_chunk);
75     chunk_position = 0;
76   };
77 
78   auto write_to_logd_chunk = [&](const char* message, int length) {
79     int size_written = 0;
80     const char* new_line = chunk_position > 0 ? "\n" : "";
81     if (add_file) {
82       size_written = snprintf(logd_chunk + chunk_position, sizeof(logd_chunk) - chunk_position,
83                               "%s%s%.*s", new_line, file_header.c_str(), length, message);
84     } else {
85       size_written = snprintf(logd_chunk + chunk_position, sizeof(logd_chunk) - chunk_position,
86                               "%s%.*s", new_line, length, message);
87     }
88 
89     // This should never fail, if it does and we set size_written to 0, which will skip this line
90     // and move to the next one.
91     if (size_written < 0) {
92       size_written = 0;
93     }
94     chunk_position += size_written;
95   };
96 
97   const char* newline = strchr(msg, '\n');
98   while (newline != nullptr) {
99     // If we have data in the buffer and this next line doesn't fit, write the buffer.
100     if (chunk_position != 0 && chunk_position + (newline - msg) + 1 + file_header_size > max_size) {
101       call_log_function();
102     }
103 
104     // Otherwise, either the next line fits or we have any empty buffer and too large of a line to
105     // ever fit, in both cases, we add it to the buffer and continue.
106     write_to_logd_chunk(msg, newline - msg);
107 
108     msg = newline + 1;
109     newline = strchr(msg, '\n');
110   }
111 
112   // If we have left over data in the buffer and we can fit the rest of msg, add it to the buffer
113   // then write the buffer.
114   if (chunk_position != 0 &&
115       chunk_position + static_cast<int>(strlen(msg)) + 1 + file_header_size <= max_size) {
116     write_to_logd_chunk(msg, -1);
117     call_log_function();
118   } else {
119     // If the buffer is not empty and we can't fit the rest of msg into it, write its contents.
120     if (chunk_position != 0) {
121       call_log_function();
122     }
123     // Then write the rest of the msg.
124     if (add_file) {
125       snprintf(logd_chunk, sizeof(logd_chunk), "%s%s", file_header.c_str(), msg);
126       log_function(log_id, severity, tag, logd_chunk);
127     } else {
128       log_function(log_id, severity, tag, msg);
129     }
130   }
131 }
132 
CountSizeAndNewLines(const char * message)133 static std::pair<int, int> CountSizeAndNewLines(const char* message) {
134   int size = 0;
135   int new_lines = 0;
136   while (*message != '\0') {
137     size++;
138     if (*message == '\n') {
139       ++new_lines;
140     }
141     ++message;
142   }
143   return {size, new_lines};
144 }
145 
146 // This adds the log header to each line of message and returns it as a string intended to be
147 // written to stderr.
StderrOutputGenerator(const struct tm & now,int pid,uint64_t tid,LogSeverity severity,const char * tag,const char * file,unsigned int line,const char * message)148 static std::string StderrOutputGenerator(const struct tm& now, int pid, uint64_t tid,
149                                          LogSeverity severity, const char* tag, const char* file,
150                                          unsigned int line, const char* message) {
151   char timestamp[32];
152   strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
153 
154   static const char log_characters[] = "VDIWEFF";
155   static_assert(arraysize(log_characters) - 1 == FATAL + 1,
156                 "Mismatch in size of log_characters and values in LogSeverity");
157   char severity_char = log_characters[severity];
158   std::string line_prefix;
159   if (file != nullptr) {
160     line_prefix = StringPrintf("%s %c %s %5d %5" PRIu64 " %s:%u] ", tag ? tag : "nullptr",
161                                severity_char, timestamp, pid, tid, file, line);
162   } else {
163     line_prefix = StringPrintf("%s %c %s %5d %5" PRIu64 " ", tag ? tag : "nullptr", severity_char,
164                                timestamp, pid, tid);
165   }
166 
167   auto [size, new_lines] = CountSizeAndNewLines(message);
168   std::string output_string;
169   output_string.reserve(size + new_lines * line_prefix.size() + 1);
170 
171   auto concat_lines = [&](const char* message, int size) {
172     output_string.append(line_prefix);
173     if (size == -1) {
174       output_string.append(message);
175     } else {
176       output_string.append(message, size);
177     }
178     output_string.append("\n");
179   };
180   SplitByLines(message, concat_lines);
181   return output_string;
182 }
183 
184 }  // namespace base
185 }  // namespace android
186