Refactor addr logprint via template specialization

This commit is contained in:
Stephen Seo 2023-06-26 14:01:42 +09:00
parent 77d69cabbc
commit 972ec73e88
2 changed files with 42 additions and 83 deletions

View file

@ -212,12 +212,15 @@ private:
log_impl_next(type, args...); log_impl_next(type, args...);
} // }}} } // }}}
template<typename... Targs>
void log_impl_next(UDPC_LoggingType type, UDPC_IPV6_ADDR_TYPE addr, Targs... args) { // {{{
std::cerr << UDPC_atostr((UDPC_HContext)this, addr);
log_impl_next(type, args...);
} // }}}
public: public:
void update_impl(); void update_impl();
const char *conditional_atostr(UDPC_LoggingType logType,
UDPC_IPV6_ADDR_TYPE addr);
uint_fast32_t _contextIdentifier; uint_fast32_t _contextIdentifier;
char recvBuf[UDPC_PACKET_MAX_SIZE]; char recvBuf[UDPC_PACKET_MAX_SIZE];

View file

@ -336,8 +336,7 @@ void UDPC::Context::update_impl() {
UDPC_LoggingType::UDPC_ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to init ConnectionData instance (libsodium " "Failed to init ConnectionData instance (libsodium "
"init fail) while client establishing connection with ", "init fail) while client establishing connection with ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, event.conId.addr,
event.conId.addr),
" port ", " port ",
event.conId.port); event.conId.port);
continue; continue;
@ -363,8 +362,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO, UDPC_LoggingType::UDPC_INFO,
"Client initiating connection to ", "Client initiating connection to ",
conditional_atostr(UDPC_LoggingType::UDPC_INFO, event.conId.addr,
event.conId.addr),
" port ", " port ",
event.conId.port, event.conId.port,
" ..."); " ...");
@ -372,8 +370,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_WARNING, UDPC_LoggingType::UDPC_WARNING,
"Client initiate connection, already connected to peer ", "Client initiate connection, already connected to peer ",
conditional_atostr(UDPC_LoggingType::UDPC_WARNING, event.conId.addr,
event.conId.addr),
" port ", " port ",
event.conId.port); event.conId.port);
} }
@ -422,8 +419,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Timed out connection with ", "Timed out connection with ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
continue; continue;
@ -437,8 +433,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Switching to bad mode in connection with ", "Switching to bad mode in connection with ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
iter->second.flags.reset(1); iter->second.flags.reset(1);
@ -469,8 +464,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Switching to good mode in connection with ", "Switching to good mode in connection with ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
iter->second.flags.set(1); iter->second.flags.set(1);
@ -550,8 +544,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_DEBUG, UDPC_LoggingType::UDPC_DEBUG,
"Not queueing packet to ", "Not queueing packet to ",
conditional_atostr(UDPC_LoggingType::UDPC_DEBUG, next->receiver.addr,
next->receiver.addr),
", port = ", ", port = ",
next->receiver.port, next->receiver.port,
", connection's queue reached max size"); ", connection's queue reached max size");
@ -573,8 +566,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_WARNING, UDPC_LoggingType::UDPC_WARNING,
"Dropped queued packets to ", "Dropped queued packets to ",
conditional_atostr(UDPC_LoggingType::UDPC_WARNING, next->receiver.addr,
next->receiver.addr),
", port = ", ", port = ",
next->receiver.port, next->receiver.port,
" due to connection not existing"); " due to connection not existing");
@ -633,8 +625,7 @@ void UDPC::Context::update_impl() {
iter->second.sk) != 0) { iter->second.sk) != 0) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Failed to sign packet for peer ", "Failed to sign packet for peer ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port ", ", port ",
iter->second.port); iter->second.port);
continue; continue;
@ -668,8 +659,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send disconnect packet to ", "Failed to send disconnect packet to ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
continue; continue;
@ -774,8 +764,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to initiate connection to ", "Failed to send packet to initiate connection to ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
continue; continue;
@ -784,8 +773,7 @@ void UDPC::Context::update_impl() {
this, this,
UDPC_LoggingType::UDPC_INFO, UDPC_LoggingType::UDPC_INFO,
"Sent initiate connection to ", "Sent initiate connection to ",
conditional_atostr(UDPC_LoggingType::UDPC_INFO, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port, iter->second.port,
flags.test(2) && iter->second.flags.test(6) ? flags.test(2) && iter->second.flags.test(6) ?
@ -856,16 +844,14 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to initiate connection to ", "Failed to send packet to initiate connection to ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
continue; continue;
} }
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_DEBUG, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_DEBUG,
"Sent init pkt to client ", "Sent init pkt to client ",
conditional_atostr(UDPC_LoggingType::UDPC_DEBUG, destinationInfo.sin6_addr,
destinationInfo.sin6_addr),
", port ", iter->second.port); ", port ", iter->second.port);
} }
continue; continue;
@ -908,8 +894,7 @@ void UDPC::Context::update_impl() {
iter->second.sk) != 0) { iter->second.sk) != 0) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Failed to sign packet for peer ", "Failed to sign packet for peer ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port ", ", port ",
iter->second.port); iter->second.port);
continue; continue;
@ -943,8 +928,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send heartbeat packet to ", "Failed to send heartbeat packet to ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
continue; continue;
@ -1014,8 +998,7 @@ void UDPC::Context::update_impl() {
iter->second.sk) != 0) { iter->second.sk) != 0) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Failed to sign packet for peer ", "Failed to sign packet for peer ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port ", ", port ",
iter->second.port); iter->second.port);
std::free(pInfo.data); std::free(pInfo.data);
@ -1054,8 +1037,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to send packet to ", "Failed to send packet to ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, iter->first.addr,
iter->first.addr),
", port = ", ", port = ",
iter->second.port); iter->second.port);
std::free(pInfo.data); std::free(pInfo.data);
@ -1169,8 +1151,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Received packet is smaller than header, ignoring packet from ", "Received packet is smaller than header, ignoring packet from ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port)); ntohs(receivedData.sin6_port));
continue; continue;
@ -1185,8 +1166,7 @@ void UDPC::Context::update_impl() {
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Received packet has invalid protocol id, ignoring packet " "Received packet has invalid protocol id, ignoring packet "
"from ", "from ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port)); ntohs(receivedData.sin6_port));
continue; continue;
@ -1216,8 +1196,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Got connect packet of invalid size from ", "Got connect packet of invalid size from ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
", ignoring"); ", ignoring");
@ -1228,8 +1207,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Got non-connect packet of invalid size from ", "Got non-connect packet of invalid size from ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
", ignoring"); ", ignoring");
@ -1250,8 +1228,7 @@ void UDPC::Context::update_impl() {
default: default:
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING,
"Got invalid connect pktType from ", "Got invalid connect pktType from ",
conditional_atostr(UDPC_LoggingType::UDPC_WARNING, receivedData.sin6_addr,
receivedData.sin6_addr),
", port ", ntohs(receivedData.sin6_port)); ", port ", ntohs(receivedData.sin6_port));
continue; continue;
} }
@ -1265,8 +1242,7 @@ void UDPC::Context::update_impl() {
} else { } else {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING,
"Got invalid pktType from ", "Got invalid pktType from ",
conditional_atostr(UDPC_LoggingType::UDPC_WARNING, receivedData.sin6_addr,
receivedData.sin6_addr),
", port ", ntohs(receivedData.sin6_port)); ", port ", ntohs(receivedData.sin6_port));
continue; continue;
} }
@ -1285,8 +1261,7 @@ void UDPC::Context::update_impl() {
== UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) { == UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Client peer ", "Client peer ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, receivedData.sin6_addr,
receivedData.sin6_addr),
" port ", " port ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
" attempted connection with packet authentication " " attempted connection with packet authentication "
@ -1298,8 +1273,7 @@ void UDPC::Context::update_impl() {
== UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) { == UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Client peer ", "Client peer ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, receivedData.sin6_addr,
receivedData.sin6_addr),
" port ", " port ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
" attempted connection with packet authentication " " attempted connection with packet authentication "
@ -1332,8 +1306,7 @@ void UDPC::Context::update_impl() {
UDPC_LoggingType::UDPC_ERROR, UDPC_LoggingType::UDPC_ERROR,
"Failed to init ConnectionData instance (libsodium init" "Failed to init ConnectionData instance (libsodium init"
" fail) while server establishing connection with ", " fail) while server establishing connection with ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port)); ntohs(receivedData.sin6_port));
continue; continue;
@ -1399,8 +1372,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_INFO, UDPC_LoggingType::UDPC_INFO,
"Establishing connection with client ", "Establishing connection with client ",
conditional_atostr(UDPC_LoggingType::UDPC_INFO, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
", giving client id = ", newConnection.id, ", giving client id = ", newConnection.id,
@ -1436,8 +1408,7 @@ void UDPC::Context::update_impl() {
if(iter == conMap.end() || !iter->second.flags.test(3)) { if(iter == conMap.end() || !iter->second.flags.test(3)) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_DEBUG, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_DEBUG,
"client dropped pkt from ", "client dropped pkt from ",
conditional_atostr(UDPC_LoggingType::UDPC_DEBUG, receivedData.sin6_addr,
receivedData.sin6_addr),
", port ", ntohs(receivedData.sin6_port)); ", port ", ntohs(receivedData.sin6_port));
continue; continue;
} }
@ -1452,8 +1423,7 @@ void UDPC::Context::update_impl() {
// without auth (if fallback). // without auth (if fallback).
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Server peer ", "Server peer ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, receivedData.sin6_addr,
receivedData.sin6_addr),
" port ", " port ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
" attempted connection with packet authentication " " attempted connection with packet authentication "
@ -1465,8 +1435,7 @@ void UDPC::Context::update_impl() {
== UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) { == UDPC_AuthPolicy::UDPC_AUTH_POLICY_STRICT) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_ERROR,
"Server peer ", "Server peer ",
conditional_atostr(UDPC_LoggingType::UDPC_ERROR, receivedData.sin6_addr,
receivedData.sin6_addr),
" port ", " port ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
" attempted connection with packet authentication " " attempted connection with packet authentication "
@ -1502,8 +1471,7 @@ void UDPC::Context::update_impl() {
iter->second.peer_pk) != 0) { iter->second.peer_pk) != 0) {
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_WARNING,
"Failed to verify peer (server) ", "Failed to verify peer (server) ",
conditional_atostr(UDPC_LoggingType::UDPC_WARNING, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port)); ntohs(receivedData.sin6_port));
continue; continue;
@ -1529,8 +1497,7 @@ void UDPC::Context::update_impl() {
iter->second.flags.set(4); iter->second.flags.set(4);
UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_INFO, UDPC_CHECK_LOG(this, UDPC_LoggingType::UDPC_INFO,
"Established connection with server ", "Established connection with server ",
conditional_atostr(UDPC_LoggingType::UDPC_INFO, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
", got id = ", conID, ", got id = ", conID,
@ -1575,8 +1542,7 @@ void UDPC::Context::update_impl() {
this, this,
UDPC_LoggingType::UDPC_INFO, UDPC_LoggingType::UDPC_INFO,
"Failed to verify received packet from", "Failed to verify received packet from",
conditional_atostr(UDPC_LoggingType::UDPC_INFO, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
", ignoring"); ", ignoring");
@ -1594,8 +1560,7 @@ void UDPC::Context::update_impl() {
UDPC_CHECK_LOG(this, UDPC_CHECK_LOG(this,
UDPC_LoggingType::UDPC_VERBOSE, UDPC_LoggingType::UDPC_VERBOSE,
"Received valid packet from ", "Received valid packet from ",
conditional_atostr(UDPC_LoggingType::UDPC_VERBOSE, receivedData.sin6_addr,
receivedData.sin6_addr),
", port = ", ", port = ",
ntohs(receivedData.sin6_port), ntohs(receivedData.sin6_port),
", packet id = ", seqID, ", packet id = ", seqID,
@ -1842,15 +1807,6 @@ void UDPC::Context::update_impl() {
} while (true); } while (true);
} }
const char *UDPC::Context::conditional_atostr(UDPC_LoggingType logType,
UDPC_IPV6_ADDR_TYPE addr) {
if (willLog(logType)) {
return UDPC_atostr((UDPC_HContext)this, addr);
} else {
return "";
}
}
UDPC::Context *UDPC::verifyContext(UDPC_HContext ctx) { UDPC::Context *UDPC::verifyContext(UDPC_HContext ctx) {
if(ctx == nullptr) { if(ctx == nullptr) {
return nullptr; return nullptr;
@ -2076,7 +2032,7 @@ UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient, int isUsingLib
ctx->authPolicy.exchange(UDPC_AuthPolicy::UDPC_AUTH_POLICY_FALLBACK); ctx->authPolicy.exchange(UDPC_AuthPolicy::UDPC_AUTH_POLICY_FALLBACK);
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Got listen addr ", UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Got listen addr ",
ctx->conditional_atostr(UDPC_LoggingType::UDPC_INFO, listenId.addr)); listenId.addr);
if(isUsingLibsodium) { if(isUsingLibsodium) {
#ifdef UDPC_LIBSODIUM_ENABLED #ifdef UDPC_LIBSODIUM_ENABLED