Skip to content

Commit 14d5737

Browse files
committed
Support rename logfile name on the fly.
1 parent b6c3255 commit 14d5737

File tree

4 files changed

+71
-10
lines changed

4 files changed

+71
-10
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
172172
173173
`Function`:Get log level.
174174
175+
1. tlog_set_logfile(const char *logfile)
176+
177+
`Function`:Update log file.
178+
`logfile`: log file
179+
175180
1. tlog_setlogscreen(int enable)
176181
177182
`Function`:set whether the log is output to screen.
@@ -241,6 +246,13 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
241246
`log`: The log stream handle.
242247
`return value`: private parameter.
243248
249+
1. tlog_rename_logfile(tlog_log *log, const char *logfile)
250+
251+
`Function`: Rename log file.
252+
`log`: The log stream handle.
253+
`logfile`: log file.
254+
255+
244256
## License
245257
246258
MIT License

README_zh-CN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
169169
170170
`功能`: 设置日志级别,有效参数为TLOG_DEBUG, TLOG_INFO, TLOG_NOTICE, TLOG_WARN, TLOG_ERROR, TLOG_FATAL
171171
172-
1. tlog_setlevel(tlog_level level)
172+
1. tlog_getlevel(tlog_level level)
173173
174174
`功能`: 获取设置的日志级别。
175175
176+
1. tlog_set_logfile(const char *logfile)
177+
178+
`功能`: 设置日志文件。
179+
`logfile`: 日志文件。
180+
176181
1. tlog_setlogscreen(int enable)
177182
178183
`功能`: 设置日志是否输出到屏幕。
@@ -243,6 +248,12 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
243248
`log`: 日志流句柄。
244249
`返回值`: 私有参数。
245250
251+
1. tlog_rename_logfile(tlog_log *log, const char *logfile)
252+
253+
`功能`: 重命名日志文件。
254+
`log`: 日志流句柄。
255+
`logfile`: 日志文件。
256+
246257
## License
247258
248259
MIT License

tlog.c

+41-9
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ struct tlog_log {
5757
char logdir[PATH_MAX];
5858
char logname[TLOG_LOG_NAME_LEN];
5959
char suffix[TLOG_LOG_NAME_LEN];
60+
char pending_logfile[PATH_MAX];
61+
int rename_pending;
6062
int logsize;
6163
int logcount;
6264
int block;
@@ -1023,6 +1025,26 @@ static int _tlog_archive_log(struct tlog_log *log)
10231025
}
10241026
}
10251027

1028+
void _tlog_get_log_name_dir(struct tlog_log *log)
1029+
{
1030+
char log_file[PATH_MAX];
1031+
if (log->fd > 0) {
1032+
close(log->fd);
1033+
log->fd = -1;
1034+
}
1035+
1036+
pthread_mutex_lock(&tlog.lock);
1037+
strncpy(log_file, log->pending_logfile, sizeof(log_file) - 1);
1038+
log_file[sizeof(log_file) - 1] = '\0';
1039+
strncpy(log->logdir, dirname(log_file), sizeof(log->logdir));
1040+
log->logdir[sizeof(log->logdir) - 1] = '\0';
1041+
strncpy(log_file, log->pending_logfile, PATH_MAX);
1042+
log_file[sizeof(log_file) - 1] = '\0';
1043+
strncpy(log->logname, basename(log_file), sizeof(log->logname));
1044+
log->logname[sizeof(log->logname) - 1] = '\0';
1045+
pthread_mutex_unlock(&tlog.lock);
1046+
}
1047+
10261048
static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
10271049
{
10281050
int len;
@@ -1032,6 +1054,11 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
10321054
return 0;
10331055
}
10341056

1057+
if (log->rename_pending) {
1058+
_tlog_get_log_name_dir(log);
1059+
log->rename_pending = 0;
1060+
}
1061+
10351062
/* output log to screen */
10361063
if (log->logscreen) {
10371064
unused = write(STDOUT_FILENO, buff, bufflen);
@@ -1518,10 +1545,14 @@ tlog_level tlog_getlevel(void)
15181545
return tlog_set_level;
15191546
}
15201547

1548+
void tlog_set_logfile(const char *logfile)
1549+
{
1550+
tlog_rename_logfile(tlog.root, logfile);
1551+
}
1552+
15211553
tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
15221554
{
15231555
struct tlog_log *log = NULL;
1524-
char log_file[PATH_MAX];
15251556

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

1557-
strncpy(log_file, logfile, sizeof(log_file) - 1);
1558-
log_file[sizeof(log_file) - 1] = '\0';
1559-
strncpy(log->logdir, dirname(log_file), sizeof(log->logdir));
1560-
log->logdir[sizeof(log->logdir) - 1] = '\0';
1561-
strncpy(log_file, logfile, PATH_MAX);
1562-
log_file[sizeof(log_file) - 1] = '\0';
1563-
strncpy(log->logname, basename(log_file), sizeof(log->logname));
1564-
log->logname[sizeof(log->logname) - 1] = '\0';
1588+
tlog_rename_logfile(log, logfile);
15651589
if (log->nocompress) {
15661590
strncpy(log->suffix, TLOG_SUFFIX_LOG, sizeof(log->suffix));
15671591
} else {
@@ -1605,6 +1629,14 @@ void tlog_close(tlog_log *log)
16051629
log->is_exit = 1;
16061630
}
16071631

1632+
void tlog_rename_logfile(struct tlog_log *log, const char *logfile)
1633+
{
1634+
pthread_mutex_lock(&tlog.lock);
1635+
strncpy(log->pending_logfile, logfile, sizeof(log->pending_logfile) - 1);
1636+
pthread_mutex_unlock(&tlog.lock);
1637+
log->rename_pending = 1;
1638+
}
1639+
16081640
int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
16091641
{
16101642
pthread_attr_t attr;

tlog.h

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ extern int tlog_setlevel(tlog_level level);
9191
/* get log level */
9292
extern tlog_level tlog_getlevel(void);
9393

94+
/* set log file */
95+
extern void tlog_set_logfile(const char *logfile);
96+
9497
/* enalbe log to screen */
9598
extern void tlog_setlogscreen(int enable);
9699

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

155+
/* change log file */
156+
extern void tlog_rename_logfile(struct tlog_log *log, const char *logfile);
157+
152158
/*
153159
Function: Print log to log stream
154160
log: log stream

0 commit comments

Comments
 (0)