smartdns: update tlog.
This commit is contained in:
@@ -473,19 +473,29 @@ static int _smartdns_init(void)
|
||||
int i = 0;
|
||||
char logdir[PATH_MAX] = {0};
|
||||
int logbuffersize = 0;
|
||||
int enable_log_screen = 0;
|
||||
|
||||
if (get_system_mem_size() > 1024 * 1024 * 1024) {
|
||||
logbuffersize = 1024 * 1024;
|
||||
}
|
||||
|
||||
ret = tlog_init(logfile, dns_conf_log_size, dns_conf_log_num, logbuffersize, TLOG_NONBLOCK);
|
||||
safe_strncpy(logdir, _smartdns_log_path(), PATH_MAX);
|
||||
if (verbose_screen != 0 || dns_conf_log_console != 0 || access(dir_name(logdir), W_OK) != 0) {
|
||||
enable_log_screen = 1;
|
||||
}
|
||||
|
||||
unsigned int tlog_flag = TLOG_NONBLOCK;
|
||||
if (isatty(1) && enable_log_screen == 1) {
|
||||
tlog_flag |= TLOG_SCREEN_COLOR;
|
||||
}
|
||||
|
||||
ret = tlog_init(logfile, dns_conf_log_size, dns_conf_log_num, logbuffersize, tlog_flag);
|
||||
if (ret != 0) {
|
||||
tlog(TLOG_ERROR, "start tlog failed.\n");
|
||||
goto errout;
|
||||
}
|
||||
|
||||
safe_strncpy(logdir, _smartdns_log_path(), PATH_MAX);
|
||||
if (verbose_screen != 0 || dns_conf_log_console != 0 || access(dir_name(logdir), W_OK) != 0) {
|
||||
if (enable_log_screen) {
|
||||
tlog_setlogscreen(1);
|
||||
}
|
||||
|
||||
|
||||
71
src/tlog.c
71
src/tlog.c
@@ -79,6 +79,7 @@ struct tlog_log {
|
||||
int zip_pid;
|
||||
int multi_log;
|
||||
int logscreen;
|
||||
int logscreen_color;
|
||||
int segment_log;
|
||||
int max_line_size;
|
||||
int print_errmsg;
|
||||
@@ -651,8 +652,8 @@ static int _tlog_early_print(struct tlog_info_inter *info_inter, const char *for
|
||||
}
|
||||
|
||||
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(info_inter->info.level), info_inter->info.file, info_inter->info.line);
|
||||
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(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;
|
||||
@@ -1151,7 +1152,54 @@ static void _tlog_get_log_name_dir(struct tlog_log *log)
|
||||
pthread_mutex_unlock(&tlog.lock);
|
||||
}
|
||||
|
||||
static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
static int _tlog_write_screen(struct tlog_log *log, struct tlog_loginfo *info, const char *buff, int bufflen)
|
||||
{
|
||||
int unused __attribute__((unused));
|
||||
|
||||
if (bufflen <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (log->logscreen == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (log->logscreen_color && info != NULL) {
|
||||
const char *color = NULL;
|
||||
switch (info->level) {
|
||||
case TLOG_DEBUG:
|
||||
color = "\033[0;30m";
|
||||
break;
|
||||
case TLOG_NOTICE:
|
||||
color = "\033[0;37m";
|
||||
break;
|
||||
case TLOG_WARN:
|
||||
color = "\033[0;33m";
|
||||
break;
|
||||
case TLOG_ERROR:
|
||||
color = "\033[0;31m";
|
||||
break;
|
||||
case TLOG_FATAL:
|
||||
color = "\033[31;1m";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (color != NULL) {
|
||||
fprintf(stdout, "%s%s\e[0m", color, buff);
|
||||
} else {
|
||||
fprintf(stdout, "%s", buff);
|
||||
}
|
||||
} else {
|
||||
/* output log to screen */
|
||||
unused = write(STDOUT_FILENO, buff, bufflen);
|
||||
}
|
||||
|
||||
return bufflen;
|
||||
}
|
||||
|
||||
static int _tlog_write_ext(struct tlog_log *log, struct tlog_loginfo *info, const char *buff, int bufflen)
|
||||
{
|
||||
int len;
|
||||
int unused __attribute__((unused));
|
||||
@@ -1167,7 +1215,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
|
||||
/* output log to screen */
|
||||
if (log->logscreen) {
|
||||
unused = write(STDOUT_FILENO, buff, bufflen);
|
||||
_tlog_write_screen(log, info, buff, bufflen);
|
||||
}
|
||||
|
||||
if (log->logcount <= 0) {
|
||||
@@ -1251,6 +1299,11 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
{
|
||||
return _tlog_write_ext(log, NULL, buff, bufflen);
|
||||
}
|
||||
|
||||
int tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
{
|
||||
return _tlog_write(log, buff, bufflen);
|
||||
@@ -1484,7 +1537,7 @@ static int _tlog_root_write_log(struct tlog_log *log, const char *buff, int buff
|
||||
if (tlog.output_func == NULL) {
|
||||
if (log->segment_log) {
|
||||
head = (struct tlog_segment_log_head *)buff;
|
||||
return _tlog_write(log, head->data, head->len);
|
||||
return _tlog_write_ext(log, &head->info, head->data, head->len);
|
||||
}
|
||||
return _tlog_write(log, buff, bufflen);
|
||||
}
|
||||
@@ -1756,7 +1809,7 @@ static void _tlog_get_gzip_cmd_path(void)
|
||||
if (access(gzip_cmd_path, X_OK) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
snprintf(tlog.gzip_cmd, sizeof(tlog.gzip_cmd), "%s", gzip_cmd_path);
|
||||
break;
|
||||
}
|
||||
@@ -1797,6 +1850,7 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
|
||||
log->block = ((flag & TLOG_NONBLOCK) == 0) ? 1 : 0;
|
||||
log->nocompress = ((flag & TLOG_NOCOMPRESS) == 0) ? 0 : 1;
|
||||
log->logscreen = ((flag & TLOG_SCREEN) == 0) ? 0 : 1;
|
||||
log->logscreen_color = ((flag & TLOG_SCREEN_COLOR) == 0) ? 0 : 1;
|
||||
log->multi_log = ((flag & TLOG_MULTI_WRITE) == 0) ? 0 : 1;
|
||||
log->segment_log = ((flag & TLOG_SEGMENT) == 0) ? 0 : 1;
|
||||
log->max_line_size = TLOG_MAX_LINE_LEN;
|
||||
@@ -1808,6 +1862,11 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
|
||||
log->nocompress = 1;
|
||||
}
|
||||
|
||||
if (log->logscreen_color == 1) {
|
||||
log->logscreen = 1;
|
||||
log->segment_log = 1;
|
||||
}
|
||||
|
||||
tlog_rename_logfile(log, logfile);
|
||||
if (log->nocompress) {
|
||||
strncpy(log->suffix, TLOG_SUFFIX_LOG, sizeof(log->suffix));
|
||||
|
||||
@@ -66,6 +66,9 @@ struct tlog_time {
|
||||
/* enable support fork process */
|
||||
#define TLOG_SUPPORT_FORK (1 << 5)
|
||||
|
||||
/* enable output to screen with color */
|
||||
#define TLOG_SCREEN_COLOR (1 << 6)
|
||||
|
||||
struct tlog_loginfo {
|
||||
tlog_level level;
|
||||
const char *file;
|
||||
@@ -301,4 +304,4 @@ private:
|
||||
#define tlog_error(...) tlog(TLOG_ERROR, ##__VA_ARGS__)
|
||||
#define tlog_fatal(...) tlog(TLOG_FATAL, ##__VA_ARGS__)
|
||||
#endif
|
||||
#endif // !TLOG_H
|
||||
#endif // !TLOG_H
|
||||
Reference in New Issue
Block a user