Skip to content

Commit b27afb7

Browse files
committed
Merge bitcoin/bitcoin#29833: i2p: fix and improve logs
7d3662f i2p: fix log when an interruption happens during `Accept` (brunoerg) 3d3a83f i2p: log errors properly according to their severity (brunoerg) Pull request description: This PR improves and fixes i2p logs (joint work with vasild). - It replaces `LogPrint` to `LogPrintLevel` so we can log according to the severity. - Fix log when interruption happens during `Accept`. Before this PR, when an interruption happens, it just logs "Error accepting:", no reason is logged as it does for other situations. This PR changes it to log "Accept interrupted". - Log errors according to the severity. Stuff like creating SAM session, destroying SAM session, etc... are logged as 'debug'. ACKs for top commit: achow101: ACK 7d3662f marcofleon: ACK 7d3662f. vasild: ACK 7d3662f Tree-SHA512: 1c3d92108dbc22833f37a78e18b4efd723433d10f28166d17c74eab884cd97e908b4e0a0908fd16288df895eb2eb480f781de37b2ec6a6d414abfb71e0c86fe2
2 parents 9ac4f69 + 7d3662f commit b27afb7

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

src/i2p.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ bool Session::Listen(Connection& conn)
148148
conn.sock = StreamAccept();
149149
return true;
150150
} catch (const std::runtime_error& e) {
151-
Log("Error listening: %s", e.what());
151+
LogPrintLevel(BCLog::I2P, BCLog::Level::Error, "Couldn't listen: %s\n", e.what());
152152
CheckControlSock();
153153
}
154154
return false;
@@ -204,7 +204,11 @@ bool Session::Accept(Connection& conn)
204204
return true;
205205
}
206206

207-
Log("Error accepting%s: %s", disconnect ? " (will close the session)" : "", errmsg);
207+
if (*m_interrupt) {
208+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Accept was interrupted\n");
209+
} else {
210+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Error accepting%s: %s\n", disconnect ? " (will close the session)" : "", errmsg);
211+
}
208212
if (disconnect) {
209213
LOCK(m_mutex);
210214
Disconnect();
@@ -219,7 +223,7 @@ bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error)
219223
// Refuse connecting to arbitrary ports. We don't specify any destination port to the SAM proxy
220224
// when connecting (SAM 3.1 does not use ports) and it forces/defaults it to I2P_SAM31_PORT.
221225
if (to.GetPort() != I2P_SAM31_PORT) {
222-
Log("Error connecting to %s, connection refused due to arbitrary port %s", to.ToStringAddrPort(), to.GetPort());
226+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Error connecting to %s, connection refused due to arbitrary port %s\n", to.ToStringAddrPort(), to.GetPort());
223227
proxy_error = false;
224228
return false;
225229
}
@@ -267,7 +271,7 @@ bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error)
267271

268272
throw std::runtime_error(strprintf("\"%s\"", connect_reply.full));
269273
} catch (const std::runtime_error& e) {
270-
Log("Error connecting to %s: %s", to.ToStringAddrPort(), e.what());
274+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Error connecting to %s: %s\n", to.ToStringAddrPort(), e.what());
271275
CheckControlSock();
272276
return false;
273277
}
@@ -285,12 +289,6 @@ std::string Session::Reply::Get(const std::string& key) const
285289
return pos->second.value();
286290
}
287291

288-
template <typename... Args>
289-
void Session::Log(const std::string& fmt, const Args&... args) const
290-
{
291-
LogPrint(BCLog::I2P, "%s\n", tfm::format(fmt, args...));
292-
}
293-
294292
Session::Reply Session::SendRequestAndGetReply(const Sock& sock,
295293
const std::string& request,
296294
bool check_result_ok) const
@@ -346,7 +344,7 @@ void Session::CheckControlSock()
346344

347345
std::string errmsg;
348346
if (m_control_sock && !m_control_sock->IsConnected(errmsg)) {
349-
Log("Control socket error: %s", errmsg);
347+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Control socket error: %s\n", errmsg);
350348
Disconnect();
351349
}
352350
}
@@ -416,7 +414,7 @@ void Session::CreateIfNotCreatedAlready()
416414
const auto session_type = m_transient ? "transient" : "persistent";
417415
const auto session_id = GetRandHash().GetHex().substr(0, 10); // full is overkill, too verbose in the logs
418416

419-
Log("Creating %s SAM session %s with %s", session_type, session_id, m_control_host.ToString());
417+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Creating %s SAM session %s with %s\n", session_type, session_id, m_control_host.ToString());
420418

421419
auto sock = Hello();
422420

@@ -453,7 +451,7 @@ void Session::CreateIfNotCreatedAlready()
453451
m_session_id = session_id;
454452
m_control_sock = std::move(sock);
455453

456-
Log("%s SAM session %s created, my address=%s",
454+
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "%s SAM session %s created, my address=%s\n",
457455
Capitalize(session_type),
458456
m_session_id,
459457
m_my_addr.ToStringAddrPort());
@@ -484,9 +482,9 @@ void Session::Disconnect()
484482
{
485483
if (m_control_sock) {
486484
if (m_session_id.empty()) {
487-
Log("Destroying incomplete SAM session");
485+
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "Destroying incomplete SAM session\n");
488486
} else {
489-
Log("Destroying SAM session %s", m_session_id);
487+
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "Destroying SAM session %s\n", m_session_id);
490488
}
491489
m_control_sock.reset();
492490
}

src/i2p.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,6 @@ class Session
156156
std::string Get(const std::string& key) const;
157157
};
158158

159-
/**
160-
* Log a message in the `BCLog::I2P` category.
161-
* @param[in] fmt printf(3)-like format string.
162-
* @param[in] args printf(3)-like arguments that correspond to `fmt`.
163-
*/
164-
template <typename... Args>
165-
void Log(const std::string& fmt, const Args&... args) const;
166-
167159
/**
168160
* Send request and get a reply from the SAM proxy.
169161
* @param[in] sock A socket that is connected to the SAM proxy.

0 commit comments

Comments
 (0)