output.cpp 12 KB

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