improve log hook

This commit is contained in:
lixianjing 2023-12-31 10:06:39 +08:00
parent b11b58c9b5
commit 1aee4dd896
15 changed files with 105 additions and 54 deletions

View File

@ -1,5 +1,10 @@
# 最新动态
2023/12/30
* 完善log hook方便支持多线程。
* 为了避免冲突将tkc中的log\_level\_t改为tk\_log\_level\_t
* tk\_service send\_resp 增加互斥。
2023/12/27
* 增加函数tk\_islower/tk\_isupper(感谢兆坤提供补丁)
* 增加函数slist\_find\_ex(感谢兆坤提供补丁)

View File

@ -631,7 +631,7 @@ static ret_t debugger_fscript_log_ex(void* ctx, const char* msg, bool_t native)
return RET_OK;
}
static ret_t debugger_fscript_log(void* ctx, log_level_t level, const char* msg) {
static ret_t debugger_fscript_log(void* ctx, tk_log_level_t level, const char* msg) {
return debugger_fscript_log_ex(ctx, msg, TRUE);
}

View File

@ -740,7 +740,7 @@ ret_t remote_ui_dispatch_one(remote_ui_t* ui, rbuffer_t* rb) {
return RET_OK;
}
ret_t remote_ui_hook_log(remote_ui_t* ui, tk_log_hook_t log, void* ctx) {
ret_t remote_ui_hook_log(remote_ui_t* ui, remote_ui_on_log_message_t log, void* ctx) {
return_value_if_fail(ui != NULL, RET_BAD_PARAMS);
ui->log_hook = log;
ui->log_hook_ctx = ctx;

View File

@ -33,6 +33,8 @@
#define WITH_FULL_REMOTE_UI 1
#endif/*TK_IS_PC*/
typedef ret_t (*remote_ui_on_log_message_t)(void* ctx, tk_log_level_t level, const char* msg);
BEGIN_C_DECLS
/**
@ -47,7 +49,7 @@ typedef struct _remote_ui_t {
tk_object_t* event_handlers;
darray_t pending_events;
darray_t dispatching_events;
tk_log_hook_t log_hook;
remote_ui_on_log_message_t log_hook;
void* log_hook_ctx;
} remote_ui_t;
@ -465,11 +467,11 @@ ret_t remote_ui_get_loaded_assets_info(remote_ui_t* ui, const char* file);
* @method remote_ui_hook_log
* log钩子函数
* @param {remote_ui_t*} ui remote ui客户端对象
* @param {tk_log_hook_t} log log hook
* @param {remote_ui_on_log_message_t} log log hook
* @param {void*} ctx
* @return {ret_t} RET_OK表示成功
*/
ret_t remote_ui_hook_log(remote_ui_t* ui, tk_log_hook_t log, void* ctx);
ret_t remote_ui_hook_log(remote_ui_t* ui, remote_ui_on_log_message_t log, void* ctx);
/**
* @method remote_ui_unhook_log

View File

@ -605,17 +605,26 @@ static ret_t remote_ui_dev_info_write(wbuffer_t* wb, remote_ui_dev_info_t* info)
return RET_OK;
}
static ret_t remote_ui_on_log(void* ctx, log_level_t level, const char* msg) {
static ret_t remote_ui_on_log(void* ctx, tk_log_level_t level, const char* format, va_list ap) {
remote_ui_service_t* ui = (remote_ui_service_t*)ctx;
if (ui != NULL && ui->service.destroy == (tk_service_destroy_t)remote_ui_service_destroy) {
char msg[1024] = {0};
wbuffer_t* wb = &(ui->service.wb);
tk_vsnprintf(msg, sizeof(msg)-1, format, ap);
wbuffer_rewind(wb);
wbuffer_write_string(wb, "<log>");
wbuffer_write_int32(wb, EVT_LOG_MESSAGE);
wbuffer_write_int8(wb, level);
wbuffer_write_string(wb, msg);
if (ui->dispatching) {
log_debug("ignore log message because busy\n");
} else {
tk_service_send_resp(&(ui->service), MSG_CODE_NOTIFY, MSG_DATA_TYPE_BINARY, RET_OK, wb);
}
} else {
remote_ui_service_hook_log(ui, FALSE);
}

View File

@ -32,12 +32,25 @@ ret_t tk_service_init(tk_service_t* service, tk_iostream_t* io) {
return_value_if_fail(service != NULL, RET_BAD_PARAMS);
service->io = io;
service->mutex = tk_mutex_create();
wbuffer_init_extendable(&(service->wb));
wbuffer_extend_capacity(&(service->wb), 1024);
return RET_OK;
}
ret_t tk_service_lock(tk_service_t* service) {
return_value_if_fail(service != NULL && service->mutex != NULL, RET_BAD_PARAMS);
return tk_mutex_lock(service->mutex);
}
ret_t tk_service_unlock(tk_service_t* service) {
return_value_if_fail(service != NULL && service->mutex != NULL, RET_BAD_PARAMS);
return tk_mutex_unlock(service->mutex);
}
ret_t tk_service_dispatch(tk_service_t* service) {
return_value_if_fail(service != NULL && service->dispatch != NULL, RET_BAD_PARAMS);
@ -50,6 +63,9 @@ ret_t tk_service_destroy(tk_service_t* service) {
wbuffer_deinit(&(service->wb));
TK_OBJECT_UNREF(service->io);
tk_mutex_destroy(service->mutex);
service->mutex = NULL;
return service->destroy(service);
}
@ -243,10 +259,13 @@ ret_t tk_service_send_resp(tk_service_t* service, uint32_t type, uint32_t data_t
int32_t len = 0;
int32_t retry_times = 0;
tk_msg_header_t header;
ret_t ret = RET_OK;
return_value_if_fail(service != NULL && wb != NULL, RET_BAD_PARAMS);
tk_service_lock(service);
if (service->retry_times < 1) {
return tk_service_send_resp_impl(service, type, data_type, resp_code, wb);
ret = tk_service_send_resp_impl(service, type, data_type, resp_code, wb);
goto end;
}
while (retry_times < service->retry_times) {
@ -259,7 +278,8 @@ ret_t tk_service_send_resp(tk_service_t* service, uint32_t type, uint32_t data_t
break_if_fail(len == sizeof(header));
if (header.resp_code == RET_OK) {
return RET_OK;
ret = RET_OK;
goto end;
}
if (header.resp_code == RET_CRC) {
@ -271,8 +291,12 @@ ret_t tk_service_send_resp(tk_service_t* service, uint32_t type, uint32_t data_t
break;
}
}
ret = RET_FAIL;
return RET_FAIL;
end:
tk_service_unlock(service);
return ret;
}
ret_t tk_service_read_req_impl(tk_service_t* service, tk_msg_header_t* header, wbuffer_t* wb) {

View File

@ -22,6 +22,7 @@
#ifndef TK_SERVICE_H
#define TK_SERVICE_H
#include "tkc/mutex.h"
#include "tkc/buffer.h"
#include "tkc/iostream.h"
#include "service/msg_header.h"
@ -58,6 +59,7 @@ struct _tk_service_t {
/*private*/
uint32_t retry_times;
tk_mutex_t* mutex;
tk_service_dispatch_t dispatch;
tk_service_destroy_t destroy;
};
@ -72,6 +74,24 @@ struct _tk_service_t {
*/
ret_t tk_service_init(tk_service_t* service, tk_iostream_t* io);
/**
* @method tk_service_lock
*
* @param {tk_service_t*} service
*
* @return {ret_t} RET_OK表示成功
*/
ret_t tk_service_lock(tk_service_t* service);
/**
* @method tk_service_unlock
*
* @param {tk_service_t*} service
*
* @return {ret_t} RET_OK表示成功
*/
ret_t tk_service_unlock(tk_service_t* service);
/**
* @method tk_service_dispatch
*

View File

@ -197,7 +197,7 @@ log_message_event_t* log_message_event_cast(event_t* event) {
return (log_message_event_t*)event;
}
event_t* log_message_event_init(log_message_event_t* event, log_level_t level, const char* message) {
event_t* log_message_event_init(log_message_event_t* event, tk_log_level_t level, const char* message) {
return_value_if_fail(event != NULL, NULL);
memset(event, 0x00, sizeof(*event));
event->e = event_init(EVT_LOG_MESSAGE, NULL);

View File

@ -509,11 +509,11 @@ event_t* value_change_event_init(value_change_event_t* event, uint32_t type, voi
typedef struct _log_message_event_t {
event_t e;
/**
* @property {log_level_t} level
* @property {tk_log_level_t} level
* @annotation ["readable"]
*
*/
log_level_t level;
tk_log_level_t level;
/**
* @property {const char*} message
@ -537,12 +537,12 @@ log_message_event_t* log_message_event_cast(event_t* event);
* @method log_message_event_init
*
* @param {log_message_event_t*} event event对象
* @param {log_level_t} level
* @param {tk_log_level_t} level
* @param {const char*} message
*
* @return {event_t*} event对象
*/
event_t* log_message_event_init(log_message_event_t* event, log_level_t level, const char* message);
event_t* log_message_event_init(log_message_event_t* event, tk_log_level_t level, const char* message);
END_C_DECLS

View File

@ -16,15 +16,15 @@
#include "tkc/mem.h"
#include "tkc/utils.h"
static log_level_t s_log_level = LOG_LEVEL_DEBUG;
static tk_log_level_t s_log_level = LOG_LEVEL_DEBUG;
ret_t log_set_log_level(log_level_t log_level) {
ret_t log_set_log_level(tk_log_level_t log_level) {
s_log_level = log_level;
return RET_OK;
}
log_level_t log_get_log_level(void) {
tk_log_level_t log_get_log_level(void) {
return s_log_level;
}
@ -34,23 +34,16 @@ int32_t log_dummy(const char* fmt, ...) {
#ifndef WITHOUT_FSCRIPT
#include "tkc/thread.h"
#define TK_LOG_BUFF_SIZE 255
static char* s_log_buff = NULL;
static void* s_log_hook_ctx = NULL;
static tk_log_hook_t s_log_hook = NULL;
static uint64_t s_log_hook_tid = 0;
ret_t log_notify(log_level_t level, const char* format, ...) {
ret_t log_notify(tk_log_level_t level, const char* format, ...) {
va_list va;
if (s_log_buff != NULL && s_log_hook != NULL && s_log_hook_tid == tk_thread_self()) {
if (s_log_hook != NULL) {
va_start(va, format);
*s_log_buff = '\0';
tk_vsnprintf(s_log_buff, TK_LOG_BUFF_SIZE, format, va);
s_log_hook(s_log_hook_ctx, level, format, va);
va_end(va);
s_log_hook(s_log_hook_ctx, level, s_log_buff);
}
return RET_OK;
@ -59,20 +52,11 @@ ret_t log_notify(log_level_t level, const char* format, ...) {
ret_t log_set_hook(tk_log_hook_t log, void* ctx) {
s_log_hook = log;
s_log_hook_ctx = ctx;
s_log_hook_tid = tk_thread_self();
if (log != NULL) {
if (s_log_buff == NULL) {
s_log_buff = TKMEM_ALLOC(TK_LOG_BUFF_SIZE + 1);
}
} else {
TKMEM_FREE(s_log_buff);
}
return RET_OK;
}
#else
ret_t log_notify(log_level_t level, const char* format, ...) {
ret_t log_notify(tk_log_level_t level, const char* format, ...) {
return RET_OK;
}
ret_t log_set_hook(tk_log_hook_t log, void* ctx) {

View File

@ -25,11 +25,11 @@
BEGIN_C_DECLS
/**
* @enum log_level_t
* @enum tk_log_level_t
* @prefix LOG_LEVEL_
* LOG的级别
*/
typedef enum _log_level_t {
typedef enum _tk_log_level_t {
/**
* @const LOG_LEVEL_DEBUG
* DEBUG
@ -50,7 +50,7 @@ typedef enum _log_level_t {
* ERROR
*/
LOG_LEVEL_ERROR
} log_level_t;
} tk_log_level_t;
/**
* @class log_t
@ -64,20 +64,20 @@ typedef enum _log_level_t {
*
* log的级别
*
* @return {log_level_t} log的级别
* @return {tk_log_level_t} log的级别
*/
log_level_t log_get_log_level(void);
tk_log_level_t log_get_log_level(void);
/**
* @method log_set_log_level
*
* log的级别
*
* @param {log_level_t} log_level log的级别
* @param {tk_log_level_t} log_level log的级别
*
* @return {ret_t} RET_OK表示成功
*/
ret_t log_set_log_level(log_level_t log_level);
ret_t log_set_log_level(tk_log_level_t log_level);
int32_t log_dummy(const char* fmt, ...);
@ -146,14 +146,14 @@ int32_t log_dummy(const char* fmt, ...);
*
* >
*
* @param {log_level_t} level
* @param {tk_log_level_t} level
* @param {const char*} format
*
* @return {ret_t} RET_OK表示成功
*/
ret_t log_notify(log_level_t level, const char* format, ...);
ret_t log_notify(tk_log_level_t level, const char* format, ...);
typedef ret_t (*tk_log_hook_t)(void* ctx, log_level_t level, const char* msg);
typedef ret_t (*tk_log_hook_t)(void* ctx, tk_log_level_t level, const char * format, va_list ap);
/**
* @method log_set_hook

View File

@ -1,9 +1,13 @@
#include "tkc/str.h"
#include "tkc/log.h"
#include "tkc/utils.h"
#include "gtest/gtest.h"
static ret_t debugger_log(void* ctx, log_level_t level, const char* msg) {
static ret_t debugger_log(void* ctx, log_level_t level, const char* format, va_list ap) {
str_t* str = (str_t*)ctx;
char msg[1024] = {0};
tk_vsnprintf(msg, sizeof(msg)-1, format, ap);
str_append_more(str, msg, ";", NULL);
return RET_OK;

View File

@ -9,7 +9,10 @@ using namespace std;
static string s_log;
static ret_t debugger_log(void* ctx, log_level_t level, const char* msg) {
static ret_t debugger_log(void* ctx, log_level_t level, const char* format, va_list ap) {
char msg[1024] = {0};
tk_vsnprintf(msg, sizeof(msg)-1, format, ap);
s_log += msg;
return RET_OK;
}

View File

@ -107,7 +107,7 @@ static inline bool_t is_space(char c) { return c == ' ' || c == '\t'; }
LOG_LEVEL_WARN
LOG_LEVEL_ERROR
*/
static void default_log_hook(void* ctx, log_level_t level, const char* s) {
static void default_log_hook(void* ctx, tk_log_level_t level, const char* s) {
if (level == LOG_LEVEL_ERROR) {
log_error("%s", s);
} else if (level == LOG_LEVEL_WARN) {
@ -117,7 +117,7 @@ static void default_log_hook(void* ctx, log_level_t level, const char* s) {
}
}
static void app_log(check_ctx_t* ctx, log_level_t level, const char* fmt, ...) {
static void app_log(check_ctx_t* ctx, tk_log_level_t level, const char* fmt, ...) {
va_list va;
char buf[2048+1] = {0};
return_if_fail(fmt != NULL);

View File

@ -24,7 +24,7 @@
BEGIN_C_DECLS
typedef void (*log_hook_t)(void* ctx, log_level_t level, const char* s);
typedef void (*log_hook_t)(void* ctx, tk_log_level_t level, const char* s);
bool_t check_api_doc(code_assist_t* ca, const char* filename, log_hook_t hook, void* log_ctx, bool_t auto_fix, bool_t* checked);
/* 方便自动测试 */