From dcbe205df532573f2b1f65024774788ce5dc01f9 Mon Sep 17 00:00:00 2001 From: Yiwei Zhang Date: Thu, 16 Dec 2021 06:30:51 +0000 Subject: [PATCH] proxy: add log prefix and fix line break Signed-off-by: Yiwei Zhang Reviewed-by: Chia-I Wu --- src/proxy/proxy_common.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/proxy/proxy_common.c b/src/proxy/proxy_common.c index 4a7c6a2..ca409e9 100644 --- a/src/proxy/proxy_common.c +++ b/src/proxy/proxy_common.c @@ -13,8 +13,35 @@ struct proxy_renderer proxy_renderer; void proxy_log(const char *fmt, ...) { + const char prefix[] = "proxy: "; + char line[1024]; + size_t len; va_list va; + int ret; + + len = ARRAY_SIZE(prefix) - 1; + memcpy(line, prefix, len); + va_start(va, fmt); - virgl_logv(fmt, va); + ret = vsnprintf(line + len, ARRAY_SIZE(line) - len, fmt, va); va_end(va); + + if (ret < 0) { + const char log_error[] = "log error"; + memcpy(line + len, log_error, ARRAY_SIZE(log_error) - 1); + len += ARRAY_SIZE(log_error) - 1; + } else if ((size_t)ret < ARRAY_SIZE(line) - len) { + len += ret; + } else { + len = ARRAY_SIZE(line) - 1; + } + + /* make room for newline */ + if (len + 1 >= ARRAY_SIZE(line)) + len--; + + line[len++] = '\n'; + line[len] = '\0'; + + virgl_log(line); }