spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

Dont want spdlog crash on exceptions !!

Open rohitjun opened this issue 2 years ago • 10 comments

Hi,

I have seen spdlog crashes sometimes due to some exceptions. For example below

terminate called after throwing an instance of 'spdlog::spdlog_ex' what: async log: thread log does not exist anymore.

I understand it crashed due to exit call from code. I dont want spdlog to crashes but ignore, and i will handle code errors myself, How could i do that.

Thanks very much for suggestions and help.

rohitjun avatar Jan 23 '23 10:01 rohitjun

Caused by using async logger even though the thread pool has been deleted.

https://github.com/gabime/spdlog/blob/ad08f13aacc92b5e38c4a77f9cabd24d54fd53b1/include/spdlog/async_logger-inl.h#L27-L37

spdlog does not work after calling spdlog::shutdown().

tt4g avatar Jan 23 '23 10:01 tt4g

Thanks,

Is it possible to avoid SPDLOG crashing even though its called after thread pool is deleted?

rohitjun avatar Jan 23 '23 11:01 rohitjun

Yes.

tt4g avatar Jan 23 '23 11:01 tt4g

How?

thanks.

rohitjun avatar Jan 23 '23 11:01 rohitjun

My apologies, I misread your comment.

The async logger that uses the thread pool will not work after the thread pool is deleted. Non async logger does not use a thread pool so it continues to work even after the thread pool is deleted.

Note: After spdlog::shutdown() is called, spdlog's internal resources are released, so some features (including the thread pool) will not work.

tt4g avatar Jan 23 '23 11:01 tt4g

That being said, I think the async sink_it_() better not to throw anyway and call err_handler_ instead. Or maybe better just wrap with

try {....} 
  SPDLOG_LOGGER_CATCH(msg.source)

PR is welcome

gabime avatar Jan 23 '23 12:01 gabime

hi,

I was reading in some links, i can create a separate thread pool for spdlog then it wont be impacted from others. Will it impact the logging in someway or if there is any other impact? Sorry dont know much internals of how spdlog works.

Thanks for help

rohitjun avatar Jan 23 '23 12:01 rohitjun

Custom thread pools are not managed by spdlog, so you are responsible for destroying the pools yourself.

Wiki: https://github.com/gabime/spdlog/wiki/6.-Asynchronous-logging#construct-directly-and-use-a-custom-thread-pool

tt4g avatar Jan 23 '23 12:01 tt4g

thanks, I checked, creating a separate thread pool does not suit my setup.

can i use some option when i call create_async_nb, so it does not crash but some error trace only ?

auto new_logger = spdlog::create_async_nbspdlog::sinks::stdout_sink_mt("console_logger");

rohitjun avatar Jan 23 '23 13:01 rohitjun

The async_logger custom steps are all described in the Wiki. And spdlog::create_async_nb() is the factory function for the thread pool and async logger. What is done in this function is just one of the Wiki setup steps.

https://github.com/gabime/spdlog/blob/5a589438d2d2abc67bdbec2aad7e2a94800c00ea/include/spdlog/async.h#L69-L73

https://github.com/gabime/spdlog/blob/5a589438d2d2abc67bdbec2aad7e2a94800c00ea/include/spdlog/async.h#L61

https://github.com/gabime/spdlog/blob/5a589438d2d2abc67bdbec2aad7e2a94800c00ea/include/spdlog/async.h#L31-L58

tt4g avatar Jan 23 '23 13:01 tt4g

Fixed in 1fba68bfe238629733b49d4c9d576556f7788585

gabime avatar May 27 '23 12:05 gabime