Add file name and line number to libsnapshot logs
Libsnapshot logs by default go through __android_log_logd_logger, which
completely discards file name and line number information. Format the
log before sending to __android_log_logd_logger to preserve these
information. This makes debugging easier
Test: th
Change-Id: Ic919064ac8d6fab8b5072e7b0f690a73d41f89d2
diff --git a/aosp/logging_android.cc b/aosp/logging_android.cc
index 5ccf7bc..8b5465a 100644
--- a/aosp/logging_android.cc
+++ b/aosp/logging_android.cc
@@ -22,6 +22,7 @@
#include <algorithm>
#include <functional>
#include <iomanip>
+#include <sstream>
#include <string>
#include <string_view>
#include <vector>
@@ -35,6 +36,7 @@
#include <base/strings/stringprintf.h>
#include <log/log.h>
+#include "android/log.h"
#include "update_engine/common/utils.h"
using std::string;
@@ -204,8 +206,23 @@
}
}
void operator()(const struct __android_log_message* log_message) {
- for (auto&& logger : loggers_) {
- logger(log_message);
+ if (log_message->file != nullptr && log_message->line != 0) {
+ __android_log_message formatted = *log_message;
+ std::stringstream ss;
+ ss << "[" << LogPriorityToCString(formatted.priority) << ":"
+ << formatted.file << "(" << formatted.line << ")] "
+ << formatted.message;
+ formatted.file = nullptr;
+ formatted.line = 0;
+ const auto str = ss.str();
+ formatted.message = str.c_str();
+ for (auto&& logger : loggers_) {
+ logger(&formatted);
+ }
+ } else {
+ for (auto&& logger : loggers_) {
+ logger(log_message);
+ }
}
}
@@ -248,7 +265,17 @@
} else {
// This will eventually be redirected to CombinedLogger.
// Use nullptr as tag so that liblog infers log tag from getprogname().
- __android_log_write(priority, nullptr /* tag */, str.c_str());
+ if (file == nullptr || file[0] == 0 || line == 0) {
+ __android_log_write(priority, nullptr /* tag */, str.c_str());
+ } else {
+ __android_log_print(priority,
+ nullptr,
+ "[%s:%s(%d)] %s",
+ LogPriorityToCString(priority),
+ file,
+ line,
+ str.c_str());
+ }
}
return true;
}