log: output error to syslog when load config file.
This commit is contained in:
@@ -683,8 +683,6 @@ static int _config_server(int argc, char *argv[], dns_server_type_t type, int de
|
||||
default:
|
||||
tlog(TLOG_WARN, "unknown server option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
syslog(LOG_WARNING, "unknown server option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2225,8 +2223,6 @@ static int _config_bind_ip(int argc, char *argv[], DNS_BIND_TYPE type)
|
||||
default:
|
||||
tlog(TLOG_WARN, "unknown bind option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
syslog(LOG_WARNING, "unknown bind option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -3275,8 +3271,6 @@ static int _conf_ip_rules(void *data, int argc, char *argv[])
|
||||
default:
|
||||
tlog(TLOG_WARN, "unknown ip-rules option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
syslog(LOG_WARNING, "unknown ip-rules option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -3683,8 +3677,6 @@ static int _conf_domain_rules(void *data, int argc, char *argv[])
|
||||
default:
|
||||
tlog(TLOG_WARN, "unknown domain-rules option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
syslog(LOG_WARNING, "unknown domain-rules option: %s at '%s:%d'.", argv[optind - 1], conf_get_conf_file(),
|
||||
conf_get_current_lineno());
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -4226,12 +4218,10 @@ static int _conf_printf(const char *file, int lineno, int ret)
|
||||
case CONF_RET_WARN:
|
||||
case CONF_RET_BADCONF:
|
||||
tlog(TLOG_WARN, "process config failed at '%s:%d'.", file, lineno);
|
||||
syslog(LOG_WARNING, "process config failed at '%s:%d'.", file, lineno);
|
||||
return -1;
|
||||
break;
|
||||
case CONF_RET_NOENT:
|
||||
tlog(TLOG_WARN, "unsupported config at '%s:%d'.", file, lineno);
|
||||
syslog(LOG_WARNING, "unsupported config at '%s:%d'.", file, lineno);
|
||||
return -1;
|
||||
break;
|
||||
default:
|
||||
@@ -4270,7 +4260,6 @@ int config_additional_file(void *data, int argc, char *argv[])
|
||||
|
||||
if (access(file_path, R_OK) != 0) {
|
||||
tlog(TLOG_WARN, "config file '%s' is not readable, %s", conf_file, strerror(errno));
|
||||
syslog(LOG_WARNING, "config file '%s' is not readable, %s", conf_file, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@
|
||||
#include <sys/resource.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <syslog.h>
|
||||
#include <ucontext.h>
|
||||
|
||||
#define MAX_KEY_LEN 64
|
||||
@@ -725,6 +726,51 @@ static int _smartdns_init_pre(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _smartdns_early_log(struct tlog_loginfo *loginfo, const char *format, va_list ap)
|
||||
{
|
||||
char log_buf[TLOG_MAX_LINE_LEN];
|
||||
int sys_log_level = LOG_INFO;
|
||||
|
||||
if (loginfo->level < TLOG_WARN) {
|
||||
return;
|
||||
}
|
||||
|
||||
int len = vsnprintf(log_buf, sizeof(log_buf), format, ap);
|
||||
if (len <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (log_buf[len - 1] != '\n') {
|
||||
log_buf[len] = '\n';
|
||||
len++;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s", log_buf);
|
||||
|
||||
switch (loginfo->level) {
|
||||
case TLOG_ERROR:
|
||||
sys_log_level = LOG_ERR;
|
||||
break;
|
||||
case TLOG_WARN:
|
||||
sys_log_level = LOG_WARNING;
|
||||
break;
|
||||
case TLOG_NOTICE:
|
||||
sys_log_level = LOG_NOTICE;
|
||||
break;
|
||||
case TLOG_INFO:
|
||||
sys_log_level = LOG_INFO;
|
||||
break;
|
||||
case TLOG_DEBUG:
|
||||
sys_log_level = LOG_DEBUG;
|
||||
break;
|
||||
default:
|
||||
sys_log_level = LOG_INFO;
|
||||
break;
|
||||
}
|
||||
|
||||
syslog(sys_log_level, "%s", log_buf);
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
|
||||
static smartdns_post_func _smartdns_post = NULL;
|
||||
@@ -832,6 +878,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
srand(time(NULL));
|
||||
|
||||
tlog_reg_early_printf_callback(_smartdns_early_log);
|
||||
|
||||
ret = dns_server_load_conf(config_file);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "load config failed.\n");
|
||||
|
||||
33
src/tlog.c
33
src/tlog.c
@@ -148,6 +148,7 @@ static struct tlog tlog;
|
||||
static int tlog_disable_early_print = 0;
|
||||
static tlog_level tlog_set_level = TLOG_INFO;
|
||||
static tlog_format_func tlog_format;
|
||||
static tlog_early_print_func tlog_early_print;
|
||||
static unsigned int tlog_localtime_lock = 0;
|
||||
|
||||
static const char *tlog_level_str[] = {
|
||||
@@ -628,7 +629,7 @@ int tlog_printf(struct tlog_log *log, const char *format, ...)
|
||||
return len;
|
||||
}
|
||||
|
||||
static int _tlog_early_print(tlog_level level, const char *file, int line, const char *func, const char *format, va_list ap)
|
||||
static int _tlog_early_print(struct tlog_info_inter *info_inter, const char *format, va_list ap)
|
||||
{
|
||||
char log_buf[TLOG_MAX_LINE_LEN];
|
||||
size_t len = 0;
|
||||
@@ -644,9 +645,14 @@ static int _tlog_early_print(tlog_level level, const char *file, int line, const
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tlog_early_print != NULL) {
|
||||
tlog_early_print(&info_inter->info, format, ap);
|
||||
return out_len;
|
||||
}
|
||||
|
||||
len = snprintf(log_buf, sizeof(log_buf), "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d] ",
|
||||
cur_time.year, cur_time.mon, cur_time.mday, cur_time.hour, cur_time.min, cur_time.sec, cur_time.usec / 1000,
|
||||
tlog_get_level_string(level), file, line);
|
||||
tlog_get_level_string(info_inter->info.level), info_inter->info.file, info_inter->info.line);
|
||||
out_len = len;
|
||||
len = vsnprintf(log_buf + out_len, sizeof(log_buf) - out_len - 1, format, ap);
|
||||
out_len += len;
|
||||
@@ -662,7 +668,7 @@ static int _tlog_early_print(tlog_level level, const char *file, int line, const
|
||||
}
|
||||
|
||||
unused = write(STDOUT_FILENO, log_buf, out_len);
|
||||
return len;
|
||||
return out_len;
|
||||
}
|
||||
|
||||
int tlog_vext(tlog_level level, const char *file, int line, const char *func, void *userptr, const char *format, va_list ap)
|
||||
@@ -673,14 +679,6 @@ int tlog_vext(tlog_level level, const char *file, int line, const char *func, vo
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (tlog.root == NULL) {
|
||||
return _tlog_early_print(level, file, line, func, format, ap);
|
||||
}
|
||||
|
||||
if (unlikely(tlog.root->logsize <= 0)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (level >= TLOG_END) {
|
||||
return -1;
|
||||
}
|
||||
@@ -694,6 +692,14 @@ int tlog_vext(tlog_level level, const char *file, int line, const char *func, vo
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tlog.root == NULL) {
|
||||
return _tlog_early_print(&info_inter, format, ap);
|
||||
}
|
||||
|
||||
if (unlikely(tlog.root->logsize <= 0)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _tlog_vprintf(tlog.root, _tlog_root_log_buffer, &info_inter, format, ap);
|
||||
}
|
||||
|
||||
@@ -1620,6 +1626,11 @@ void tlog_set_early_printf(int enable)
|
||||
tlog_disable_early_print = (enable == 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
void tlog_reg_early_printf_callback(tlog_early_print_func callback)
|
||||
{
|
||||
tlog_early_print = callback;
|
||||
}
|
||||
|
||||
const char *tlog_get_level_string(tlog_level level)
|
||||
{
|
||||
if (level >= TLOG_END) {
|
||||
|
||||
@@ -111,6 +111,10 @@ extern void tlog_setlogscreen(int enable);
|
||||
/* enable early log to screen */
|
||||
extern void tlog_set_early_printf(int enable);
|
||||
|
||||
/* set early log callback */
|
||||
typedef void (*tlog_early_print_func)(struct tlog_loginfo *loginfo, const char *format, va_list ap);
|
||||
extern void tlog_reg_early_printf_callback(tlog_early_print_func callback);
|
||||
|
||||
/* Get log level in string */
|
||||
extern const char *tlog_get_level_string(tlog_level level);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user