You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2KB

  1. // Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
  2. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  3. #pragma once
  4. #include "spdlog/details/null_mutex.h"
  5. #include "spdlog/sinks/base_sink.h"
  6. #include <mutex>
  7. #include <ostream>
  8. namespace spdlog {
  9. namespace sinks {
  10. template<typename Mutex>
  11. class ostream_sink final : public base_sink<Mutex>
  12. {
  13. public:
  14. explicit ostream_sink(std::ostream &os, bool force_flush = false)
  15. : ostream_(os)
  16. , force_flush_(force_flush)
  17. {}
  18. ostream_sink(const ostream_sink &) = delete;
  19. ostream_sink &operator=(const ostream_sink &) = delete;
  20. protected:
  21. void sink_it_(const details::log_msg &msg) override
  22. {
  23. memory_buf_t formatted;
  24. base_sink<Mutex>::formatter_->format(msg, formatted);
  25. ostream_.write(formatted.data(), static_cast<std::streamsize>(formatted.size()));
  26. if (force_flush_)
  27. {
  28. ostream_.flush();
  29. }
  30. }
  31. void flush_() override
  32. {
  33. ostream_.flush();
  34. }
  35. std::ostream &ostream_;
  36. bool force_flush_;
  37. };
  38. using ostream_sink_mt = ostream_sink<std::mutex>;
  39. using ostream_sink_st = ostream_sink<details::null_mutex>;
  40. } // namespace sinks
  41. } // namespace spdlog