stdout_sinks-inl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #ifndef SPDLOG_HEADER_ONLY
  5. # include <spdlog/sinks/stdout_sinks.h>
  6. #endif
  7. #include <spdlog/details/console_globals.h>
  8. #include <spdlog/pattern_formatter.h>
  9. #include <memory>
  10. #ifdef _WIN32
  11. // under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675)
  12. // so instead we use ::FileWrite
  13. # include <spdlog/details/windows_include.h>
  14. # ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp
  15. # include <fileapi.h> // WriteFile (..)
  16. # endif
  17. # include <io.h> // _get_osfhandle(..)
  18. # include <stdio.h> // _fileno(..)
  19. #endif // WIN32
  20. namespace spdlog {
  21. namespace sinks {
  22. template<typename ConsoleMutex>
  23. SPDLOG_INLINE stdout_sink_base<ConsoleMutex>::stdout_sink_base(FILE *file)
  24. : mutex_(ConsoleMutex::mutex())
  25. , file_(file)
  26. , formatter_(details::make_unique<spdlog::pattern_formatter>())
  27. {
  28. #ifdef _WIN32
  29. // get windows handle from the FILE* object
  30. handle_ = reinterpret_cast<HANDLE>(::_get_osfhandle(::_fileno(file_)));
  31. // don't throw to support cases where no console is attached,
  32. // and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE).
  33. // throw only if non stdout/stderr target is requested (probably regular file and not console).
  34. if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr)
  35. {
  36. throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
  37. }
  38. #endif // WIN32
  39. }
  40. template<typename ConsoleMutex>
  41. SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::log(const details::log_msg &msg)
  42. {
  43. #ifdef _WIN32
  44. if (handle_ == INVALID_HANDLE_VALUE)
  45. {
  46. return;
  47. }
  48. std::lock_guard<mutex_t> lock(mutex_);
  49. memory_buf_t formatted;
  50. formatter_->format(msg, formatted);
  51. auto size = static_cast<DWORD>(formatted.size());
  52. DWORD bytes_written = 0;
  53. bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0;
  54. if (!ok)
  55. {
  56. throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + std::to_string(::GetLastError()));
  57. }
  58. #else
  59. std::lock_guard<mutex_t> lock(mutex_);
  60. memory_buf_t formatted;
  61. formatter_->format(msg, formatted);
  62. ::fwrite(formatted.data(), sizeof(char), formatted.size(), file_);
  63. #endif // WIN32
  64. ::fflush(file_); // flush every line to terminal
  65. }
  66. template<typename ConsoleMutex>
  67. SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::flush()
  68. {
  69. std::lock_guard<mutex_t> lock(mutex_);
  70. fflush(file_);
  71. }
  72. template<typename ConsoleMutex>
  73. SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_pattern(const std::string &pattern)
  74. {
  75. std::lock_guard<mutex_t> lock(mutex_);
  76. formatter_ = std::unique_ptr<spdlog::formatter>(new pattern_formatter(pattern));
  77. }
  78. template<typename ConsoleMutex>
  79. SPDLOG_INLINE void stdout_sink_base<ConsoleMutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
  80. {
  81. std::lock_guard<mutex_t> lock(mutex_);
  82. formatter_ = std::move(sink_formatter);
  83. }
  84. // stdout sink
  85. template<typename ConsoleMutex>
  86. SPDLOG_INLINE stdout_sink<ConsoleMutex>::stdout_sink()
  87. : stdout_sink_base<ConsoleMutex>(stdout)
  88. {}
  89. // stderr sink
  90. template<typename ConsoleMutex>
  91. SPDLOG_INLINE stderr_sink<ConsoleMutex>::stderr_sink()
  92. : stdout_sink_base<ConsoleMutex>(stderr)
  93. {}
  94. } // namespace sinks
  95. // factory methods
  96. template<typename Factory>
  97. SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_mt(const std::string &logger_name)
  98. {
  99. return Factory::template create<sinks::stdout_sink_mt>(logger_name);
  100. }
  101. template<typename Factory>
  102. SPDLOG_INLINE std::shared_ptr<logger> stdout_logger_st(const std::string &logger_name)
  103. {
  104. return Factory::template create<sinks::stdout_sink_st>(logger_name);
  105. }
  106. template<typename Factory>
  107. SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_mt(const std::string &logger_name)
  108. {
  109. return Factory::template create<sinks::stderr_sink_mt>(logger_name);
  110. }
  111. template<typename Factory>
  112. SPDLOG_INLINE std::shared_ptr<logger> stderr_logger_st(const std::string &logger_name)
  113. {
  114. return Factory::template create<sinks::stderr_sink_st>(logger_name);
  115. }
  116. } // namespace spdlog