Skip to content

Commit

Permalink
Check IsDebuggerPresent in msvc_sink before doing work. Fix #2408
Browse files Browse the repository at this point in the history
  • Loading branch information
gabime committed Oct 31, 2022
1 parent 4accce5 commit bd5a81d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/spdlog/sinks/msvc_sink.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright(c) 2016 Alexander Dalshov.
// Copyright(c) 2016 Alexander Dalshov & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)

#pragma once


#if defined(_WIN32)

# include <spdlog/details/null_mutex.h>
Expand All @@ -13,6 +14,7 @@

// Avoid including windows.h (https://stackoverflow.com/a/30741042)
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString);
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();

namespace spdlog {
namespace sinks {
Expand All @@ -24,17 +26,25 @@ class msvc_sink : public base_sink<Mutex>
{
public:
msvc_sink() = default;
msvc_sink(bool check_ebugger_present)
: check_debbugger_present_{check_ebugger_present} {};

protected:
void sink_it_(const details::log_msg &msg) override
{
if (check_debbugger_present_ && !IsDebuggerPresent())
{
return;
}
memory_buf_t formatted;
base_sink<Mutex>::formatter_->format(msg, formatted);
formatted.push_back('\0'); // add a null terminator for OutputDebugStringA
OutputDebugStringA(formatted.data());
}

void flush_() override {}

bool check_debbugger_present_ = true;
};

using msvc_sink_mt = msvc_sink<std::mutex>;
Expand Down

0 comments on commit bd5a81d

Please sign in to comment.