pymumu / tinylog

A lightweight C, C++ logging library developed for Linux, It is designed with high performance, asynchronized, thread-safe and process-safe; tinylog是一个专为UNIX设计的轻量级的C/C++日志模块,其提供了高性能,异步,线程安全,进程安全的日志功能。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When I use tinylog in multiprocess, it make me confuse.

jesson3264 opened this issue · comments

The another.log file only record the parent process's print info, Did I lack of some func revoke?

#include "tlog.h"

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <time.h>
#define PID_IS_CHILD 0

tlog_log *log = NULL;
int printlog()
{
	int i = 0;
	tlog_printf(log, "This is child log stream %d\n", i++);
	while (i<1000)
	{
		tlog_printf(log, "This is child log stream %d\n", i++);
		usleep(10);
	}

	return 0;
}


int paraentprint()
{
	int i = 0;
	while (i<10000)
	{
		tlog_printf(log, "This is paraent log stream %d\n", i++);
		usleep(10);
	}

	return 0;
}
int main(int argc, char *argv[])
{

    /* init and output log message */
    tlog_init("example.log", 1024 * 1024, 8, 0, 0);
    //tlog(TLOG_INFO, "This is a log message.\n");

    /* c++ cout style log */
    //tlog_info << "This is a c++ style log.\n";
    
    /* open another log file, and output message*/
    log = tlog_open("another.log", 1024*1024 * 1024, 8, 0, TLOG_SEGMENT);
    tlog_printf(log, "This is a separate log stream\n");
    /* c++ style log */
    tlog_out(log) << "This is a separate c++ log stream\n"; 
    

	int pid = fork();
	if (pid == PID_IS_CHILD)
	{
		fprintf(stderr, "child func start\n");
		printlog();
	}
	else if (pid < 0) 
	{
		tlog_printf(log, "bad func start\n");
	}
	else 
	{
		tlog_printf(log, "is parent func start\n");
	}

	paraentprint();
    /* close log stream */
	sleep(100);
    tlog_close(log);

    /* flush pending message, and exit tlog */
    tlog_exit();
    return 0;
}

This scenario cannot be supported. the reason is that fork does not clone threads.

You can try this, but you should take care with multithread process when executing fork
tinylog.zip

You mean multithread and multiprocess mode should be careful ?