27#define LOGGER_ENTRY_MAX_PAYLOAD 4068
34template <
typename F,
typename...
Args>
36 const char* newline = strchr(msg,
'\n');
37 while (newline !=
nullptr) {
38 log_function(msg, newline - msg, args...);
40 newline = strchr(msg,
'\n');
43 log_function(msg, -1, args...);
52 unsigned int line,
const char* msg,
const F& log_function) {
67 std::string file_header;
71 int file_header_size = file_header.size();
73 __attribute__((uninitialized)) std::vector<char> logd_chunk(max_size + 1);
74 ptrdiff_t chunk_position = 0;
76 auto call_log_function = [&]() {
77 log_function(
log_id, severity, tag, logd_chunk.data());
81 auto write_to_logd_chunk = [&](
const char* message,
int length) {
83 const char* new_line = chunk_position > 0 ?
"\n" :
"";
85 size_written = snprintf(logd_chunk.data() + chunk_position, logd_chunk.size() - chunk_position,
86 "%s%s%.*s", new_line, file_header.c_str(), length, message);
88 size_written = snprintf(logd_chunk.data() + chunk_position, logd_chunk.size() - chunk_position,
89 "%s%.*s", new_line, length, message);
94 if (size_written < 0) {
97 chunk_position += size_written;
100 const char* newline = strchr(msg,
'\n');
101 while (newline !=
nullptr) {
103 if (chunk_position != 0 && chunk_position + (newline - msg) + 1 + file_header_size > max_size) {
109 write_to_logd_chunk(msg, newline - msg);
112 newline = strchr(msg,
'\n');
117 if (chunk_position != 0 &&
118 chunk_position +
static_cast<int>(strlen(msg)) + 1 + file_header_size <= max_size) {
119 write_to_logd_chunk(msg, -1);
123 if (chunk_position != 0) {
128 snprintf(logd_chunk.data(), logd_chunk.size(),
"%s%s", file_header.c_str(), msg);
129 log_function(
log_id, severity, tag, logd_chunk.data());
131 log_function(
log_id, severity, tag, msg);
139 while (*message !=
'\0') {
141 if (*message ==
'\n') {
146 return {
size, new_lines};
152 LogSeverity severity,
const char* tag,
const char* file,
153 unsigned int line,
const char* message) {
155 localtime_r(&ts.tv_sec, &
now);
156 char timestamp[
sizeof(
"mm-DD HH:MM:SS.mmm\0")];
157 size_t n = strftime(timestamp,
sizeof(timestamp),
"%m-%d %H:%M:%S", &
now);
158 snprintf(timestamp + n,
sizeof(timestamp) - n,
".%03ld", ts.tv_nsec / (1000 * 1000));
160 static const char log_characters[] =
"VDIWEFF";
162 "Mismatch in size of log_characters and values in LogSeverity");
163 char severity_char = log_characters[severity];
164 std::string line_prefix;
165 const char* real_tag = tag ? tag :
"nullptr";
166 if (file !=
nullptr) {
167 line_prefix =
StringPrintf(
"%s %5d %5" PRIu64
" %c %-8s: %s:%u ", timestamp, pid, tid,
168 severity_char, real_tag, file, line);
171 StringPrintf(
"%s %5d %5" PRIu64
" %c %-8s: ", timestamp, pid, tid, severity_char, real_tag);
175 std::string output_string;
176 output_string.reserve(
size + new_lines * line_prefix.size() + 1);
178 auto concat_lines = [&](
const char* message,
int size) {
179 output_string.append(line_prefix);
181 output_string.append(message);
183 output_string.append(message,
size);
185 output_string.append(
"\n");
188 return output_string;
log_id
Definition: log.h:138
uint32_t size
Definition: io.h:2
#define LOGGER_ENTRY_MAX_PAYLOAD
Definition: logging_splitters.h:27
#define arraysize(array)
Definition: macros.h:76
static std::pair< int, int > CountSizeAndNewLines(const char *message)
Definition: logging_splitters.h:136
std::string StringPrintf(const char *fmt,...) __attribute__((__format__(__printf__
Definition: stringprintf.cpp:68
static void SplitByLogdChunks(LogId log_id, LogSeverity severity, const char *tag, const char *file, unsigned int line, const char *msg, const F &log_function)
Definition: logging_splitters.h:51
LogId
Definition: logging.h:97
static std::string StderrOutputGenerator(const struct timespec &ts, int pid, uint64_t tid, LogSeverity severity, const char *tag, const char *file, unsigned int line, const char *message)
Definition: logging_splitters.h:151
LogSeverity
Definition: logging.h:87
@ FATAL_WITHOUT_ABORT
Definition: logging.h:93
@ FATAL
Definition: logging.h:94
static void SplitByLines(const char *msg, const F &log_function, Args &&... args)
Definition: logging_splitters.h:35
class incremental::File __attribute__
std::vector< std::string_view > Args
Definition: incremental.h:28
double now()
Definition: util.cpp:45