mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
lua-skynet: lerror 格式符使用 "%*s", 节省一次 strlen (#2051)
* lua-skynet: lerror 格式符使用 "%*s", 节省一次 strlen * make skynet_strdup/skynet_strndup static, remove skynet_asprintf/skynet_vasprintf --------- Co-authored-by: efve.zff <efve.zff@alibaba-inc.com>
This commit is contained in:
@@ -340,8 +340,9 @@ lerror(lua_State *L) {
|
|||||||
int n = lua_gettop(L);
|
int n = lua_gettop(L);
|
||||||
if (n <= 1) {
|
if (n <= 1) {
|
||||||
lua_settop(L, 1);
|
lua_settop(L, 1);
|
||||||
const char * s = luaL_tolstring(L, 1, NULL);
|
size_t len;
|
||||||
skynet_error(context, "%s", s);
|
const char *s = luaL_tolstring(L, 1, &len);
|
||||||
|
skynet_error(context, "%*s", (int)len, s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
@@ -355,7 +356,9 @@ lerror(lua_State *L) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
luaL_pushresult(&b);
|
luaL_pushresult(&b);
|
||||||
skynet_error(context, "%s", lua_tostring(L, -1));
|
size_t len;
|
||||||
|
const char *s = luaL_tolstring(L, -1, &len);
|
||||||
|
skynet_error(context, "%*s", (int)len, s);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -323,14 +323,6 @@ dump_c_mem() {
|
|||||||
skynet_error(NULL, "+total: %zdkb",total >> 10);
|
skynet_error(NULL, "+total: %zdkb",total >> 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
|
||||||
skynet_strdup(const char *str) {
|
|
||||||
size_t sz = strlen(str);
|
|
||||||
char * ret = skynet_malloc(sz+1);
|
|
||||||
memcpy(ret, str, sz+1);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
void *
|
||||||
skynet_lalloc(void *ptr, size_t osize, size_t nsize) {
|
skynet_lalloc(void *ptr, size_t osize, size_t nsize) {
|
||||||
if (nsize == 0) {
|
if (nsize == 0) {
|
||||||
|
|||||||
@@ -1,15 +1,34 @@
|
|||||||
#include "skynet.h"
|
#include "skynet.h"
|
||||||
#include "skynet_handle.h"
|
#include "skynet_handle.h"
|
||||||
|
#include "skynet_imp.h"
|
||||||
#include "skynet_mq.h"
|
#include "skynet_mq.h"
|
||||||
#include "skynet_server.h"
|
#include "skynet_server.h"
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define LOG_MESSAGE_SIZE 256
|
#define LOG_MESSAGE_SIZE 256
|
||||||
|
|
||||||
|
static int
|
||||||
|
log_try_vasprintf(char **strp, const char *fmt, va_list ap) {
|
||||||
|
if (strcmp(fmt, "%*s") == 0) {
|
||||||
|
// for `lerror` in lua-skynet.c
|
||||||
|
const int len = va_arg(ap, int);
|
||||||
|
const char *tmp = va_arg(ap, const char*);
|
||||||
|
*strp = skynet_strndup(tmp, len);
|
||||||
|
return *strp != NULL ? len : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char tmp[LOG_MESSAGE_SIZE];
|
||||||
|
int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, fmt, ap);
|
||||||
|
if (len >= 0 && len < LOG_MESSAGE_SIZE) {
|
||||||
|
*strp = skynet_strndup(tmp, len);
|
||||||
|
if (*strp == NULL) return -1;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_error(struct skynet_context * context, const char *msg, ...) {
|
skynet_error(struct skynet_context * context, const char *msg, ...) {
|
||||||
static uint32_t logger = 0;
|
static uint32_t logger = 0;
|
||||||
@@ -20,36 +39,29 @@ skynet_error(struct skynet_context * context, const char *msg, ...) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char tmp[LOG_MESSAGE_SIZE];
|
|
||||||
char *data = NULL;
|
char *data = NULL;
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
va_start(ap,msg);
|
va_start(ap, msg);
|
||||||
int len = vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap);
|
int len = log_try_vasprintf(&data, msg, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
if (len >=0 && len < LOG_MESSAGE_SIZE) {
|
|
||||||
data = skynet_strdup(tmp);
|
|
||||||
} else {
|
|
||||||
int max_size = LOG_MESSAGE_SIZE;
|
|
||||||
for (;;) {
|
|
||||||
max_size *= 2;
|
|
||||||
data = skynet_malloc(max_size);
|
|
||||||
va_start(ap,msg);
|
|
||||||
len = vsnprintf(data, max_size, msg, ap);
|
|
||||||
va_end(ap);
|
|
||||||
if (len < max_size) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
skynet_free(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
skynet_free(data);
|
perror("vasprintf error :");
|
||||||
perror("vsnprintf error :");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (data == NULL) { // unlikely
|
||||||
|
data = skynet_malloc(len + 1);
|
||||||
|
va_start(ap, msg);
|
||||||
|
len = vsnprintf(data, len + 1, msg, ap);
|
||||||
|
va_end(ap);
|
||||||
|
if (len < 0) {
|
||||||
|
skynet_free(data);
|
||||||
|
perror("vsnprintf error :");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct skynet_message smsg;
|
struct skynet_message smsg;
|
||||||
if (context == NULL) {
|
if (context == NULL) {
|
||||||
@@ -62,4 +74,3 @@ skynet_error(struct skynet_context * context, const char *msg, ...) {
|
|||||||
smsg.sz = len | ((size_t)PTYPE_TEXT << MESSAGE_TYPE_SHIFT);
|
smsg.sz = len | ((size_t)PTYPE_TEXT << MESSAGE_TYPE_SHIFT);
|
||||||
skynet_context_push(logger, &smsg);
|
skynet_context_push(logger, &smsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "skynet.h"
|
#include "skynet.h"
|
||||||
|
|
||||||
#include "skynet_handle.h"
|
#include "skynet_handle.h"
|
||||||
|
#include "skynet_imp.h"
|
||||||
#include "skynet_server.h"
|
#include "skynet_server.h"
|
||||||
#include "rwlock.h"
|
#include "rwlock.h"
|
||||||
|
|
||||||
@@ -265,4 +266,3 @@ skynet_handle_init(int harbor) {
|
|||||||
|
|
||||||
// Don't need to free H
|
// Don't need to free H
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef SKYNET_IMP_H
|
#ifndef SKYNET_IMP_H
|
||||||
#define SKYNET_IMP_H
|
#define SKYNET_IMP_H
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
struct skynet_config {
|
struct skynet_config {
|
||||||
int thread;
|
int thread;
|
||||||
int harbor;
|
int harbor;
|
||||||
@@ -20,4 +22,19 @@ struct skynet_config {
|
|||||||
|
|
||||||
void skynet_start(struct skynet_config * config);
|
void skynet_start(struct skynet_config * config);
|
||||||
|
|
||||||
|
static inline char *
|
||||||
|
skynet_strndup(const char *str, size_t size) {
|
||||||
|
char * ret = skynet_malloc(size+1);
|
||||||
|
if (ret == NULL) return NULL;
|
||||||
|
memcpy(ret, str, size);
|
||||||
|
ret[size] = '\0';
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline char *
|
||||||
|
skynet_strdup(const char *str) {
|
||||||
|
size_t sz = strlen(str);
|
||||||
|
return skynet_strndup(str, sz);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ void * skynet_malloc(size_t sz);
|
|||||||
void * skynet_calloc(size_t nmemb,size_t size);
|
void * skynet_calloc(size_t nmemb,size_t size);
|
||||||
void * skynet_realloc(void *ptr, size_t size);
|
void * skynet_realloc(void *ptr, size_t size);
|
||||||
void skynet_free(void *ptr);
|
void skynet_free(void *ptr);
|
||||||
char * skynet_strdup(const char *str);
|
|
||||||
void * skynet_lalloc(void *ptr, size_t osize, size_t nsize); // use for lua
|
void * skynet_lalloc(void *ptr, size_t osize, size_t nsize); // use for lua
|
||||||
void * skynet_memalign(size_t alignment, size_t size);
|
void * skynet_memalign(size_t alignment, size_t size);
|
||||||
void * skynet_aligned_alloc(size_t alignment, size_t size);
|
void * skynet_aligned_alloc(size_t alignment, size_t size);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "skynet.h"
|
#include "skynet.h"
|
||||||
|
|
||||||
|
#include "skynet_imp.h"
|
||||||
#include "skynet_module.h"
|
#include "skynet_module.h"
|
||||||
#include "spinlock.h"
|
#include "spinlock.h"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user