-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check IsDebuggerPresent in msvc_sink before doing work #2408
Comments
Seems like good idea. How fast is |
The I made this change - from checking once at startup and adding the |
Well in this case this seems fine. |
This is the hard part of library design. The strongest argument I have for not adding the Maybe the answer is for me to implement my own |
If there is a big performance gain (any numbers?) It is certainly worth while to add to spdlog as an option. |
FWIW I already have such an implementation in my code: class debug_sink final : public spdlog::sinks::base_sink<spdlog::details::null_mutex> {
protected:
void sink_it_(const spdlog::details::log_msg &msg) override
{
if (IsDebuggerPresent())
{
spdlog::memory_buf_t formatted;
this->formatter_->format(msg, formatted);
formatted.push_back('\0');
OutputDebugStringA(formatted.data());
}
}
void flush_() noexcept override
{ }
}; I am not opposed to making this part of the built-in MSVC sink |
Sorry for the delayed response, I don't really have any real numbers to show regarding performance. In my current (non-game) project I figured it would be easiest to just implement it myself and so wrote this:
|
Please note that "OutputDebugString" doesn't necessarily need a debugger. Applications like DebugView can receive these messages. |
Exactly. Thats why I am hesitating adding this. On the other hand seems like with debugger is the common use and IsDebuggerPresent() probably improves performance significantly. |
Changed default behavior to check for debugger using |
I'm not sure if everybody will want to do this, so this is just a suggestion and something up for discussion. I've included some code below.
The
OutputDebugString
function can be relatively slow at times since it has to lock a global mutex before copying your message to a listener. With many applications trying to use this in parallel you can hit contention and slow down your program. One way to mitigate this is to check if a debugger is attached first usingIsDebuggerPresent
before doing any processing work and callingOutputDebugString
.The
IsDebuggerPresent
function itself just checks a global flag in your process address space, so it doesn't need to lock or go into the kernel, so it is quite fast and can be checked before each call.I've seen people call it once at program startup when setting up logging, so if a debugger is not attached you skip initialising that sink. In this case you do end up missing all logging output when you later attach a debugger to the running process.
The one down side I see of checking if a debugger is attached before sinking every log message is that you won't see your debug output in something like Sysinternals DebugView tool. But realistically you should be able to hook up a different logging sink if you want a live feed.
The code changes required are adding this forward declare:
Then changing the body of
msvc_sink.h
to something like:The text was updated successfully, but these errors were encountered: