diff --git a/src/log/ConsoleLog.cpp b/src/log/ConsoleLog.cpp index 7b0445907..6439eb07a 100644 --- a/src/log/ConsoleLog.cpp +++ b/src/log/ConsoleLog.cpp @@ -40,7 +40,10 @@ ConsoleLog::ConsoleLog(bool colors) : m_colors(colors) { - uv_tty_init(uv_default_loop(), &m_tty, 1, 0); + if (uv_tty_init(uv_default_loop(), &m_tty, 1, 0) < 0) { + return; + } + uv_tty_set_mode(&m_tty, UV_TTY_MODE_NORMAL); # ifdef WIN32 @@ -58,6 +61,10 @@ ConsoleLog::ConsoleLog(bool colors) : void ConsoleLog::message(int level, const char* fmt, va_list args) { + if (!isWritable()) { + return; + } + time_t now = time(nullptr); tm stime; @@ -112,6 +119,10 @@ void ConsoleLog::message(int level, const char* fmt, va_list args) void ConsoleLog::text(const char* fmt, va_list args) { + if (!isWritable()) { + return; + } + char *buf = new char[64 + strlen(fmt) + 2]; sprintf(buf, "%s%s\n", fmt, m_colors ? Log::kCL_N : ""); @@ -120,6 +131,12 @@ void ConsoleLog::text(const char* fmt, va_list args) } +bool ConsoleLog::isWritable() const +{ + return uv_is_writable(reinterpret_cast(&m_tty)) == 1 && uv_guess_handle(1) == UV_TTY; +} + + void ConsoleLog::print(char *fmt, va_list args) { vsnprintf(m_buf, sizeof(m_buf) - 1, fmt, args); diff --git a/src/log/ConsoleLog.h b/src/log/ConsoleLog.h index b96b5e50c..e3d06e601 100644 --- a/src/log/ConsoleLog.h +++ b/src/log/ConsoleLog.h @@ -40,6 +40,7 @@ public: void text(const char *fmt, va_list args) override; private: + bool isWritable() const; void print(char *fmt, va_list args); bool m_colors;