output.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "output.h"
  2. #include <util/string/cast.h>
  3. #include "format.h"
  4. #include <util/memory/tempbuf.h>
  5. #include <util/generic/singleton.h>
  6. #include <util/generic/yexception.h>
  7. #include <util/charset/utf8.h>
  8. #include <util/charset/wide.h>
  9. #if defined(_android_)
  10. #include <util/system/dynlib.h>
  11. #include <util/system/guard.h>
  12. #include <util/system/mutex.h>
  13. #include <android/log.h>
  14. #endif
  15. #include <cerrno>
  16. #include <cstdio>
  17. #include <filesystem>
  18. #include <string_view>
  19. #if defined(_win_)
  20. #include <io.h>
  21. #endif
  22. constexpr size_t MAX_UTF8_BYTES = 4; // UTF-8-encoded code point takes between 1 and 4 bytes
  23. IOutputStream::IOutputStream() noexcept = default;
  24. IOutputStream::~IOutputStream() = default;
  25. void IOutputStream::DoFlush() {
  26. /*
  27. * do nothing
  28. */
  29. }
  30. void IOutputStream::DoFinish() {
  31. Flush();
  32. }
  33. void IOutputStream::DoWriteV(const TPart* parts, size_t count) {
  34. for (size_t i = 0; i < count; ++i) {
  35. const TPart& part = parts[i];
  36. DoWrite(part.buf, part.len);
  37. }
  38. }
  39. void IOutputStream::DoWriteC(char ch) {
  40. DoWrite(&ch, 1);
  41. }
  42. template <>
  43. void Out<wchar16>(IOutputStream& o, wchar16 ch) {
  44. const wchar32 w32ch = ReadSymbol(&ch, &ch + 1);
  45. size_t length;
  46. unsigned char buffer[MAX_UTF8_BYTES];
  47. WriteUTF8Char(w32ch, length, buffer);
  48. o.Write(buffer, length);
  49. }
  50. template <>
  51. void Out<wchar32>(IOutputStream& o, wchar32 ch) {
  52. size_t length;
  53. unsigned char buffer[MAX_UTF8_BYTES];
  54. WriteUTF8Char(ch, length, buffer);
  55. o.Write(buffer, length);
  56. }
  57. static void WriteString(IOutputStream& o, const wchar16* w, size_t n) {
  58. const size_t buflen = (n * MAX_UTF8_BYTES); // * 4 because the conversion functions can convert unicode character into maximum 4 bytes of UTF8
  59. TTempBuf buffer(buflen + 1);
  60. char* const data = buffer.Data();
  61. size_t written = 0;
  62. WideToUTF8(w, n, data, written);
  63. data[written] = 0;
  64. o.Write(data, written);
  65. }
  66. static void WriteString(IOutputStream& o, const wchar32* w, size_t n) {
  67. const size_t buflen = (n * MAX_UTF8_BYTES); // * 4 because the conversion functions can convert unicode character into maximum 4 bytes of UTF8
  68. TTempBuf buffer(buflen + 1);
  69. char* const data = buffer.Data();
  70. size_t written = 0;
  71. WideToUTF8(w, n, data, written);
  72. data[written] = 0;
  73. o.Write(data, written);
  74. }
  75. template <>
  76. void Out<TString>(IOutputStream& o, const TString& p) {
  77. o.Write(p.data(), p.size());
  78. }
  79. template <>
  80. void Out<std::string>(IOutputStream& o, const std::string& p) {
  81. o.Write(p.data(), p.length());
  82. }
  83. template <>
  84. void Out<std::string_view>(IOutputStream& o, const std::string_view& p) {
  85. o.Write(p.data(), p.length());
  86. }
  87. template <>
  88. void Out<std::u16string_view>(IOutputStream& o, const std::u16string_view& p) {
  89. WriteString(o, p.data(), p.length());
  90. }
  91. template <>
  92. void Out<std::u32string_view>(IOutputStream& o, const std::u32string_view& p) {
  93. WriteString(o, p.data(), p.length());
  94. }
  95. #ifndef USE_STL_SYSTEM
  96. // FIXME thegeorg@: remove #ifndef upon raising minimal macOS version to 10.15 in https://st.yandex-team.ru/DTCC-836
  97. template <>
  98. void Out<std::filesystem::path>(IOutputStream& o, const std::filesystem::path& p) {
  99. o.Write(p.string());
  100. }
  101. #endif
  102. template <>
  103. void Out<TStringBuf>(IOutputStream& o, const TStringBuf& p) {
  104. o.Write(p.data(), p.length());
  105. }
  106. template <>
  107. void Out<TWtringBuf>(IOutputStream& o, const TWtringBuf& p) {
  108. WriteString(o, p.data(), p.length());
  109. }
  110. template <>
  111. void Out<TUtf32StringBuf>(IOutputStream& o, const TUtf32StringBuf& p) {
  112. WriteString(o, p.data(), p.length());
  113. }
  114. template <>
  115. void Out<const wchar16*>(IOutputStream& o, const wchar16* w) {
  116. if (w) {
  117. WriteString(o, w, std::char_traits<wchar16>::length(w));
  118. } else {
  119. o.Write("(null)");
  120. }
  121. }
  122. template <>
  123. void Out<const wchar32*>(IOutputStream& o, const wchar32* w) {
  124. if (w) {
  125. WriteString(o, w, std::char_traits<wchar32>::length(w));
  126. } else {
  127. o.Write("(null)");
  128. }
  129. }
  130. template <>
  131. void Out<TUtf16String>(IOutputStream& o, const TUtf16String& w) {
  132. WriteString(o, w.c_str(), w.size());
  133. }
  134. template <>
  135. void Out<TUtf32String>(IOutputStream& o, const TUtf32String& w) {
  136. WriteString(o, w.c_str(), w.size());
  137. }
  138. #define DEF_CONV_DEFAULT(type) \
  139. template <> \
  140. void Out<type>(IOutputStream & o, type p) { \
  141. o << ToString(p); \
  142. }
  143. #define DEF_CONV_CHR(type) \
  144. template <> \
  145. void Out<type>(IOutputStream & o, type p) { \
  146. o.Write((char)p); \
  147. }
  148. #define DEF_CONV_NUM(type, len) \
  149. template <> \
  150. void Out<type>(IOutputStream & o, type p) { \
  151. char buf[len]; \
  152. o.Write(buf, ToString(p, buf, sizeof(buf))); \
  153. } \
  154. \
  155. template <> \
  156. void Out<volatile type>(IOutputStream & o, volatile type p) { \
  157. Out<type>(o, p); \
  158. }
  159. DEF_CONV_NUM(bool, 64)
  160. DEF_CONV_CHR(char)
  161. DEF_CONV_CHR(signed char)
  162. DEF_CONV_CHR(unsigned char)
  163. DEF_CONV_NUM(signed short, 64)
  164. DEF_CONV_NUM(signed int, 64)
  165. DEF_CONV_NUM(signed long int, 64)
  166. DEF_CONV_NUM(signed long long int, 64)
  167. DEF_CONV_NUM(unsigned short, 64)
  168. DEF_CONV_NUM(unsigned int, 64)
  169. DEF_CONV_NUM(unsigned long int, 64)
  170. DEF_CONV_NUM(unsigned long long int, 64)
  171. DEF_CONV_NUM(float, 512)
  172. DEF_CONV_NUM(double, 512)
  173. DEF_CONV_NUM(long double, 512)
  174. #if !defined(_YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION) || (_YNDX_LIBCXX_ENABLE_VECTOR_BOOL_COMPRESSION == 1)
  175. // TODO: acknowledge std::bitset::reference for both libc++ and libstdc++
  176. template <>
  177. void Out<typename std::vector<bool>::reference>(IOutputStream& o, const std::vector<bool>::reference& bit) {
  178. return Out<bool>(o, static_cast<bool>(bit));
  179. }
  180. #endif
  181. #ifndef TSTRING_IS_STD_STRING
  182. template <>
  183. void Out<TBasicCharRef<TString>>(IOutputStream& o, const TBasicCharRef<TString>& c) {
  184. o << static_cast<char>(c);
  185. }
  186. template <>
  187. void Out<TBasicCharRef<TUtf16String>>(IOutputStream& o, const TBasicCharRef<TUtf16String>& c) {
  188. o << static_cast<wchar16>(c);
  189. }
  190. template <>
  191. void Out<TBasicCharRef<TUtf32String>>(IOutputStream& o, const TBasicCharRef<TUtf32String>& c) {
  192. o << static_cast<wchar32>(c);
  193. }
  194. #endif
  195. template <>
  196. void Out<const void*>(IOutputStream& o, const void* t) {
  197. o << Hex(size_t(t));
  198. }
  199. template <>
  200. void Out<void*>(IOutputStream& o, void* t) {
  201. Out<const void*>(o, t);
  202. }
  203. using TNullPtr = decltype(nullptr);
  204. template <>
  205. void Out<TNullPtr>(IOutputStream& o, TTypeTraits<TNullPtr>::TFuncParam) {
  206. o << TStringBuf("nullptr");
  207. }
  208. #if defined(_android_)
  209. namespace {
  210. class TAndroidStdIOStreams {
  211. public:
  212. TAndroidStdIOStreams()
  213. : LogLibrary("liblog.so")
  214. , LogFuncPtr((TLogFuncPtr)LogLibrary.Sym("__android_log_write"))
  215. , Out(LogFuncPtr)
  216. , Err(LogFuncPtr)
  217. {
  218. }
  219. public:
  220. using TLogFuncPtr = void (*)(int, const char*, const char*);
  221. class TAndroidStdOutput: public IOutputStream {
  222. public:
  223. inline TAndroidStdOutput(TLogFuncPtr logFuncPtr) noexcept
  224. : Buffer()
  225. , LogFuncPtr(logFuncPtr)
  226. {
  227. }
  228. virtual ~TAndroidStdOutput() {
  229. }
  230. private:
  231. virtual void DoWrite(const void* buf, size_t len) override {
  232. with_lock (BufferMutex) {
  233. Buffer.Write(buf, len);
  234. }
  235. }
  236. virtual void DoFlush() override {
  237. with_lock (BufferMutex) {
  238. LogFuncPtr(ANDROID_LOG_DEBUG, GetTag(), Buffer.Data());
  239. Buffer.Clear();
  240. }
  241. }
  242. virtual const char* GetTag() const = 0;
  243. private:
  244. TMutex BufferMutex;
  245. TStringStream Buffer;
  246. TLogFuncPtr LogFuncPtr;
  247. };
  248. class TStdErr: public TAndroidStdOutput {
  249. public:
  250. TStdErr(TLogFuncPtr logFuncPtr)
  251. : TAndroidStdOutput(logFuncPtr)
  252. {
  253. }
  254. virtual ~TStdErr() {
  255. }
  256. private:
  257. virtual const char* GetTag() const override {
  258. return "stderr";
  259. }
  260. };
  261. class TStdOut: public TAndroidStdOutput {
  262. public:
  263. TStdOut(TLogFuncPtr logFuncPtr)
  264. : TAndroidStdOutput(logFuncPtr)
  265. {
  266. }
  267. virtual ~TStdOut() {
  268. }
  269. private:
  270. virtual const char* GetTag() const override {
  271. return "stdout";
  272. }
  273. };
  274. static bool Enabled;
  275. TDynamicLibrary LogLibrary; // field order is important, see constructor
  276. TLogFuncPtr LogFuncPtr;
  277. TStdOut Out;
  278. TStdErr Err;
  279. static inline TAndroidStdIOStreams& Instance() {
  280. return *SingletonWithPriority<TAndroidStdIOStreams, 4>();
  281. }
  282. };
  283. bool TAndroidStdIOStreams::Enabled = false;
  284. }
  285. #endif // _android_
  286. namespace {
  287. class TStdOutput: public IOutputStream {
  288. public:
  289. inline TStdOutput(FILE* f) noexcept
  290. : F_(f)
  291. {
  292. }
  293. ~TStdOutput() override = default;
  294. private:
  295. void DoWrite(const void* buf, size_t len) override {
  296. if (len != fwrite(buf, 1, len, F_)) {
  297. #if defined(_win_)
  298. // On Windows, if 'F_' is console -- 'fwrite' returns count of written characters.
  299. // If, for example, console output codepage is UTF-8, then returned value is
  300. // not equal to 'len'. So, we ignore some 'errno' values...
  301. if ((errno == 0 || errno == EINVAL || errno == EILSEQ) && _isatty(fileno(F_))) {
  302. return;
  303. }
  304. #endif
  305. ythrow TSystemError() << "write failed";
  306. }
  307. }
  308. void DoFlush() override {
  309. if (fflush(F_) != 0) {
  310. ythrow TSystemError() << "fflush failed";
  311. }
  312. }
  313. private:
  314. FILE* F_;
  315. };
  316. struct TStdIOStreams {
  317. struct TStdErr: public TStdOutput {
  318. inline TStdErr()
  319. : TStdOutput(stderr)
  320. {
  321. }
  322. ~TStdErr() override = default;
  323. };
  324. struct TStdOut: public TStdOutput {
  325. inline TStdOut()
  326. : TStdOutput(stdout)
  327. {
  328. }
  329. ~TStdOut() override = default;
  330. };
  331. TStdOut Out;
  332. TStdErr Err;
  333. static inline TStdIOStreams& Instance() {
  334. return *SingletonWithPriority<TStdIOStreams, 4>();
  335. }
  336. };
  337. }
  338. IOutputStream& NPrivate::StdErrStream() noexcept {
  339. #if defined(_android_)
  340. if (TAndroidStdIOStreams::Enabled) {
  341. return TAndroidStdIOStreams::Instance().Err;
  342. }
  343. #endif
  344. return TStdIOStreams::Instance().Err;
  345. }
  346. IOutputStream& NPrivate::StdOutStream() noexcept {
  347. #if defined(_android_)
  348. if (TAndroidStdIOStreams::Enabled) {
  349. return TAndroidStdIOStreams::Instance().Out;
  350. }
  351. #endif
  352. return TStdIOStreams::Instance().Out;
  353. }
  354. void RedirectStdioToAndroidLog(bool redirect) {
  355. #if defined(_android_)
  356. TAndroidStdIOStreams::Enabled = redirect;
  357. #else
  358. Y_UNUSED(redirect);
  359. #endif
  360. }