Open Lord-Kamina opened 4 days ago
As far as I can see there are two problems.
SpdLogger::getLogger()
is not protected by std::mutex
, so it does not look thread-safe.SpdLogger::getLogger()
returns std::shared_ptr&
, so std::shared_ptr
may become invalid if it leaves the scope of SpdLogger::getLogger()
.As far as I can see there are two problems.
SpdLogger::getLogger()
is not protected bystd::mutex
, so it does not look thread-safe.SpdLogger::getLogger()
returnsstd::shared_ptr&
, sostd::shared_ptr
may become invalid if it leaves the scope ofSpdLogger::getLogger()
.
I have sidestepped the error for now, although I don't think it was related to that. Still, it was kinda dumb on my part in retrospect.
No other obvious problems are found in the code provided.
Since spdlog::sinks::sink::should_log()
is crashing, it is possible that m_sink
is an invalid pointer.
There is another case where the logger was disabled after calling spdlog::shutdown()
(#2572), but the same problem should not occur since std::shared_ptr<spdlog::logger>
is copied in builtLoggers
.
No other obvious problems are found in the code provided.
Since
spdlog::sinks::sink::should_log()
is crashing, it is possible thatm_sink
is an invalid pointer.There is another case where the logger was disabled after calling
spdlog::shutdown()
(#2572), but the same problem should not occur sincestd::shared_ptr<spdlog::logger>
is copied inbuiltLoggers
.
I think the latter may have been the case, because the way I sidestepped it is, instead of adding an after-close
handler, I just moved the logging call to the SpdLogger destructor, right before calling spdlog::shutdown
Maybe duplicate #2113
After my question re: having a "subsystem"/"context" name for my logs, I decided to give a whirl to just having one logger for each subsystem, so I can use the logger name.
So, I'm constructing my logger class as follows:
Since creating loggers is theoretically cheap, but accessing the registry not so much, I decided to have my own in-class registry, in the form of an unordered map.
The way I thought it up is the following: I have a getLogger function (code attached below) that looks loggers up in its own registry and returns a
shared_ptr
if found. If not found, it looks it up in the spdlog registry. If It finds them, it adds it to the in-class registry and returns the pointer. If not found, it creates and registers the logger with spdlog, then it adds it to the in-class registry and returns the pointer.It looks like this:
One thing to note, the
warn
function in the constructor above is not spdlog's but rather a shortcut to a log function I wrote, as follows:The problem? I get a segfault, apparently caused by the
before_open
file-handler (If I comment that handler out, the sink is created without issue)Here's a backtrace of that crash:
Now, I thought maybe it could have been some sort of race-condition between creating the sink, the logger, opening the file and writing that first message to it, which is why I modified the code slightly to the version I pasted above (If you look closely, you'll see the backtrace is from a slightly different version). In this newer iteration, I decided to try to construct the
dist_sink
first, and construct theLOGGER
logger which would be used on that event handler. After both of those are done, I construct the console and file sinks, and add them to the dist-sink usingadd_sink
.By doing it in this way, I manage to get the
after-close
text on my console, before, once again, getting a segfault.Can you spot what the issue might be?