mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-29 18:48:09 +08:00
improve log hook
This commit is contained in:
parent
b11b58c9b5
commit
1aee4dd896
@ -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(感谢兆坤提供补丁)
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
tk_service_send_resp(&(ui->service), MSG_CODE_NOTIFY, MSG_DATA_TYPE_BINARY, RET_OK, wb);
|
||||
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);
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
* 处理服务器请求。
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -24,11 +24,11 @@
|
||||
|
||||
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);
|
||||
|
||||
/* 方便自动测试 */
|
||||
typedef void (*auto_fix_hook_t)(const char* path, const char* buff, uint32_t size);
|
||||
bool_t check_api_doc2(code_assist_t* ca, const char* filename, log_hook_t hook, void* log_ctx, bool_t auto_fix, auto_fix_hook_t fix_hook, bool_t* checked);
|
||||
|
||||
END_C_DECLS
|
||||
END_C_DECLS
|
||||
|
Loading…
Reference in New Issue
Block a user