diff options
author | Yahan Zhou <yahan@google.com> | 2023-12-06 14:13:23 -0800 |
---|---|---|
committer | Yahan Zhou <yahan@google.com> | 2023-12-06 14:13:23 -0800 |
commit | f3cee276792cbd7e506597c5f8f468dc7da06799 (patch) | |
tree | 8fde70dabfe461cdc1cd919992c2ec63a39db0ab | |
parent | ed69e33fb47e6cbe1b1d07c63d4d293dabc770f6 (diff) | |
download | aemu-f3cee276792cbd7e506597c5f8f468dc7da06799.tar.gz |
Allow setting up external loggers
We need to collect more logs in crash reports to debug initialization
failures.
Bug: 314841455
Change-Id: I370320adde52ecd28ae209a116ced846c1c48cd9
-rw-r--r-- | host-common/include/host-common/logging.h | 5 | ||||
-rw-r--r-- | host-common/logging.cpp | 35 |
2 files changed, 35 insertions, 5 deletions
diff --git a/host-common/include/host-common/logging.h b/host-common/include/host-common/logging.h index 24b3c3e..493f006 100644 --- a/host-common/include/host-common/logging.h +++ b/host-common/include/host-common/logging.h @@ -17,6 +17,11 @@ #include <cstdint> #include <cstdio> +typedef void (*gfxstream_logger_t)(const char* fmt, ...); + +void set_gfxstream_logger(gfxstream_logger_t f); +void set_gfxstream_fine_logger(gfxstream_logger_t f); + // Outputs a log line using Google's standard prefix. (http://go/logging#prefix) // // Do not use this function directly. Instead, use one of the logging macros below. diff --git a/host-common/logging.cpp b/host-common/logging.cpp index 2c11780..395169b 100644 --- a/host-common/logging.cpp +++ b/host-common/logging.cpp @@ -35,6 +35,9 @@ namespace { constexpr int kMaxThreadIdLength = 7; // 7 digits for the thread id is what Google uses everywhere. +gfxstream_logger_t sLogger = nullptr; +gfxstream_logger_t sFineLogger = nullptr; + // Returns the current thread id as a string of at most kMaxThreadIdLength characters. // We try to avoid using std::this_thread::get_id() because on Linux at least it returns a long // number (e.g. 139853607339840) which isn't the same as the thread id from the OS itself. @@ -81,8 +84,16 @@ const char* GetFileBasename(const char* file) { } // namespace +void set_gfxstream_logger(gfxstream_logger_t f) { sLogger = f; } + +void set_gfxstream_fine_logger(gfxstream_logger_t f) { sFineLogger = f; } + void OutputLog(FILE* stream, char severity, const char* file, unsigned int line, int64_t timestamp_us, const char* format, ...) { + gfxstream_logger_t logger = + severity == 'I' || severity == 'W' || severity == 'E' || severity == 'F' ? sLogger + : sFineLogger; + if (timestamp_us == 0) { timestamp_us = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::system_clock::now().time_since_epoch()) @@ -103,14 +114,28 @@ void OutputLog(FILE* stream, char severity, const char* file, unsigned int line, // Output the standard Google logging prefix // See also: https://github.com/google/glog/blob/9dc1107f88d3a1613d61b80040d83c1c1acbac3d/src/logging.cc#L1612-L1615 - fprintf(stream, "%c%02d%02d %02d:%02d:%02d.%06" PRId64 " %7s %s:%d] ", severity, - ts_parts.tm_mon + 1, ts_parts.tm_mday, ts_parts.tm_hour, ts_parts.tm_min, - ts_parts.tm_sec, microseconds, getCachedThreadID(), GetFileBasename(file), line); + if (logger) { + logger("%c%02d%02d %02d:%02d:%02d.%06" PRId64 " %7s %s:%d] ", severity, ts_parts.tm_mon + 1, + ts_parts.tm_mday, ts_parts.tm_hour, ts_parts.tm_min, ts_parts.tm_sec, microseconds, + getCachedThreadID(), GetFileBasename(file), line); + } else { + fprintf(stream, "%c%02d%02d %02d:%02d:%02d.%06" PRId64 " %7s %s:%d] ", severity, + ts_parts.tm_mon + 1, ts_parts.tm_mday, ts_parts.tm_hour, ts_parts.tm_min, + ts_parts.tm_sec, microseconds, getCachedThreadID(), GetFileBasename(file), line); + } // Output the actual log message and newline va_list args; va_start(args, format); - vfprintf(stream, format, args); + if (logger) { + logger(format, args); + } else { + vfprintf(stream, format, args); + } va_end(args); - fprintf(stream, "\n"); + if (logger) { + logger("\n"); + } else { + fprintf(stream, "\n"); + } } |