#1183 Disable stdin handler if not available.

This commit is contained in:
XMRig 2019-09-21 03:22:19 +07:00
parent 7a1ff6bfed
commit e57798360f
5 changed files with 100 additions and 14 deletions

View file

@ -24,7 +24,7 @@
*/
#include <stdio.h>
#include <cstdio>
#include "base/tools/Handle.h"
@ -32,9 +32,13 @@
#include "base/io/log/Log.h"
xmrig::ConsoleLog::ConsoleLog() :
m_stream(nullptr)
xmrig::ConsoleLog::ConsoleLog()
{
if (!isSupported()) {
Log::colors = false;
return;
}
m_tty = new uv_tty_t;
if (uv_tty_init(uv_default_loop(), m_tty, 1, 0) < 0) {
@ -66,7 +70,7 @@ xmrig::ConsoleLog::~ConsoleLog()
void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool colors)
{
if (Log::colors != colors) {
if (!m_tty || Log::colors != colors) {
return;
}
@ -86,12 +90,18 @@ void xmrig::ConsoleLog::print(int, const char *line, size_t, size_t size, bool c
}
bool xmrig::ConsoleLog::isSupported() const
{
const uv_handle_type type = uv_guess_handle(1);
return type == UV_TTY || type == UV_NAMED_PIPE;
}
bool xmrig::ConsoleLog::isWritable() const
{
if (!m_stream || uv_is_writable(m_stream) != 1) {
return false;
}
const uv_handle_type type = uv_guess_handle(1);
return type == UV_TTY || type == UV_NAMED_PIPE;
return isSupported();
}

View file

@ -27,11 +27,12 @@
#define XMRIG_CONSOLELOG_H
typedef struct uv_stream_s uv_stream_t;
typedef struct uv_tty_s uv_tty_t;
using uv_stream_t = struct uv_stream_s;
using uv_tty_t = struct uv_tty_s;
#include "base/kernel/interfaces/ILogBackend.h"
#include "base/tools/Object.h"
namespace xmrig {
@ -40,6 +41,8 @@ namespace xmrig {
class ConsoleLog : public ILogBackend
{
public:
XMRIG_DISABLE_COPY_MOVE(ConsoleLog)
ConsoleLog();
~ConsoleLog() override;
@ -47,10 +50,11 @@ protected:
void print(int level, const char *line, size_t offset, size_t size, bool colors) override;
private:
bool isSupported() const;
bool isWritable() const;
uv_stream_t *m_stream;
uv_tty_t *m_tty;
uv_stream_t *m_stream = nullptr;
uv_tty_t *m_tty = nullptr;
};