[goldfish] Remove syslog_static dependency

This removes the syslog_static dependency from mesa.

Bug: b/312557350
Change-Id: Iedb20b44a487b34228bc125b8a650627b0773126
diff --git a/BUILD.gn b/BUILD.gn
index ae9597b..f9e730a 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -188,10 +188,10 @@
       "//sdk/fidl/fuchsia.hardware.goldfish:fuchsia.hardware.goldfish_cpp",
       "//sdk/fidl/fuchsia.logger:fuchsia.logger_cpp",
       "//sdk/fidl/fuchsia.sysmem:fuchsia.sysmem_cpp",
+      "//sdk/lib/syslog/structured_backend",
       "//src/zircon/lib/zircon",
       "//zircon/system/ulib/async:async-cpp",
       "//zircon/system/ulib/async-loop:async-loop-cpp",
-      "//zircon/system/ulib/syslog:syslog-static",
       "//zircon/system/ulib/trace:trace-with-static-engine",
       "//zircon/system/ulib/trace-provider:trace-provider-with-static-engine",
       "//zircon/system/ulib/zx",
diff --git a/fuchsia/fuchsia_stdio.cc b/fuchsia/fuchsia_stdio.cc
index 5948250..303b834 100644
--- a/fuchsia/fuchsia_stdio.cc
+++ b/fuchsia/fuchsia_stdio.cc
@@ -3,12 +3,24 @@
 // found in the LICENSE file.
 
 #include <assert.h>
-#include <lib/syslog/global.h>
+#include <lib/syslog/structured_backend/cpp/fuchsia_syslog.h>
+#include <log/log.h>
 #include <stdio.h>
 #include <stdlib.h>
 
+#include <cstdarg>
+
+void goldfish_fuchsia_log_vararg(int8_t severity, const char* tag, const char* file, int line,
+                                 const char* format, ...) {
+    va_list args;
+    va_start(args, format);
+    goldfish_fuchsia_log(severity, tag, file, line, format, args);
+    va_end(args);
+}
+
 void __assert_fail(const char* expr, const char* file, int line, const char* func) {
-    FX_LOGF(ERROR, "goldfish", "Assertion failed: %s (%s: %s: %d)", expr, file, func, line);
+    goldfish_fuchsia_log_vararg(FUCHSIA_LOG_ERROR, "goldfish", file, line,
+                                "Assertion failed: %s (%s: %s: %d)", expr, file, func, line);
     abort();
 }
 
@@ -35,14 +47,15 @@
     return 0;
 }
 
-static inline fx_log_severity_t severity(FILE* stream) {
-    return stream == stdout ? FX_LOG_INFO : FX_LOG_ERROR;
+static inline FuchsiaLogSeverity severity(FILE* stream) {
+    return stream == stdout ? FUCHSIA_LOG_INFO : FUCHSIA_LOG_ERROR;
 }
 
 int fputs(const char* s, FILE* stream) {
     assert(stream == stdout || stream == stderr);
     if (stream == stdout || stream == stderr) {
-        _FX_LOG(severity(stream), "goldfish", s);
+        // File is set to nullptr as that information isn't available here.
+        goldfish_fuchsia_log_vararg(severity(stream), "goldfish", nullptr, 0, s);
     }
     return 0;
 }
@@ -50,7 +63,7 @@
 int vfprintf(FILE* stream, const char* format, va_list ap) {
     assert(stream == stdout || stream == stderr);
     if (stream == stdout || stream == stderr) {
-        _FX_LOGVF(severity(stream), "goldfish", __FILE__, __LINE__, format, ap);
+        goldfish_fuchsia_log(severity(stream), "goldfish", __FILE__, __LINE__, format, ap);
     }
     return 0;
 }
diff --git a/fuchsia/include/log/log.h b/fuchsia/include/log/log.h
index 8b451ae..37c96e4 100644
--- a/fuchsia/include/log/log.h
+++ b/fuchsia/include/log/log.h
@@ -2,5 +2,14 @@
 #define __LOG_LOG_H__
 
 #include <cutils/log.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+#ifdef __Fuchsia__
+extern "C" {
+void goldfish_fuchsia_log(int8_t severity, const char* tag, const char* file, int line,
+                          const char* format, va_list va);
+}
+#endif
 
 #endif
diff --git a/fuchsia/port.cc b/fuchsia/port.cc
index d68c1ca..0c81b6c 100644
--- a/fuchsia/port.cc
+++ b/fuchsia/port.cc
@@ -13,7 +13,8 @@
 #include <thread>
 
 #if defined(__Fuchsia__)
-#include <lib/syslog/global.h>
+#include <lib/syslog/structured_backend/cpp/fuchsia_syslog.h>
+#include <log/log.h>
 #else
 #include <libgen.h>
 #endif
@@ -53,20 +54,23 @@
     switch (priority) {
         case ANDROID_LOG_VERBOSE:
         case ANDROID_LOG_DEBUG:
-            FX_LOGVF(DEBUG, local_tag, file, line, format, ap);
+            // Debug and verbose logs are intentionally dropped by Goldfish to match
+            // previous behavior.
             break;
         case ANDROID_LOG_WARN:
-            FX_LOGVF(WARNING, local_tag, file, line, format, ap);
+            goldfish_fuchsia_log(FUCHSIA_LOG_WARNING, local_tag, file, line, format, ap);
             break;
         case ANDROID_LOG_ERROR:
-            FX_LOGVF(ERROR, local_tag, file, line, format, ap);
+            goldfish_fuchsia_log(FUCHSIA_LOG_ERROR, local_tag, file, line, format, ap);
             break;
         case ANDROID_LOG_FATAL:
-            FX_LOGVF(FATAL, local_tag, file, line, format, ap);
+            goldfish_fuchsia_log(FUCHSIA_LOG_FATAL, local_tag, file, line, format, ap);
+            // Most repositories choose to panic when getting a FATAL log message.
+            abort();
             break;
         case ANDROID_LOG_INFO:
         default:
-            FX_LOGVF(INFO, local_tag, file, line, format, ap);
+            goldfish_fuchsia_log(FUCHSIA_LOG_INFO, local_tag, file, line, format, ap);
             break;
     }
 #else
@@ -85,7 +89,7 @@
     va_list ap;
     va_start(ap, format);
 #if defined(__Fuchsia__)
-    FX_LOGVF(ERROR, local_tag, file, line, format, ap);
+    goldfish_fuchsia_log(FUCHSIA_LOG_ERROR, local_tag, file, line, format, ap);
 #else
     linux_log_prefix(local_tag, file, line, format, ap);
 #endif
diff --git a/system/vulkan/goldfish_vulkan.cpp b/system/vulkan/goldfish_vulkan.cpp
index 6b45e47..13c7698 100644
--- a/system/vulkan/goldfish_vulkan.cpp
+++ b/system/vulkan/goldfish_vulkan.cpp
@@ -17,11 +17,14 @@
 #include <string.h>
 #ifdef VK_USE_PLATFORM_FUCHSIA
 #include <fidl/fuchsia.logger/cpp/wire.h>
-#include <lib/syslog/global.h>
+#include <lib/syslog/structured_backend/cpp/fuchsia_syslog.h>
 #include <lib/zx/channel.h>
 #include <lib/zx/socket.h>
 #include <lib/zxio/zxio.h>
 #include <unistd.h>
+#include <zircon/system/public/zircon/process.h>
+
+#include <cstdarg>
 
 #include "TraceProviderFuchsia.h"
 #include "services/service_connector.h"
@@ -746,6 +749,8 @@
     const bool mHostSupportsGoldfish;
 };
 
