Skip to content

Commit f3d0a33

Browse files
committed
Support fork process
1 parent 27ef655 commit f3d0a33

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
140140
* `TLOG_NOCOMPRESS`: The archive log is not compressed.
141141
* `TLOG_SEGMENT`: Log segmentation, used to register the callback function, returns a complete log for subsequent processing.
142142
* `TLOG_NONBLOCK`: Do not block when buffer is insufficient.
143-
* `TLOG_SCREEN`: Output logs to the screen.
143+
* `TLOG_SCREEN`: Output logs to the screen.
144+
* `TLOG_SUPPORT_FORK`: Support fork process.
144145
145146
1. tlog(level, format, ...)
146147
@@ -252,7 +253,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
252253
`log`: The log stream handle.
253254
`logfile`: log file.
254255
255-
256256
## License
257257
258258
MIT License

README_zh-CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBASE_FILE_NAME='\"$(notdir $<)\"'")
142142
* `TLOG_SEGMENT`: 日志分段,用于注册回调函数后,返回一条完整的日志用于后续处理。
143143
* `TLOG_NONBLOCK`: 缓冲区不足时,不阻塞。
144144
* `TLOG_SCREEN`: 输出日志到屏幕。
145+
* `TLOG_SUPPORT_FORK`: 支持fork运行的进程。
145146
146147
1. tlog(level, format, ...)
147148

tlog.c

+48-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct tlog_log {
6161
char suffix[TLOG_LOG_NAME_LEN];
6262
char pending_logfile[PATH_MAX];
6363
int rename_pending;
64+
int fail;
6465
int logsize;
6566
int logcount;
6667
int block;
@@ -1072,7 +1073,7 @@ static int _tlog_write(struct tlog_log *log, const char *buff, int bufflen)
10721073
int len;
10731074
int unused __attribute__ ((unused));
10741075

1075-
if (bufflen <= 0) {
1076+
if (bufflen <= 0 || log->fail) {
10761077
return 0;
10771078
}
10781079

@@ -1599,6 +1600,7 @@ tlog_log *tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int bu
15991600
log->filesize = 0;
16001601
log->zip_pid = -1;
16011602
log->is_exit = 0;
1603+
log->fail = 0;
16021604
log->waiters = 0;
16031605
log->block = ((flag & TLOG_NONBLOCK) == 0) ? 1 : 0;
16041606
log->nocompress = ((flag & TLOG_NOCOMPRESS) == 0) ? 0 : 1;
@@ -1660,6 +1662,48 @@ void tlog_rename_logfile(struct tlog_log *log, const char *logfile)
16601662
log->rename_pending = 1;
16611663
}
16621664

1665+
static void tlog_fork_prepare() {
1666+
if (tlog.root == NULL) {
1667+
return;
1668+
}
1669+
1670+
pthread_mutex_lock(&tlog.lock);
1671+
}
1672+
1673+
static void tlog_fork_parent() {
1674+
if (tlog.root == NULL) {
1675+
return;
1676+
}
1677+
1678+
pthread_mutex_unlock(&tlog.lock);
1679+
}
1680+
1681+
static void tlog_fork_child() {
1682+
pthread_attr_t attr;
1683+
tlog_log *next;
1684+
if (tlog.root == NULL) {
1685+
return;
1686+
}
1687+
1688+
pthread_attr_init(&attr);
1689+
int ret = pthread_create(&tlog.tid, &attr, _tlog_work, NULL);
1690+
if (ret != 0) {
1691+
fprintf(stderr, "create tlog work thread failed, %s\n", strerror(errno));
1692+
goto errout;
1693+
}
1694+
1695+
goto out;
1696+
errout:
1697+
next = tlog.log;
1698+
while (next) {
1699+
next->fail = 1;
1700+
next = next->next;
1701+
}
1702+
out:
1703+
pthread_mutex_unlock(&tlog.lock);
1704+
1705+
}
1706+
16631707
int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
16641708
{
16651709
pthread_attr_t attr;
@@ -1700,6 +1744,9 @@ int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize
17001744
}
17011745

17021746
tlog.root = log;
1747+
if (flag & TLOG_SUPPORT_FORK) {
1748+
pthread_atfork(&tlog_fork_prepare, &tlog_fork_parent, &tlog_fork_child);
1749+
}
17031750
return 0;
17041751
errout:
17051752
if (tlog.tid > 0) {

tlog.h

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ struct tlog_time {
6060
/* enable log to screen */
6161
#define TLOG_SCREEN (1 << 4)
6262

63+
/* enable suppport fork process */
64+
#define TLOG_SUPPORT_FORK (1 << 5)
65+
6366
struct tlog_loginfo {
6467
tlog_level level;
6568
const char *file;

0 commit comments

Comments
 (0)