Skip to content

Commit

Permalink
优化日志系统输出
Browse files Browse the repository at this point in the history
  • Loading branch information
sfxfs committed Nov 21, 2023
1 parent 076bfc1 commit 73498b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
12 changes: 6 additions & 6 deletions protocols/rpc_cjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int rpc_add_method(rpc_handle_t *handle, jrpc_function function_pointer, char *n
if (!ptr)
{
if (handle->debug_level > 0)
printf("Cannot alloc mem for func %s\n", name);
printf("jsonrpc: Cannot alloc mem for func %s\n", name);
return -1;
}
handle->procedures = ptr;
Expand Down Expand Up @@ -99,7 +99,7 @@ int rpc_del_method(rpc_handle_t *handle, char *name)
else
{
if (handle->debug_level > 0)
printf("Method '%s' not found\n", name);
printf("jsonrpc: Method '%s' not found\n", name);
return -1;
}
return 0;
Expand Down Expand Up @@ -190,7 +190,7 @@ static cJSON *invoke_procedure(rpc_handle_t *handle, char *name, cJSON *params,
if (!procedure_found)
{
if (handle->debug_level > 0)
printf("Method not found: %s\n", name);
printf("jsonrpc: Method not found: %s\n", name);
return rpc_err(handle, JRPC_METHOD_NOT_FOUND, strdup("Method not found."), id);
}
else
Expand Down Expand Up @@ -230,7 +230,7 @@ static cJSON *rpc_invoke_method(rpc_handle_t *handle, cJSON *request)
id->valuestring)
: cJSON_CreateNumber(id->valueint);
if (handle->debug_level > 0)
printf("Method Invoked: %s\n", method->valuestring);
printf("jsonrpc: Method Invoked: %s\n", method->valuestring);
return invoke_procedure(handle, method->valuestring, params, id_copy);
}
}
Expand Down Expand Up @@ -267,13 +267,13 @@ static cJSON *rpc_invoke_method_array(rpc_handle_t *handle, cJSON *request)
char *rpc_process(rpc_handle_t *handle, const char *json_req, size_t req_len)
{
if (handle->debug_level > 1)
printf("recv json str:\n%.*s\n", req_len, json_req);
printf("jsonrpc: recv json str:\n%.*s\n", req_len, json_req);

cJSON *request = cJSON_ParseWithLength(json_req, req_len);
if (request == NULL)
{
if (handle->debug_level > 0)
printf("Valid JSON Received\n");
printf("jsonrpc: Valid JSON Received\n");
return cJSON_PrintUnformatted(rpc_err(handle, JRPC_PARSE_ERROR, strdup("Parse error: Not in JSON format."), NULL));
}

Expand Down
20 changes: 11 additions & 9 deletions user/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ int main(int argc, char **argv)
}
}
} else {
printf(" _ \n"
" /\\ /\\/\\ /\\ /\\/\\ __ _ ___| |_ ___ _ __ \n"
"/ / \\ \\ \\ / / / \\ / _` / __| __/ _ \\ '__|\n"
"\\ \\_/ /\\ V / / /\\/\\ \\ (_| \\__ \\ || __/ | \n"
" \\___/ \\_/ \\/ \\/\\__,_|___/\\__\\___|_| \n"
" \n");
printf("info: starting uv-master app...\n");
unsigned char debug_lvl = 0;
if (strcmp(debug_env, "debug") == 0)
debug_lvl = 4;
Expand All @@ -60,15 +67,10 @@ int main(int argc, char **argv)
else if (strcmp(debug_env, "error") == 0)
debug_lvl = 1;
else
printf("warn: debug level not found...\n");

printf( " _ \n"
" /\\ /\\/\\ /\\ /\\/\\ __ _ ___| |_ ___ _ __ \n"
"/ / \\ \\ \\ / / / \\ / _` / __| __/ _ \\ '__|\n"
"\\ \\_/ /\\ V / / /\\/\\ \\ (_| \\__ \\ || __/ | \n"
" \\___/ \\_/ \\/ \\/\\__,_|___/\\__\\___|_| \n"
" \n");
printf("info: starting uv-master app...\n");
{
printf("warn: debug level not found, used level \"info\" ...\n");
debug_lvl = 3;
}
uvm_init(debug_lvl);
for (;;) {
uvm_loop();
Expand Down
2 changes: 1 addition & 1 deletion user/main_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void uvm_init(uint8_t debug_level)
elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_TIME | ELOG_FMT_LVL | ELOG_FMT_TAG);
elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_TIME | ELOG_FMT_LVL | ELOG_FMT_TAG);
elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_TIME | ELOG_FMT_LVL | ELOG_FMT_TAG);
elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_TIME);
elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_DIR | ELOG_FMT_FUNC | ELOG_FMT_LINE);
elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
elog_set_text_color_enabled(true);
elog_start();
Expand Down
9 changes: 9 additions & 0 deletions user/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <pthread.h>
#include <sys/time.h>
#include <mln_event.h>
#include <onion/log.h>
#include <onion/block.h>
#include <onion/onion.h>
#include "handler/info.h"
Expand Down Expand Up @@ -164,10 +165,18 @@ int jsonrpc_server_run(struct rov_info* info, uint8_t debug_level)
}
onion_url_add_with_data(url, "", strip_rpc, info, NULL);

// 设置 onion 的日志等级
if (debug_level <= ELOG_LVL_INFO)
{
extern int onion_log_flags;
onion_log_flags = OF_INIT | OF_NOINFO;
}
// 设置 jsonrpc 的日志等级
if (debug_level == ELOG_LVL_INFO)
rpc.debug_level = 1;
if (debug_level == ELOG_LVL_DEBUG)
rpc.debug_level = 2;

info_handler_reg(info);
control_handler_reg(info);
debug_handler_reg(info);
Expand Down

0 comments on commit 73498b6

Please sign in to comment.