Photon 1.0.0
Loading...
Searching...
No Matches
msvc_sink.h
Go to the documentation of this file.
1// Copyright(c) 2016 Alexander Dalshov & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#if defined(_WIN32)
7
9#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
10#include <spdlog/details/os.h>
11#endif
13
14#include <mutex>
15#include <string>
16
17// Avoid including windows.h (https://stackoverflow.com/a/30741042)
18#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
19extern "C" __declspec(dllimport) void __stdcall OutputDebugStringW(const wchar_t* lpOutputString);
20#else
21extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* lpOutputString);
22#endif
23extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
24
25namespace spdlog
26{
27 namespace sinks
28 {
29 /*
30 * MSVC sink (logging using OutputDebugStringA)
31 */
32 template <typename Mutex>
33 class msvc_sink : public base_sink<Mutex>
34 {
35 public:
36 msvc_sink() = default;
37 msvc_sink(bool check_debugger_present)
38 : check_debugger_present_{check_debugger_present} {};
39
40 protected:
41 void sink_it_(const details::log_msg& msg) override
42 {
43 if (check_debugger_present_ && !IsDebuggerPresent())
44 {
45 return;
46 }
47 memory_buf_t formatted;
48 base_sink<Mutex>::formatter_->format(msg, formatted);
49 formatted.push_back('\0'); // add a null terminator for OutputDebugString
50#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
51 wmemory_buf_t wformatted;
52 details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted);
53 OutputDebugStringW(wformatted.data());
54#else
55 OutputDebugStringA(formatted.data());
56#endif
57 }
58
59 void flush_() override
60 {
61 }
62
63 bool check_debugger_present_ = true;
64 };
65
66 using msvc_sink_mt = msvc_sink<std::mutex>;
67 using msvc_sink_st = msvc_sink<details::null_mutex>;
68
69 using windebug_sink_mt = msvc_sink_mt;
70 using windebug_sink_st = msvc_sink_st;
71
72 } // namespace sinks
73} // namespace spdlog
74
75#endif
Definition async.h:26
fmt::basic_string_view< char > string_view_t
Definition common.h:172
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173