From 9a02c267fdf10ce86f0eb9ca41a13b7256bf7cf0 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Tue, 9 Apr 2024 20:22:42 +0300 Subject: [PATCH] Make user feedback during ssh login less verbose --- src/vtpty/SshSession.cpp | 28 +++++++++++++++++++--------- src/vtpty/SshSession.h | 8 ++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/vtpty/SshSession.cpp b/src/vtpty/SshSession.cpp index 357750d1d3..6d5e9abbbf 100644 --- a/src/vtpty/SshSession.cpp +++ b/src/vtpty/SshSession.cpp @@ -612,9 +612,9 @@ void SshSession::processState() void SshSession::start() { if (_config.port == 22) - logInfo("Starting SSH session to host: {}@{}", _config.username, _config.hostname); + logInfoWithInject("Starting SSH session to host: {}@{}", _config.username, _config.hostname); else - logInfo("Starting SSH session to host: {}@{}:{}", _config.username, _config.hostname, _config.port); + logInfoWithInject("Starting SSH session to host: {}@{}:{}", _config.username, _config.hostname, _config.port); assert(_state == State::Initial); // auto const _ = std::lock_guard { _mutex }; @@ -897,16 +897,26 @@ void SshSession::injectRead(std::string_view buf) _injectCV.notify_all(); } +void SshSession::logInject(std::string_view message) const +{ + const_cast(this)->injectRead(fmt::format("\U0001F511 \033[1;33m{}\033[m\r\n", message)); +} + void SshSession::logInfo(std::string_view message) const { sshLog()("{}", message); - const_cast(this)->injectRead(fmt::format("\U0001F511 \033[1;33m{}\033[m\r\n", message)); } +void SshSession::logInfoWithInject(std::string_view message) const +{ + logInfo(message); + logInject(message); +} + + void SshSession::logError(std::string_view message) const { errorLog()("{}", message); - const_cast(this)->injectRead(fmt::format("\U0001F511 \033[1;31m{}\033[m\r\n", message)); } bool SshSession::connect(std::string_view host, int port) @@ -968,9 +978,9 @@ bool SshSession::connect(std::string_view host, int port) auto const addrAndPort = port == 22 ? std::string(addrStr) : fmt::format("{}:{}", addrStr, port); if (host != addrStr) - logInfo("Connected to {} ({})", host, addrAndPort); + logInfoWithInject("Connected to {} ({})", host, addrAndPort); else - logInfo("Connected to {}", addrAndPort); + logInfoWithInject("Connected to {}", addrAndPort); return true; } @@ -1196,7 +1206,7 @@ void SshSession::authenticateWithPrivateKey() return; } - logInfo("Successfully authenticated with private key."); + logInfoWithInject("Successfully authenticated with private key."); setState(State::OpenChannel); } @@ -1234,7 +1244,7 @@ void SshSession::authenticateWithPassword() return; } - logInfo("Successfully authenticated with password."); + logInfoWithInject("Successfully authenticated with password."); setState(State::OpenChannel); } @@ -1289,7 +1299,7 @@ bool SshSession::authenticateWithAgent() } if (rc == LIBSSH2_ERROR_NONE) { - logInfo("Successfully authenticated with SSH agent with identity: {}", identity->comment); + logInfoWithInject("Successfully authenticated with SSH agent with identity: {}", identity->comment); setState(State::OpenChannel); return true; } diff --git a/src/vtpty/SshSession.h b/src/vtpty/SshSession.h index 667f5e460a..8d383f41e9 100644 --- a/src/vtpty/SshSession.h +++ b/src/vtpty/SshSession.h @@ -119,6 +119,14 @@ class SshSession final: public Pty void setState(State nextState); void logInfo(std::string_view message) const; + void logInject(std::string_view message) const; + void logInfoWithInject(std::string_view message) const; + + template + void logInfoWithInject(fmt::format_string fmt, Args&&... args) const + { + logInfoWithInject(fmt::format(fmt, std::forward(args)...)); + } template void logInfo(fmt::format_string fmt, Args&&... args) const