ostream.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Formatting library for C++ - std::ostream support
  2. //
  3. // Copyright (c) 2012 - present, Victor Zverovich
  4. // All rights reserved.
  5. //
  6. // For the license information refer to format.h.
  7. #ifndef FMT_OSTREAM_H_
  8. #define FMT_OSTREAM_H_
  9. #ifndef FMT_MODULE
  10. # include <fstream> // std::filebuf
  11. #endif
  12. #ifdef _WIN32
  13. # ifdef __GLIBCXX__
  14. # include <ext/stdio_filebuf.h>
  15. # include <ext/stdio_sync_filebuf.h>
  16. # endif
  17. # include <io.h>
  18. #endif
  19. #include "chrono.h" // formatbuf
  20. #ifdef _MSVC_STL_UPDATE
  21. # define FMT_MSVC_STL_UPDATE _MSVC_STL_UPDATE
  22. #elif defined(_MSC_VER) && _MSC_VER < 1912 // VS 15.5
  23. # define FMT_MSVC_STL_UPDATE _MSVC_LANG
  24. #else
  25. # define FMT_MSVC_STL_UPDATE 0
  26. #endif
  27. FMT_BEGIN_NAMESPACE
  28. namespace detail {
  29. // Generate a unique explicit instantion in every translation unit using a tag
  30. // type in an anonymous namespace.
  31. namespace {
  32. struct file_access_tag {};
  33. } // namespace
  34. template <typename Tag, typename BufType, FILE* BufType::*FileMemberPtr>
  35. class file_access {
  36. friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; }
  37. };
  38. #if FMT_MSVC_STL_UPDATE
  39. template class file_access<file_access_tag, std::filebuf,
  40. &std::filebuf::_Myfile>;
  41. auto get_file(std::filebuf&) -> FILE*;
  42. #endif
  43. // Write the content of buf to os.
  44. // It is a separate function rather than a part of vprint to simplify testing.
  45. template <typename Char>
  46. void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
  47. const Char* buf_data = buf.data();
  48. using unsigned_streamsize = make_unsigned_t<std::streamsize>;
  49. unsigned_streamsize size = buf.size();
  50. unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
  51. do {
  52. unsigned_streamsize n = size <= max_size ? size : max_size;
  53. os.write(buf_data, static_cast<std::streamsize>(n));
  54. buf_data += n;
  55. size -= n;
  56. } while (size != 0);
  57. }
  58. template <typename T> struct streamed_view {
  59. const T& value;
  60. };
  61. } // namespace detail
  62. // Formats an object of type T that has an overloaded ostream operator<<.
  63. template <typename Char>
  64. struct basic_ostream_formatter : formatter<basic_string_view<Char>, Char> {
  65. void set_debug_format() = delete;
  66. template <typename T, typename Context>
  67. auto format(const T& value, Context& ctx) const -> decltype(ctx.out()) {
  68. auto buffer = basic_memory_buffer<Char>();
  69. auto&& formatbuf = detail::formatbuf<std::basic_streambuf<Char>>(buffer);
  70. auto&& output = std::basic_ostream<Char>(&formatbuf);
  71. output.imbue(std::locale::classic()); // The default is always unlocalized.
  72. output << value;
  73. output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
  74. return formatter<basic_string_view<Char>, Char>::format(
  75. {buffer.data(), buffer.size()}, ctx);
  76. }
  77. };
  78. using ostream_formatter = basic_ostream_formatter<char>;
  79. template <typename T, typename Char>
  80. struct formatter<detail::streamed_view<T>, Char>
  81. : basic_ostream_formatter<Char> {
  82. template <typename Context>
  83. auto format(detail::streamed_view<T> view, Context& ctx) const
  84. -> decltype(ctx.out()) {
  85. return basic_ostream_formatter<Char>::format(view.value, ctx);
  86. }
  87. };
  88. /**
  89. * Returns a view that formats `value` via an ostream `operator<<`.
  90. *
  91. * **Example**:
  92. *
  93. * fmt::print("Current thread id: {}\n",
  94. * fmt::streamed(std::this_thread::get_id()));
  95. */
  96. template <typename T>
  97. constexpr auto streamed(const T& value) -> detail::streamed_view<T> {
  98. return {value};
  99. }
  100. inline void vprint(std::ostream& os, string_view fmt, format_args args) {
  101. auto buffer = memory_buffer();
  102. detail::vformat_to(buffer, fmt, args);
  103. FILE* f = nullptr;
  104. #if FMT_MSVC_STL_UPDATE && FMT_USE_RTTI
  105. if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
  106. f = detail::get_file(*buf);
  107. #elif defined(_WIN32) && defined(__GLIBCXX__) && FMT_USE_RTTI
  108. auto* rdbuf = os.rdbuf();
  109. if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
  110. f = sfbuf->file();
  111. else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf<char>*>(rdbuf))
  112. f = fbuf->file();
  113. #endif
  114. #ifdef _WIN32
  115. if (f) {
  116. int fd = _fileno(f);
  117. if (_isatty(fd)) {
  118. os.flush();
  119. if (detail::write_console(fd, {buffer.data(), buffer.size()})) return;
  120. }
  121. }
  122. #endif
  123. detail::ignore_unused(f);
  124. detail::write_buffer(os, buffer);
  125. }
  126. /**
  127. * Prints formatted data to the stream `os`.
  128. *
  129. * **Example**:
  130. *
  131. * fmt::print(cerr, "Don't {}!", "panic");
  132. */
  133. FMT_EXPORT template <typename... T>
  134. void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
  135. fmt::vargs<T...> vargs = {{args...}};
  136. if (detail::use_utf8) return vprint(os, fmt.str, vargs);
  137. auto buffer = memory_buffer();
  138. detail::vformat_to(buffer, fmt.str, vargs);
  139. detail::write_buffer(os, buffer);
  140. }
  141. FMT_EXPORT template <typename... T>
  142. void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
  143. fmt::print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
  144. }
  145. FMT_END_NAMESPACE
  146. #endif // FMT_OSTREAM_H_