dns-debug: support record fail packet for debugging
This commit is contained in:
43
src/tlog.c
43
src/tlog.c
@@ -79,6 +79,7 @@ struct tlog_log {
|
||||
int zip_pid;
|
||||
int multi_log;
|
||||
int logscreen;
|
||||
int no_write_log;
|
||||
int segment_log;
|
||||
int max_line_size;
|
||||
|
||||
@@ -216,7 +217,6 @@ static int _tlog_mkdir(const char *path)
|
||||
}
|
||||
|
||||
if (mkdir(path_c, 0750) != 0) {
|
||||
fprintf(stderr, "create directory %s failed, %s\n", path_c, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1130,6 +1130,10 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
unused = write(STDOUT_FILENO, buff, bufflen);
|
||||
}
|
||||
|
||||
if (log->no_write_log) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* if log file size exceeds threshold, start to compress */
|
||||
if (log->multi_log && log->fd > 0) {
|
||||
log->filesize = lseek(log->fd, 0, SEEK_END);
|
||||
@@ -1160,7 +1164,15 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
|
||||
|
||||
char logfile[PATH_MAX * 2];
|
||||
if (_tlog_mkdir(log->logdir) != 0) {
|
||||
fprintf(stderr, "create log dir %s failed.\n", log->logdir);
|
||||
if (print_errmsg == 0) {
|
||||
return -1;
|
||||
}
|
||||
print_errmsg = 0;
|
||||
fprintf(stderr, "create log dir %s failed, %s\n", log->logdir, strerror(errno));
|
||||
if (errno == EACCES && log->logscreen == 0) {
|
||||
fprintf(stderr, "no permission to write log file, output log to console\n");
|
||||
tlog_logscreen_only(log, 1);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
snprintf(logfile, sizeof(logfile), "%s/%s", log->logdir, log->logname);
|
||||
@@ -1574,11 +1586,26 @@ static void _tlog_log_setlogscreen(struct tlog_log *log, int enable)
|
||||
log->logscreen = (enable != 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
static void _tlog_log_setlogscreen_only(struct tlog_log *log, int enable)
|
||||
{
|
||||
if (log == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
log->logscreen = (enable != 0) ? 1 : 0;
|
||||
log->no_write_log = (enable != 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
void tlog_setlogscreen(int enable)
|
||||
{
|
||||
_tlog_log_setlogscreen(tlog.root, enable);
|
||||
}
|
||||
|
||||
void tlog_setlogscreen_only(int enable)
|
||||
{
|
||||
_tlog_log_setlogscreen_only(tlog.root, enable);
|
||||
}
|
||||
|
||||
int tlog_write_log(char *buff, int bufflen)
|
||||
{
|
||||
if (unlikely(tlog.root == NULL)) {
|
||||
@@ -1597,6 +1624,15 @@ void tlog_logscreen(tlog_log *log, int enable)
|
||||
_tlog_log_setlogscreen(log, enable);
|
||||
}
|
||||
|
||||
void tlog_logscreen_only(tlog_log *log, int enable)
|
||||
{
|
||||
if (log == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
_tlog_log_setlogscreen_only(log, enable);
|
||||
}
|
||||
|
||||
int tlog_reg_output_func(tlog_log *log, tlog_output_func output)
|
||||
{
|
||||
if (log == NULL) {
|
||||
@@ -1830,13 +1866,13 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
|
||||
}
|
||||
tlog_reg_output_func(log, _tlog_root_write_log);
|
||||
|
||||
tlog.root = log;
|
||||
ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "create tlog work thread failed, %s\n", strerror(errno));
|
||||
goto errout;
|
||||
}
|
||||
|
||||
tlog.root = log;
|
||||
if (flag & TLOG_SUPPORT_FORK) {
|
||||
pthread_atfork(&tlog_fork_prepare, &tlog_fork_parent, &tlog_fork_child);
|
||||
}
|
||||
@@ -1852,6 +1888,7 @@ errout:
|
||||
pthread_cond_destroy(&tlog.cond);
|
||||
pthread_mutex_destroy(&tlog.lock);
|
||||
tlog.run = 0;
|
||||
tlog.root = NULL;
|
||||
|
||||
_tlog_close(log, 1);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user