Skip to content

Commit

Permalink
Support rename logfile name on the fly.
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Sep 10, 2020
1 parent b6c3255 commit 14d5737
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
`Function`:Get log level.
1. tlog_set_logfile(const char *logfile)
`Function`:Update log file.
`logfile`: log file
1. tlog_setlogscreen(int enable)
`Function`:set whether the log is output to screen.
Expand Down Expand Up @@ -241,6 +246,13 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
`log`: The log stream handle.
`return value`: private parameter.
1. tlog_rename_logfile(tlog_log *log, const char *logfile)
`Function`: Rename log file.
`log`: The log stream handle.
`logfile`: log file.
## License
MIT License
13 changes: 12 additions & 1 deletion README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,15 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
`功能`: 设置日志级别,有效参数为TLOG_DEBUG, TLOG_INFO, TLOG_NOTICE, TLOG_WARN, TLOG_ERROR, TLOG_FATAL
1. tlog_setlevel(tlog_level level)
1. tlog_getlevel(tlog_level level)
`功能`: 获取设置的日志级别。
1. tlog_set_logfile(const char *logfile)
`功能`: 设置日志文件。
`logfile`: 日志文件。
1. tlog_setlogscreen(int enable)
`功能`: 设置日志是否输出到屏幕。
Expand Down Expand Up @@ -243,6 +248,12 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
`log`: 日志流句柄。
`返回值`: 私有参数。
1. tlog_rename_logfile(tlog_log *log, const char *logfile)
`功能`: 重命名日志文件。
`log`: 日志流句柄。
`logfile`: 日志文件。
## License
MIT License
50 changes: 41 additions & 9 deletions tlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ struct tlog_log {
char logdir[PATH_MAX];
char logname[TLOG_LOG_NAME_LEN];
char suffix[TLOG_LOG_NAME_LEN];
char pending_logfile[PATH_MAX];
int rename_pending;
int logsize;
int logcount;
int block;
Expand Down Expand Up @@ -1023,6 +1025,26 @@ static int _tlog_archive_log(struct tlog_log *log)
}
}

void _tlog_get_log_name_dir(struct tlog_log *log)
{
char log_file[PATH_MAX];
if (log->fd > 0) {
close(log->fd);
log->fd = -1;
}

pthread_mutex_lock(&tlog.lock);
strncpy(log_file, log->pending_logfile, sizeof(log_file) - 1);
log_file[sizeof(log_file) - 1] = '\0';
strncpy(log->logdir, dirname(log_file), sizeof(log->logdir));
log->logdir[sizeof(log->logdir) - 1] = '\0';
strncpy(log_file, log->pending_logfile, PATH_MAX);
log_file[sizeof(log_file) - 1] = '\0';
strncpy(log->logname, basename(log_file), sizeof(log->logname));
log->logname[sizeof(log->logname) - 1] = '\0';
pthread_mutex_unlock(&tlog.lock);
}

static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
{
int len;
Expand All @@ -1032,6 +1054,11 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
return 0;
}

if (log->rename_pending) {
_tlog_get_log_name_dir(log);
log->rename_pending = 0;
}

/* output log to screen */
if (log->logscreen) {
unused = write(STDOUT_FILENO, buff, bufflen);
Expand Down Expand Up @@ -1518,10 +1545,14 @@ tlog_level tlog_getlevel(void)
return tlog_set_level;
}

void tlog_set_logfile(const char *logfile)
{
tlog_rename_logfile(tlog.root, logfile);
}

tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
{
struct tlog_log *log = NULL;
char log_file[PATH_MAX];

if (tlog.run == 0) {
fprintf(stderr, "tlog is not initialized.");
Expand Down Expand Up @@ -1554,14 +1585,7 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
log->segment_log = ((flag & TLOG_SEGMENT) == 0) ? 0 : 1;
log->output_func = _tlog_write;

strncpy(log_file, logfile, sizeof(log_file) - 1);
log_file[sizeof(log_file) - 1] = '\0';
strncpy(log->logdir, dirname(log_file), sizeof(log->logdir));
log->logdir[sizeof(log->logdir) - 1] = '\0';
strncpy(log_file, logfile, PATH_MAX);
log_file[sizeof(log_file) - 1] = '\0';
strncpy(log->logname, basename(log_file), sizeof(log->logname));
log->logname[sizeof(log->logname) - 1] = '\0';
tlog_rename_logfile(log, logfile);
if (log->nocompress) {
strncpy(log->suffix, TLOG_SUFFIX_LOG, sizeof(log->suffix));
} else {
Expand Down Expand Up @@ -1605,6 +1629,14 @@ void tlog_close(tlog_log *log)
log->is_exit = 1;
}

void tlog_rename_logfile(struct tlog_log *log, const char *logfile)
{
pthread_mutex_lock(&tlog.lock);
strncpy(log->pending_logfile, logfile, sizeof(log->pending_logfile) - 1);
pthread_mutex_unlock(&tlog.lock);
log->rename_pending = 1;
}

int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
{
pthread_attr_t attr;
Expand Down
6 changes: 6 additions & 0 deletions tlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ extern int tlog_setlevel(tlog_level level);
/* get log level */
extern tlog_level tlog_getlevel(void);

/* set log file */
extern void tlog_set_logfile(const char *logfile);

/* enalbe log to screen */
extern void tlog_setlogscreen(int enable);

Expand Down Expand Up @@ -149,6 +152,9 @@ extern int tlog_write(struct tlog_log *log, const char *buff, int bufflen);
/* close log stream */
extern void tlog_close(tlog_log *log);

/* change log file */
extern void tlog_rename_logfile(struct tlog_log *log, const char *logfile);

/*
Function: Print log to log stream
log: log stream
Expand Down

0 comments on commit 14d5737

Please sign in to comment.