Add config load error log
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#define DEFAULT_DNS_CACHE_SIZE 512
|
||||
|
||||
@@ -707,6 +708,21 @@ struct config_item config_item[] = {
|
||||
CONF_END(),
|
||||
};
|
||||
|
||||
int conf_printf(const char *file, int lineno, int ret)
|
||||
{
|
||||
if (ret == CONF_RET_ERR) {
|
||||
tlog(TLOG_ERROR, "process config file '%s' failed at line %d.", file, lineno);
|
||||
syslog(LOG_NOTICE, "process config file '%s' failed at line %d.", file, lineno);
|
||||
return -1;
|
||||
} else if (ret == CONF_RET_WARN) {
|
||||
tlog(TLOG_WARN, "process config file '%s' failed at line %d.", file, lineno);
|
||||
syslog(LOG_NOTICE, "process config file '%s' failed at line %d.", file, lineno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_addtional_file(void *data, int argc, char *argv[])
|
||||
{
|
||||
char *file_path = argv[1];
|
||||
@@ -716,7 +732,7 @@ int config_addtional_file(void *data, int argc, char *argv[])
|
||||
return 0;
|
||||
}
|
||||
|
||||
return load_conf(file_path, config_item);
|
||||
return load_conf(file_path, config_item, conf_printf);
|
||||
}
|
||||
|
||||
int _dns_server_load_conf_init(void)
|
||||
@@ -741,7 +757,10 @@ void dns_server_load_exit(void)
|
||||
|
||||
int dns_server_load_conf(const char *file)
|
||||
{
|
||||
int ret = 0;
|
||||
_dns_server_load_conf_init();
|
||||
|
||||
return load_conf(file, config_item);
|
||||
openlog ("smartdns", LOG_CONS | LOG_NDELAY, LOG_LOCAL1);
|
||||
ret = load_conf(file, config_item, conf_printf);
|
||||
closelog();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
#define CONF_INT_MAX (~(1 << 31))
|
||||
#define CONF_INT_MIN (1 << 31)
|
||||
|
||||
#define CONF_RET_OK 0
|
||||
#define CONF_RET_ERR -1
|
||||
#define CONF_RET_WARN -2
|
||||
#define CONF_RET_NOENT -3
|
||||
|
||||
struct config_item {
|
||||
const char *item;
|
||||
int (*item_func)(const char *item, void *data, int argc, char *argv[]);
|
||||
@@ -107,9 +112,9 @@ extern int conf_size(const char *item, void *data, int argc, char *argv[]);
|
||||
*
|
||||
*/
|
||||
|
||||
int load_conf(const char *file, struct config_item items[]);
|
||||
typedef int(conf_error_handler)(const char *file, int lineno, int ret);
|
||||
|
||||
int load_conf_get_line_count(void);
|
||||
int load_conf(const char *file, struct config_item items[], conf_error_handler handler);
|
||||
|
||||
void load_exit(void);
|
||||
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
__thread int line_no;
|
||||
|
||||
int conf_custom(const char *item, void *data, int argc, char *argv[])
|
||||
{
|
||||
struct config_item_custom *item_custom = data;
|
||||
@@ -182,12 +180,21 @@ void load_exit(void)
|
||||
return;
|
||||
}
|
||||
|
||||
int load_conf_get_line_count(void)
|
||||
int load_conf_printf(const char *file, int lineno, int ret)
|
||||
{
|
||||
return line_no;
|
||||
if (ret != CONF_RET_OK) {
|
||||
printf("process config file '%s' failed at line %d.", file, lineno);
|
||||
if (ret == CONF_RET_ERR || ret == CONF_RET_NOENT) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int load_conf_file(const char *file, struct config_item *items)
|
||||
int load_conf_file(const char *file, struct config_item *items, conf_error_handler handler)
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
char line[MAX_LINE_LEN];
|
||||
@@ -197,6 +204,13 @@ int load_conf_file(const char *file, struct config_item *items)
|
||||
int i;
|
||||
int argc;
|
||||
char *argv[1024];
|
||||
int ret = 0;
|
||||
int call_ret = 0;
|
||||
int line_no = 0;
|
||||
|
||||
if (handler == NULL) {
|
||||
handler = load_conf_printf;
|
||||
}
|
||||
|
||||
fp = fopen(file, "r");
|
||||
if (fp == NULL) {
|
||||
@@ -223,6 +237,7 @@ int load_conf_file(const char *file, struct config_item *items)
|
||||
|
||||
for (i = 0;; i++) {
|
||||
if (items[i].item == NULL) {
|
||||
handler(file, line_no, CONF_RET_NOENT);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -236,7 +251,9 @@ int load_conf_file(const char *file, struct config_item *items)
|
||||
|
||||
conf_getopt_reset();
|
||||
/* call item function */
|
||||
if (items[i].item_func(items[i].item, items[i].data, argc, argv) != 0) {
|
||||
call_ret = items[i].item_func(items[i].item, items[i].data, argc, argv);
|
||||
ret = handler(file, line_no, call_ret);
|
||||
if (ret != 0) {
|
||||
conf_getopt_reset();
|
||||
goto errout;
|
||||
}
|
||||
@@ -257,7 +274,7 @@ errout:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int load_conf(const char *file, struct config_item items[])
|
||||
int load_conf(const char *file, struct config_item items[], conf_error_handler handler)
|
||||
{
|
||||
return load_conf_file(file, items);
|
||||
return load_conf_file(file, items, handler);
|
||||
}
|
||||
|
||||
@@ -384,6 +384,8 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
if (dns_server_load_conf(config_file) != 0) {
|
||||
fprintf(stderr, "load config failed.\n");
|
||||
goto errout;
|
||||
}
|
||||
|
||||
if (create_pid_file(pid_file) != 0) {
|
||||
|
||||
Reference in New Issue
Block a user