Skip to content

Commit 23e7fe8

Browse files
committed
Merge #12569: net: Increase signal-to-noise ratio in debug.log by adjusting log level when logging failed non-manual connect():s
cba2800 Increase signal-to-noise ratio in debug.log by adjusting log level when logging failed non-manual connect():s (practicalswift) Pull request description: Increase signal-to-noise ratio in `debug.log` by adjusting log level when logging failed non-manual `connect()`:s. Before this patch: ``` $ src/bitcoind -printtoconsole … 2018-02-28 18:42:51 UpdateTip: new best=0000000000005448b10a219683d34b770a28044e1cc421032dea1a79ff548948 height=1286903 version=0x20000000 log2_work=69.791313 tx=17408546 date='2018-02-28 18:42:46' progress=1.000000 cache=0.0MiB(173txo) 2018-02-28 18:37:52 connect() 10.11.21.34:18333 failed after select(): Connection refused (111) 2018-02-28 18:43:22 connect() to 10.11.43.14:18333 failed after select(): Network is unreachable (101) 2018-02-28 18:44:49 UpdateTip: new best=000000000000029a521ff2803e1441b09413b876accff5084a4cccf7747d798b height=1286904 version=0x20000000 log2_work=69.791345 tx=17408559 date='2018-02-28 18:44:51' progress=1.000000 cache=0.1MiB(502txo) 2018-02-28 18:46:54 connect() to [2001:0:9d38:78ff:1234:1234:1234:1234]:18333 failed: Network is unreachable (101) 2018-02-28 18:48:56 connect() to [2001:0:9d38:6aff:1234:1234:1234:1234]:18333 failed: Network is unreachable (101) 2018-02-28 18:49:11 UpdateTip: new best=000000000000000206b79eb235e5dd907b6369de0e5d764330bf40ec0d460311 height=1286905 version=0x20000000 log2_work=69.791377 tx=17408577 date='2018-02-28 18:49:12' progress=1.000000 cache=1.0MiB(5245txo) ``` After this patch: ``` $ src/bitcoind -printtoconsole … 2018-02-28 18:42:51 UpdateTip: new best=0000000000005448b10a219683d34b770a28044e1cc421032dea1a79ff548948 height=1286903 version=0x20000000 log2_work=69.791313 tx=17408546 date='2018-02-28 18:42:46' progress=1.000000 cache=0.0MiB(173txo) 2018-02-28 18:44:49 UpdateTip: new best=000000000000029a521ff2803e1441b09413b876accff5084a4cccf7747d798b height=1286904 version=0x20000000 log2_work=69.791345 tx=17408559 date='2018-02-28 18:44:51' progress=1.000000 cache=0.1MiB(502txo) 2018-02-28 18:49:11 UpdateTip: new best=000000000000000206b79eb235e5dd907b6369de0e5d764330bf40ec0d460311 height=1286905 version=0x20000000 log2_work=69.791377 tx=17408577 date='2018-02-28 18:49:12' progress=1.000000 cache=1.0MiB(5245txo) ``` Please note that "manual `connect()`:s" (invoked via `-connect`, `-proxy` or `addnode`) are still reported at the default log level as these messages are likely to be relevant to end-users: ``` $ src/bitcoind -printtoconsole -connect=127.0.0.1:1234 … 2018-02-28 18:31:13 connect() to 127.0.0.1:1234 failed after select(): Connection refused (111) $ src/bitcoind -printtoconsole -proxy=127.0.0.1:1234 … 2018-02-28 18:32:32 connect() to 127.0.0.1:1234 failed after select(): Connection refused (111) $ src/bitcoind -printtoconsole & $ src/bitcoin-cli addnode 127.0.0.1:1234 onetry … 2018-02-28 18:33:40 connect() to 127.0.0.1:1234 failed after select(): Connection refused (111) ``` Tree-SHA512: 92e3c1e4b54ce8ccdd7ec31de147c8505710cd799ceb2bbc8576a086709967802403c9184df364b3cfa59bd98859f6ac8feb27fb09b9324194c6c47a042fc6d3
2 parents e625548 + cba2800 commit 23e7fe8

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

