Skip to content

Commit

Permalink
增加日志输出等级调整
Browse files Browse the repository at this point in the history
  • Loading branch information
sfxfs committed Nov 8, 2023
1 parent 252d23c commit 55c65ba
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@
## 运行

```shell
sudo ONION_LOG=(noinfo / nodebug / nocolor / syslog) UV_DEBUG=1 ./uvmaster
sudo ONION_LOG=(noinfo / nodebug / nocolor / syslog) UV_DEBUG=(error / warn / debug / info) ./uvmaster
```

2 changes: 1 addition & 1 deletion user/config/parameters/dev_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cJSON* dev_ctl_params_add_to_root(pwmDev_attr_t *params)
if (node == NULL)
return NULL;

cJSON_AddItemToObject(node, "enabled", params->enabled == true ? cJSON_CreateTrue() : cJSON_CreateFalse());
cJSON_AddBoolToObject(node, "enabled", params->enabled);
cJSON_AddNumberToObject(node, "channel", params->channel);
cJSON_AddNumberToObject(node, "pMax", params->pMax);
cJSON_AddNumberToObject(node, "nMax", params->nMax);
Expand Down
3 changes: 1 addition & 2 deletions user/config/parameters/others.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ void server_params_init(struct server_config *params)
{
if (params->port != NULL)
free(params->port);
params->port = malloc(5 * sizeof(char));
memset(params->port, '\0', 5 * sizeof(char));
params->port = calloc(5, sizeof(char));
strcpy(params->port, "8888");
params->clt_timeout = 3000;
}
Expand Down
4 changes: 2 additions & 2 deletions user/config/parameters/propeller.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ cJSON* propeller_params_add_to_root(propeller_attr_t *params)
if (node == NULL)
return NULL;

cJSON_AddItemToObject(node, "reversed", params->reversed == true ? cJSON_CreateTrue() : cJSON_CreateFalse());
cJSON_AddItemToObject(node, "enabled", params->enabled == true ? cJSON_CreateTrue() : cJSON_CreateFalse());
cJSON_AddBoolToObject(node, "reversed", params->reversed);
cJSON_AddBoolToObject(node, "enabled", params->enabled);
cJSON_AddNumberToObject(node, "channel", params->channel);
cJSON_AddNumberToObject(node, "deadzone_upper", params->deadzone_upper);
cJSON_AddNumberToObject(node, "deadzone_lower", params->deadzone_lower);
Expand Down
2 changes: 1 addition & 1 deletion user/config/parameters/rocket_ratio.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static cJSON* _rocket_dir_add_to_root(rocket_propeller_attr_t params)
if (node == NULL)
return NULL;

cJSON_AddItemToObject(node, "reversed", params.reversed == true ? cJSON_CreateTrue() : cJSON_CreateFalse());
cJSON_AddBoolToObject(node, "reversed", params.reversed);
cJSON_AddNumberToObject(node, "ratio_p", params.ratio_p);
cJSON_AddNumberToObject(node, "ratio_n", params.ratio_n);

Expand Down
4 changes: 1 addition & 3 deletions user/config/parameters/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ char *cjson_value_analysis_string(cJSON *params, const char *str)
char *temp = cJSON_GetObjectItem(params, str)->valuestring;
if (temp == NULL)
return NULL;
size_t size = (strlen(temp) + 1) * sizeof(char); // +1 for '\0'
char *ret_str = malloc(size);
memset(ret_str, '\0', size);
char *ret_str = calloc(strlen(temp) + 1, sizeof(char));
strcpy(ret_str, temp);
return ret_str;
}
22 changes: 17 additions & 5 deletions user/main.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>

extern void uvm_init(int debug_mode);
extern void uvm_init(unsigned char debug_mode);
extern void uvm_deinit(void);
extern void uvm_loop(void);

