Snap for 11342248 from 79a683261f3b818e1062acea39e2e9464f202075 to emu-33-release

Change-Id: I4c04b480dd3fc700069508b56076b473f065ad77
diff --git a/host-common/include/host-common/logging.h b/host-common/include/host-common/logging.h
index a58b3f1..ac212ac 100644
--- a/host-common/include/host-common/logging.h
+++ b/host-common/include/host-common/logging.h
@@ -3,6 +3,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 011a27f..bb22e69 100644
--- a/host-common/logging.cpp
+++ b/host-common/logging.cpp
@@ -21,6 +21,9 @@
 
 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.
@@ -67,8 +70,16 @@
 
 }  // 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())
@@ -89,14 +100,28 @@
 
     // 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);
+    char temp[2048];
+    int ret = vsnprintf(temp, sizeof(temp), format, args);
+    temp[sizeof(temp) - 1] = 0;
+
+    if (logger) {
+        logger("%s\n", temp);
+    } else {
+        fprintf(stream, "%s\n", temp);
+    }
     va_end(args);
-    fprintf(stream, "\n");
+
 }