From e402d114bfbe5a834aba063a69becf3ef1cecc61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Wed, 25 Sep 2013 11:05:58 +0800 Subject: [PATCH] remove skynet_error length limit --- skynet-src/skynet_error.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/skynet-src/skynet_error.c b/skynet-src/skynet_error.c index a4ccfa96..8e496649 100644 --- a/skynet-src/skynet_error.c +++ b/skynet-src/skynet_error.c @@ -6,8 +6,9 @@ #include #include #include +#include -#define LOG_MESSAGE_SIZE 1024 +#define LOG_MESSAGE_SIZE 256 void skynet_error(struct skynet_context * context, const char *msg, ...) { @@ -20,17 +21,31 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { } char tmp[LOG_MESSAGE_SIZE]; + char *data = NULL; va_list ap; + va_start(ap,msg); int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap); va_end(ap); - - if (len >= LOG_MESSAGE_SIZE) { - len = LOG_MESSAGE_SIZE - 1; - tmp[len] = '\0'; + if (len < LOG_MESSAGE_SIZE) { + data = strdup(tmp); + } else { + int max_size = LOG_MESSAGE_SIZE; + for (;;) { + max_size *= 2; + data = malloc(max_size); + va_start(ap,msg); + len = vsnprintf(data, max_size, msg, ap); + va_end(ap); + if (len < max_size) { + break; + } + free(data); + } } + struct skynet_message smsg; if (context == NULL) { smsg.source = 0; @@ -38,7 +53,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) { smsg.source = skynet_context_handle(context); } smsg.session = 0; - smsg.data = strdup(tmp); + smsg.data = data; smsg.sz = len | (PTYPE_TEXT << HANDLE_REMOTE_SHIFT); skynet_context_push(logger, &smsg); }