Skip to content

Commit 3d3a83f

Browse files
brunoergvasild
andcommitted
i2p: log errors properly according to their severity
Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
1 parent a7bc9b7 commit 3d3a83f

File tree

2 files changed

+9
-23
lines changed

2 files changed

+9
-23
lines changed

src/i2p.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ bool Session::Listen(Connection& conn)
146146
conn.sock = StreamAccept();
147147
return true;
148148
} catch (const std::runtime_error& e) {
149-
Log("Error listening: %s", e.what());
149+
LogPrintLevel(BCLog::I2P, BCLog::Level::Error, "Couldn't listen: %s\n", e.what());
150150
CheckControlSock();
151151
}
152152
return false;
@@ -202,7 +202,7 @@ bool Session::Accept(Connection& conn)
202202
return true;
203203
}
204204

205-
Log("Error accepting%s: %s", disconnect ? " (will close the session)" : "", errmsg);
205+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Error accepting%s: %s\n", disconnect ? " (will close the session)" : "", errmsg);
206206
if (disconnect) {
207207
LOCK(m_mutex);
208208
Disconnect();
@@ -217,7 +217,7 @@ bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error)
217217
// Refuse connecting to arbitrary ports. We don't specify any destination port to the SAM proxy
218218
// when connecting (SAM 3.1 does not use ports) and it forces/defaults it to I2P_SAM31_PORT.
219219
if (to.GetPort() != I2P_SAM31_PORT) {
220-
Log("Error connecting to %s, connection refused due to arbitrary port %s", to.ToStringAddrPort(), to.GetPort());
220+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Error connecting to %s, connection refused due to arbitrary port %s\n", to.ToStringAddrPort(), to.GetPort());
221221
proxy_error = false;
222222
return false;
223223
}
@@ -265,7 +265,7 @@ bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error)
265265

266266
throw std::runtime_error(strprintf("\"%s\"", connect_reply.full));
267267
} catch (const std::runtime_error& e) {
268-
Log("Error connecting to %s: %s", to.ToStringAddrPort(), e.what());
268+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Error connecting to %s: %s\n", to.ToStringAddrPort(), e.what());
269269
CheckControlSock();
270270
return false;
271271
}
@@ -283,12 +283,6 @@ std::string Session::Reply::Get(const std::string& key) const
283283
return pos->second.value();
284284
}
285285

286-
template <typename... Args>
287-
void Session::Log(const std::string& fmt, const Args&... args) const
288-
{
289-
LogPrint(BCLog::I2P, "%s\n", tfm::format(fmt, args...));
290-
}
291-
292286
Session::Reply Session::SendRequestAndGetReply(const Sock& sock,
293287
const std::string& request,
294288
bool check_result_ok) const
@@ -344,7 +338,7 @@ void Session::CheckControlSock()
344338

345339
std::string errmsg;
346340
if (m_control_sock && !m_control_sock->IsConnected(errmsg)) {
347-
Log("Control socket error: %s", errmsg);
341+
LogPrintLevel(BCLog::I2P, BCLog::Level::Debug, "Control socket error: %s\n", errmsg);
348342
Disconnect();
349343
}
350344
}
@@ -414,7 +408,7 @@ void Session::CreateIfNotCreatedAlready()
414408
const auto session_type = m_transient ? "transient" : "persistent";
415409
const auto session_id = GetRandHash().GetHex().substr(0, 10); // full is overkill, too verbose in the logs
416410

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

419413
auto sock = Hello();
420414

@@ -451,7 +445,7 @@ void Session::CreateIfNotCreatedAlready()
451445
m_session_id = session_id;
452446
m_control_sock = std::move(sock);
453447

454-
Log("%s SAM session %s created, my address=%s",
448+
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "%s SAM session %s created, my address=%s\n",
455449
Capitalize(session_type),
456450
m_session_id,
457451
m_my_addr.ToStringAddrPort());
@@ -482,9 +476,9 @@ void Session::Disconnect()
482476
{
483477
if (m_control_sock) {
484478
if (m_session_id.empty()) {
485-
Log("Destroying incomplete SAM session");
479+
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "Destroying incomplete SAM session\n");
486480
} else {
487-
Log("Destroying SAM session %s", m_session_id);
481+
LogPrintLevel(BCLog::I2P, BCLog::Level::Info, "Destroying SAM session %s\n", m_session_id);
488482
}
489483
m_control_sock.reset();
490484
}

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)