src/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ static CAddress GetBindAddress(SOCKET sock)
368368
return addr_bind;
369369
}
370370

371-
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
371+
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, bool manual_connection)
372372
{
373373
if (pszDest == nullptr) {
374374
if (IsLocal(addrConnect))
@@ -432,7 +432,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
432432
if (hSocket == INVALID_SOCKET) {
433433
return nullptr;
434434
}
435-
connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout);
435+
connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout, manual_connection);
436436
}
437437
if (!proxyConnectionFailed) {
438438
// If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
@@ -1992,7 +1992,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19921992
} else if (FindNode(std::string(pszDest)))
19931993
return;
19941994

1995-
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
1995+
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, manual_connection);
19961996

19971997
if (!pnode)
19981998
return;

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class CConnman
338338
CNode* FindNode(const CService& addr);
339339

340340
bool AttemptToEvictConnection();
341-
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure);
341+
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, bool manual_connection);
342342
bool IsWhitelistedRange(const CNetAddr &addr);
343343

344344
void DeleteNode(CNode* pnode);

src/netbase.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sync.h>
1010
#include <uint256.h>
1111
#include <random.h>
12+
#include <tinyformat.h>
1213
#include <util.h>
1314
#include <utilstrencodings.h>
1415

@@ -468,7 +469,17 @@ SOCKET CreateSocket(const CService &addrConnect)
468469
return hSocket;
469470
}
470471

471-
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, int nTimeout)
472+
template<typename... Args>
473+
static void LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args) {
474+
std::string error_message = tfm::format(fmt, args...);
475+
if (manual_connection) {
476+
LogPrintf("%s\n", error_message);
477+
} else {
478+
LogPrint(BCLog::NET, "%s\n", error_message);
479+
}
480+
}
481+
482+
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, int nTimeout, bool manual_connection)
472483
{
473484
struct sockaddr_storage sockaddr;
474485
socklen_t len = sizeof(sockaddr);
@@ -513,7 +524,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
513524
}
514525
if (nRet != 0)
515526
{
516-
LogPrintf("connect() to %s failed after select(): %s\n", addrConnect.ToString(), NetworkErrorString(nRet));
527+
LogConnectFailure(manual_connection, "connect() to %s failed after select(): %s", addrConnect.ToString(), NetworkErrorString(nRet));
517528
return false;
518529
}
519530
}
@@ -523,7 +534,7 @@ bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocket, i
523534
else
524535
#endif
525536
{
526-
LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
537+
LogConnectFailure(manual_connection, "connect() to %s failed: %s", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
527538
return false;
528539
}
529540
}
@@ -581,7 +592,7 @@ bool IsProxy(const CNetAddr &addr) {
581592
bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocket, int nTimeout, bool *outProxyConnectionFailed)
582593
{
583594
// first connect to proxy server
584-
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) {
595+
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout, true)) {
585596
if (outProxyConnectionFailed)
586597
*outProxyConnectionFailed = true;
587598
return false;
@@ -601,6 +612,7 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int
601612
}
602613
return true;
603614
}
615+
604616
bool LookupSubNet(const char* pszName, CSubNet& ret)
605617
{
606618
std::string strSubnet(pszName);

src/netbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault,
5252
CService LookupNumeric(const char *pszName, int portDefault = 0);
5353
bool LookupSubNet(const char *pszName, CSubNet& subnet);
5454
SOCKET CreateSocket(const CService &addrConnect);
55-
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocketRet, int nTimeout);
55+
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocketRet, int nTimeout, bool manual_connection);
5656
bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
5757
/** Return readable error string for a network error code */
5858
std::string NetworkErrorString(int err);

0 commit comments

Comments
 (0)