enhance: toml format

This commit is contained in:
luwenpeng
2024-08-20 19:20:24 +08:00
parent f061f9abc1
commit b46a5d4b23
8 changed files with 43 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
[log] [log]
output = file # stderr, file output = "stderr" # stderr, file
file = "log/stellar.log" file = "log/stellar.log"
level = FATAL # TRACE, DEBUG, INFO, WARN, ERROR, FATAL level = "ERROR" # TRACE, DEBUG, INFO, WARN, ERROR, FATAL

View File

@@ -3,9 +3,9 @@ snowflake_base = 1 # [0, 31]
snowflake_offset = 2 # [0, 127] snowflake_offset = 2 # [0, 127]
[packet_io] [packet_io]
mode = dumpfile # dumpfile, dumpfilelist, marsio mode = "dumpfile" # dumpfile, dumpfilelist, marsio
app_symbol = stellar app_symbol = "stellar"
dev_symbol = nf_0_fw dev_symbol = "nf_0_fw"
dumpfile_path = "/tmp/dumpfile/dumpfile.pcap" dumpfile_path = "/tmp/dumpfile/dumpfile.pcap"
#dumpfile_path = "/tmp/dumpfile/dumpfilelist" #dumpfile_path = "/tmp/dumpfile/dumpfilelist"

View File

@@ -46,6 +46,7 @@ static int parse_snowflake_section(toml_table_t *root, struct snowflake_options
// retuun -1: failed // retuun -1: failed
static int parse_packet_io_section(toml_table_t *root, struct packet_io_options *opts) static int parse_packet_io_section(toml_table_t *root, struct packet_io_options *opts)
{ {
char *temp;
const char *ptr; const char *ptr;
toml_table_t *table; toml_table_t *table;
toml_array_t *mask_array; toml_array_t *mask_array;
@@ -58,20 +59,21 @@ static int parse_packet_io_section(toml_table_t *root, struct packet_io_options
} }
ptr = toml_raw_in(table, "mode"); ptr = toml_raw_in(table, "mode");
if (ptr == NULL) temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->mode"); CONFIG_LOG_ERROR("config file missing packet_io->mode");
return -1; return -1;
} }
if (strcmp(ptr, "dumpfile") == 0) if (strcmp(temp, "dumpfile") == 0)
{ {
opts->mode = PACKET_IO_DUMPFILE; opts->mode = PACKET_IO_DUMPFILE;
} }
else if (strcmp(ptr, "dumpfilelist") == 0) else if (strcmp(temp, "dumpfilelist") == 0)
{ {
opts->mode = PACKET_IO_DUMPFILELIST; opts->mode = PACKET_IO_DUMPFILELIST;
} }
else if (strcmp(ptr, "marsio") == 0) else if (strcmp(temp, "marsio") == 0)
{ {
opts->mode = PACKET_IO_MARSIO; opts->mode = PACKET_IO_MARSIO;
} }
@@ -84,31 +86,33 @@ static int parse_packet_io_section(toml_table_t *root, struct packet_io_options
if (opts->mode == PACKET_IO_DUMPFILE || opts->mode == PACKET_IO_DUMPFILELIST) if (opts->mode == PACKET_IO_DUMPFILE || opts->mode == PACKET_IO_DUMPFILELIST)
{ {
ptr = toml_raw_in(table, "dumpfile_path"); ptr = toml_raw_in(table, "dumpfile_path");
if (ptr == NULL) temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->dumpfile_path"); CONFIG_LOG_ERROR("config file missing packet_io->dumpfile_path");
return -1; return -1;
} }
// skip "" strcpy(opts->dumpfile_path, temp);
strncpy(opts->dumpfile_path, ptr + 1, strlen(ptr) - 2);
} }
else else
{ {
ptr = toml_raw_in(table, "app_symbol"); ptr = toml_raw_in(table, "app_symbol");
if (ptr == NULL) temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->app_symbol"); CONFIG_LOG_ERROR("config file missing packet_io->app_symbol");
return -1; return -1;
} }
strncpy(opts->app_symbol, ptr, sizeof(opts->app_symbol) - 1); strcpy(opts->app_symbol, temp);
ptr = toml_raw_in(table, "dev_symbol"); ptr = toml_raw_in(table, "dev_symbol");
if (ptr == NULL) temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
CONFIG_LOG_ERROR("config file missing packet_io->dev_symbol"); CONFIG_LOG_ERROR("config file missing packet_io->dev_symbol");
return -1; return -1;
} }
strncpy(opts->dev_symbol, ptr, sizeof(opts->dev_symbol) - 1); strcpy(opts->dev_symbol, temp);
} }
ptr = toml_raw_in(table, "nr_threads"); ptr = toml_raw_in(table, "nr_threads");

