msvc_sink.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright(c) 2016 Alexander Dalshov.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #if defined(_WIN32)
  5. # include <spdlog/details/null_mutex.h>
  6. # include <spdlog/sinks/base_sink.h>
  7. # include <mutex>
  8. # include <string>
  9. // Avoid including windows.h (https://stackoverflow.com/a/30741042)
  10. extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString);
  11. namespace spdlog {
  12. namespace sinks {
  13. /*
  14. * MSVC sink (logging using OutputDebugStringA)
  15. */
  16. template<typename Mutex>
  17. class msvc_sink : public base_sink<Mutex>
  18. {
  19. public:
  20. msvc_sink() = default;
  21. protected:
  22. void sink_it_(const details::log_msg &msg) override
  23. {
  24. memory_buf_t formatted;
  25. base_sink<Mutex>::formatter_->format(msg, formatted);
  26. formatted.push_back('\0'); // add a null terminator for OutputDebugStringA
  27. OutputDebugStringA(formatted.data());
  28. }
  29. void flush_() override {}
  30. };
  31. using msvc_sink_mt = msvc_sink<std::mutex>;
  32. using msvc_sink_st = msvc_sink<details::null_mutex>;
  33. using windebug_sink_mt = msvc_sink_mt;
  34. using windebug_sink_st = msvc_sink_st;
  35. } // namespace sinks
  36. } // namespace spdlog
  37. #endif