static void exit_uvm(int sig)
{
printf("info: closing uv-master app...\n");
uvm_deinit();
exit(0);
exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
if (geteuid() != 0)
{
printf("please run as root...\n");
printf("error: please run as root...\n");
exit(EXIT_SUCCESS);
}

Expand All @@ -44,14 +44,26 @@ int main(int argc, char **argv)
}
}
} else {
unsigned char debug_lvl = 0;
if (strcmp(debug_env, "debug") == 0)
debug_lvl = 4;
else if (strcmp(debug_env, "info") == 0)
debug_lvl = 3;
else if (strcmp(debug_env, "warn") == 0)
debug_lvl = 2;
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");
uvm_init(1);
uvm_init(debug_lvl);
for (;;) {
uvm_loop();
}
Expand Down
12 changes: 9 additions & 3 deletions user/main_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define LOG_TAG "uvm.main"

#include <elog.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "server/server.h"
Expand All @@ -27,12 +28,17 @@ void uvm_deinit()
* @brief uvm初始化
* @param debug_mode 调试模式
*/
void uvm_init(bool debug_mode)
void uvm_init(uint8_t debug_mode)
{
if (debug_mode == true)
if (debug_mode != false)
{
// 初始化 EasyLogger
elog_init();
if (elog_init() != ELOG_NO_ERR)
{
printf("easylogger init error\n");
exit(EXIT_FAILURE);
}
elog_set_filter_lvl(debug_mode);
/* 设置 EasyLogger 日志格式 */
elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_TIME | ELOG_FMT_LVL | ELOG_FMT_TAG);
Expand Down
70 changes: 44 additions & 26 deletions user/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ void *check_lose_thread(void *arg)
*/
static void check_lose_status(mln_event_t *ev, void *data)
{
rov_info_t *info = (rov_info_t *)data;
if (info == NULL)
if (data == NULL)
{
return;
}
rov_info_t *info = (rov_info_t *)data;

pthread_mutex_lock(&info->system.server.loss_status_mtx);
if (info->control.flag.lose_clt == false)
Expand Down Expand Up @@ -104,6 +104,32 @@ onion_connection_status strip_rpc(void *_, onion_request * req, onion_response *
return OCS_PROCESSED;
}

int loss_ctl_timer_init(struct rov_info* info)
{
pthread_mutex_init(&info->system.server.loss_status_mtx, NULL);

ev = mln_event_new();
if (ev == NULL)
{
log_w("event init failed");
return -1;
}

if (mln_event_timer_set(ev, info->system.server.config.clt_timeout, info, check_lose_status) < 0)
{
log_w("timer set failed");
return -1;
}

if (pthread_create(&info->system.server.loss_status_check_tid, NULL, check_lose_thread, NULL) != 0)
{
log_w("loss check thread start failed");
return -1;
}
pthread_detach(info->system.server.loss_status_check_tid);
return 0;
}

/**
* @brief 启动 jsonrpc 服务
* @param info 含有rov信息的结构体
Expand All @@ -115,8 +141,21 @@ int jsonrpc_server_run(struct rov_info* info)
pthread_cond_init(&info->system.server.recv_cmd_cond, NULL);

o = onion_new(O_DETACH_LISTEN | O_NO_SIGTERM);
onion_set_port(o, info->system.server.config.port);
if (o == NULL)
{
log_e("http server init failed");
return -1;
}
if (info->system.server.config.port)
onion_set_port(o, info->system.server.config.port);
else
log_w("the port of http server not set, use \"8080\"");
url = onion_root_url(o);
if (url == NULL)
{
log_e("onion set url failed");
return -1;
}
onion_url_add(url, "", strip_rpc);

info_handler_reg(info);
Expand All @@ -131,29 +170,8 @@ int jsonrpc_server_run(struct rov_info* info)
}

if (info->system.server.config.clt_timeout)
{
pthread_mutex_init(&info->system.server.loss_status_mtx, NULL);

ev = mln_event_new();
if (ev == NULL)
{
log_e("event init failed");
return -1;
}

if (mln_event_timer_set(ev, info->system.server.config.clt_timeout, info, check_lose_status) < 0)
{
log_e("timer set failed");
return -1;
}

if (pthread_create(&info->system.server.loss_status_check_tid, NULL, check_lose_thread, NULL) != 0)
{
log_e("loss check thread start failed");
return -1;
}
pthread_detach(info->system.server.loss_status_check_tid);
}
if (loss_ctl_timer_init(info) != 0)
log_w("loss ctl init failed");

return 0;
}
Expand Down

0 comments on commit 55c65ba

Please sign in to comment.