View File

@@ -99,6 +99,7 @@ static int parse_config(struct log_config *config, const char *cfg_file)
int ret = -1; int ret = -1;
FILE *fp = NULL; FILE *fp = NULL;
char errbuf[200]; char errbuf[200];
char *temp;
const char *ptr; const char *ptr;
toml_table_t *log_section = NULL; toml_table_t *log_section = NULL;
toml_table_t *conf_table = NULL; toml_table_t *conf_table = NULL;
@@ -126,16 +127,17 @@ static int parse_config(struct log_config *config, const char *cfg_file)
// output // output
ptr = toml_raw_in(log_section, "output"); ptr = toml_raw_in(log_section, "output");
if (ptr == NULL) temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
fprintf(stderr, "config file %s missing log.output\n", cfg_file); fprintf(stderr, "config file %s missing log.output\n", cfg_file);
goto error_out; goto error_out;
} }
if (strcasecmp(ptr, "stderr") == 0) if (strcasecmp(temp, "stderr") == 0)
{ {
config->output = LOG_OUTPUT_STDERR; config->output = LOG_OUTPUT_STDERR;
} }
else if (strcasecmp(ptr, "file") == 0) else if (strcasecmp(temp, "file") == 0)
{ {
config->output = LOG_OUTPUT_FILE; config->output = LOG_OUTPUT_FILE;
} }
@@ -149,23 +151,24 @@ static int parse_config(struct log_config *config, const char *cfg_file)
if (config->output == LOG_OUTPUT_FILE) if (config->output == LOG_OUTPUT_FILE)
{ {
ptr = toml_raw_in(log_section, "file"); ptr = toml_raw_in(log_section, "file");
if (ptr == NULL) temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
fprintf(stderr, "config file %s missing log.file\n", cfg_file); fprintf(stderr, "config file %s missing log.file\n", cfg_file);
goto error_out; goto error_out;
} }
// skip "" strcpy(config->log_file, temp);
strncpy(config->log_file, ptr + 1, strlen(ptr) - 2);
} }
// level // level
ptr = toml_raw_in(log_section, "level"); ptr = toml_raw_in(log_section, "level");
if (ptr == NULL) temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{ {
fprintf(stderr, "config file %s missing log.level\n", cfg_file); fprintf(stderr, "config file %s missing log.level\n", cfg_file);
goto error_out; goto error_out;
} }
config->level = check_level(ptr); config->level = check_level(temp);
if (config->level == LOG_NONE) if (config->level == LOG_NONE)
{ {
fprintf(stderr, "config file %s invalid log.level\n", cfg_file); fprintf(stderr, "config file %s invalid log.level\n", cfg_file);

View File

@@ -1,4 +1,4 @@
[log] [log]
output = file # stderr, file output = "file" # stderr, file
file = stellar.log file = "stellar.log"
level = DEBUG # TRACE, DEBUG, INFO, WARN, ERROR, FATAL level = "DEBUG" # TRACE, DEBUG, INFO, WARN, ERROR, FATAL

View File

@@ -1,4 +1,4 @@
[log] [log]
output = stderr # stderr, file output = "stderr" # stderr, file
file = stellar.log file = "stellar.log"
level = DEBUG # TRACE, DEBUG, INFO, WARN, ERROR, FATAL level = "DEBUG" # TRACE, DEBUG, INFO, WARN, ERROR, FATAL

View File

@@ -1,4 +1,4 @@
[log] [log]
output = stderr # stderr, file output = "stderr" # stderr, file
level = DEBUG # TRACE, DEBUG, INFO, WARN, ERROR, FATAL level = "DEBUG" # TRACE, DEBUG, INFO, WARN, ERROR, FATAL
file = "log/stellar.log" file = "log/stellar.log"

View File

@@ -3,9 +3,9 @@ snowflake_base = 1 # [0, 31]
snowflake_offset = 2 # [0, 127] snowflake_offset = 2 # [0, 127]
[packet_io] [packet_io]
mode = marsio # dumpfile, dumpfilelist, marsio mode = "dumpfile" # dumpfile, dumpfilelist, marsio
app_symbol = stellar app_symbol = "stellar"
dev_symbol = nf_0_fw dev_symbol = "nf_0_fw"
dumpfile_path = "/tmp/dumpfile/dumpfile.pcap" dumpfile_path = "/tmp/dumpfile/dumpfile.pcap"
#dumpfile_path = "/tmp/dumpfile/dumpfilelist" #dumpfile_path = "/tmp/dumpfile/dumpfilelist"