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]
output = file # stderr, file
output = "stderr" # stderr, file
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]
[packet_io]
mode = dumpfile # dumpfile, dumpfilelist, marsio
app_symbol = stellar
dev_symbol = nf_0_fw
mode = "dumpfile" # dumpfile, dumpfilelist, marsio
app_symbol = "stellar"
dev_symbol = "nf_0_fw"
dumpfile_path = "/tmp/dumpfile/dumpfile.pcap"
#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
static int parse_packet_io_section(toml_table_t *root, struct packet_io_options *opts)
{
char *temp;
const char *ptr;
toml_table_t *table;
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");
if (ptr == NULL)
temp = NULL;
if (ptr == NULL || toml_rtos(ptr, &temp) != 0)
{
CONFIG_LOG_ERROR("config file missing packet_io->mode");
return -1;
}
if (strcmp(ptr, "dumpfile") == 0)
if (strcmp(temp, "dumpfile") == 0)
{
opts->mode = PACKET_IO_DUMPFILE;
}
else if (strcmp(ptr, "dumpfilelist") == 0)
else if (strcmp(temp, "dumpfilelist") == 0)
{
opts->mode = PACKET_IO_DUMPFILELIST;
}
else if (strcmp(ptr, "marsio") == 0)
else if (strcmp(temp, "marsio") == 0)
{
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)
{
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");
return -1;
}
// skip ""
strncpy(opts->dumpfile_path, ptr + 1, strlen(ptr) - 2);
strcpy(opts->dumpfile_path, temp);
}
else
{
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");
return -1;
}
strncpy(opts->app_symbol, ptr, sizeof(opts->app_symbol) - 1);
strcpy(opts->app_symbol, temp);
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");
return -1;
}
strncpy(opts->dev_symbol, ptr, sizeof(opts->dev_symbol) - 1);
strcpy(opts->dev_symbol, temp);
}
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;
FILE *fp = NULL;
char errbuf[200];
char *temp;
const char *ptr;
toml_table_t *log_section = NULL;
toml_table_t *conf_table = NULL;
@@ -126,16 +127,17 @@ static int parse_config(struct log_config *config, const char *cfg_file)
// 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);
goto error_out;
}
if (strcasecmp(ptr, "stderr") == 0)
if (strcasecmp(temp, "stderr") == 0)
{
config->output = LOG_OUTPUT_STDERR;
}
else if (strcasecmp(ptr, "file") == 0)
else if (strcasecmp(temp, "file") == 0)
{
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)
{
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);
goto error_out;
}
// skip ""
strncpy(config->log_file, ptr + 1, strlen(ptr) - 2);
strcpy(config->log_file, temp);
}
// 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);
goto error_out;
}
config->level = check_level(ptr);
config->level = check_level(temp);
if (config->level == LOG_NONE)
{
fprintf(stderr, "config file %s invalid log.level\n", cfg_file);

View File

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

View File

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

View File

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

View File

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