ostream.h 5.2 KB

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