spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

Async logger crash after fork()

Open hzhaop opened this issue 10 years ago • 8 comments

Here is my situation: A main process have a global async logger, and then it will fork several subprocesses. When the subprocess call log function of the global async logger, the process dead because the _worker_thread in the new process is no longer the thread in the main process.

Maybe we can do some cleanup when _worker_thread::id != this_thread::get_id() or so?

Thanks.

hzhaop avatar Jan 22 '16 10:01 hzhaop

spdlog does not support multi process. I dont think there is a quick workaround

gabime avatar Jan 22 '16 15:01 gabime

Does that mean I can't use spdlog in daemon?

bumbar1 avatar Feb 18 '16 17:02 bumbar1

it depends which type of daemon of course

gabime avatar Feb 18 '16 20:02 gabime

I'm working on a server (reading data through socket), and it's crashing when I run in as daemon. Are there any special steps I should be taking, in regards to spdlog library?

bumbar1 avatar Feb 18 '16 23:02 bumbar1

make sure you create the loggers after the fork

gabime avatar Feb 19 '16 06:02 gabime

I have logger declared in global scope as std::shared_ptr<spdlog::logger> logger, then I do the daemonization process (fork, setsid, closing stdin, stdout and stderr) and after that I create a temporary logger and assign it to logger. When I try call logger->info(...) it crashes with segfault. I'm using syslog and file logger, if it makes any difference.

bumbar1 avatar Feb 19 '16 10:02 bumbar1

I managed to got it working, but seems I have to manually call flush when writing to file log. Is there some option I missed, or is that part of library's behavior?

bumbar1 avatar Feb 21 '16 00:02 bumbar1

there is a force_flush param you can use. please see the docs

gabime avatar Feb 21 '16 09:02 gabime