+zx::socket g_log_socket = zx::socket(ZX_HANDLE_INVALID);
+
 void VulkanDevice::InitLogger() {
     auto log_socket = ([]() -> std::optional<zx::socket> {
         fidl::ClientEnd<fuchsia_logger::LogSink> channel{
@@ -756,7 +761,7 @@
         zx_status_t status = zx::socket::create(ZX_SOCKET_DATAGRAM, &local_socket, &remote_socket);
         if (status != ZX_OK) return std::nullopt;
 
-        auto result = fidl::WireCall(channel)->Connect(std::move(remote_socket));
+        auto result = fidl::WireCall(channel)->ConnectStructured(std::move(remote_socket));
 
         if (!result.ok()) return std::nullopt;
 
@@ -764,16 +769,85 @@
     })();
     if (!log_socket) return;
 
-    fx_logger_config_t config = {
-        .min_severity = FX_LOG_INFO,
-        .log_sink_socket = log_socket->release(),
-        .tags = nullptr,
-        .num_tags = 0,
-    };
-
-    fx_log_reconfigure(&config);
+    g_log_socket = std::move(*log_socket);
 }
 
+namespace {
+zx_koid_t GetKoid(zx_handle_t handle) {
+    zx_info_handle_basic_t info;
+    zx_status_t status =
+        zx_object_get_info(handle, ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr, nullptr);
+    return status == ZX_OK ? info.koid : ZX_KOID_INVALID;
+}
+
+static zx_koid_t pid = GetKoid(zx_process_self());
+
+static thread_local zx_koid_t tid = GetKoid(zx_thread_self());
+
+cpp17::optional<cpp17::string_view> CStringToStringView(const char* cstr) {
+    if (!cstr) {
+        return cpp17::nullopt;
+    }
+    return cstr;
+}
+
+const char* StripDots(const char* path) {
+    while (strncmp(path, "../", 3) == 0) {
+        path += 3;
+    }
+    return path;
+}
+
+const char* StripPath(const char* path) {
+    auto p = strrchr(path, '/');
+    if (p) {
+        return p + 1;
+    } else {
+        return path;
+    }
+}
+
+const char* StripFile(const char* file, FuchsiaLogSeverity severity) {
+    return severity > FUCHSIA_LOG_INFO ? StripDots(file) : StripPath(file);
+}
+
+extern "C" void goldfish_fuchsia_log(int8_t severity, const char* tag, const char* file, int line,
+                                     const char* format, va_list va) {
+    if (!g_log_socket.is_valid()) {
+        abort();
+    }
+    fuchsia_syslog::LogBuffer buffer;
+    constexpr size_t kFormatStringLength = 1024;
+    char fmt_string[kFormatStringLength];
+    fmt_string[kFormatStringLength - 1] = 0;
+    int n = kFormatStringLength;
+    // Format
+    // Number of bytes written not including null terminator
+    int count = vsnprintf(fmt_string, n, format, va) + 1;
+    if (count < 0) {
+        // No message to write.
+        return;
+    }
+
+    if (count >= n) {
+        // truncated
+        constexpr char kEllipsis[] = "...";
+        constexpr size_t kEllipsisSize = sizeof(kEllipsis);
+        snprintf(fmt_string + kFormatStringLength - 1 - kEllipsisSize, kEllipsisSize, kEllipsis);
+    }
+
+    if (file) {
+        file = StripFile(file, severity);
+    }
+    buffer.BeginRecord(severity, CStringToStringView(file), line, fmt_string, g_log_socket.borrow(),
+                       0, pid, tid);
+    if (tag) {
+        buffer.WriteKeyValue("tag", tag);
+    }
+    buffer.FlushRecord();
+}
+}  // namespace
+
 void VulkanDevice::InitTraceProvider() {
     if (!mTraceProvider.Initialize()) {
         ALOGE("Trace provider failed to initialize");