libbase output in host now look similar to the logcat output on device
I8189501d123fb79a255e17f675f8f6be617022e8 changed the output format of
host-side liblog. This change does the same for libbase.
For some unknown reason I don't fully understand, libbase has formatted
host-side logs by itself without relying on liblog. (note that libbase
"always" depends on liblog even for hosts. A follow-up might be reduce
the code duplication across libbase and liblog.
Bug: 287910948
Test: see the host logs
Test: atest --host libbase_test:logging_splitters
Test: atest --host libbase_test:logging
Test: atest --host libblog-host-test
Change-Id: Ie220d78a1c15686be7ae34cd5be88b858903f2e1
diff --git a/logging_splitters.h b/logging_splitters.h
index 2ec2b20..896ffc8 100644
--- a/logging_splitters.h
+++ b/logging_splitters.h
@@ -145,23 +145,31 @@
// This adds the log header to each line of message and returns it as a string intended to be
// written to stderr.
-static std::string StderrOutputGenerator(const struct tm& now, int pid, uint64_t tid,
+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) {
- char timestamp[32];
- strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
+ struct tm now;
+#if defined(_WIN32)
+ localtime_s(&now, &ts.tv_sec);
+#else
+ localtime_r(&ts.tv_sec, &now);
+#endif
+ char timestamp[sizeof("mm-DD HH:MM:SS.mmm\0")];
+ size_t n = strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
+ snprintf(timestamp + n, sizeof(timestamp) - n, ".%03ld", ts.tv_nsec / (1000 * 1000));
static const char log_characters[] = "VDIWEFF";
static_assert(arraysize(log_characters) - 1 == FATAL + 1,
"Mismatch in size of log_characters and values in LogSeverity");
char severity_char = log_characters[severity];
std::string line_prefix;
+ const char* real_tag = tag ? tag : "nullptr";
if (file != nullptr) {
- line_prefix = StringPrintf("%s %c %s %5d %5" PRIu64 " %s:%u] ", tag ? tag : "nullptr",
- severity_char, timestamp, pid, tid, file, line);
+ line_prefix = StringPrintf("%s %5d %5" PRIu64 " %c %-8s: %s:%u ", timestamp, pid, tid,
+ severity_char, real_tag, file, line);
} else {
- line_prefix = StringPrintf("%s %c %s %5d %5" PRIu64 " ", tag ? tag : "nullptr", severity_char,
- timestamp, pid, tid);
+ line_prefix =
+ StringPrintf("%s %5d %5" PRIu64 " %c %-8s: ", timestamp, pid, tid, severity_char, real_tag);
}
auto [size, new_lines] = CountSizeAndNewLines(message);