diff --git a/src/BtHandshakeMessage.cc b/src/BtHandshakeMessage.cc index bd441fee..97a3ec9b 100644 --- a/src/BtHandshakeMessage.cc +++ b/src/BtHandshakeMessage.cc @@ -103,13 +103,10 @@ size_t BtHandshakeMessage::getMessageLength() { } std::string BtHandshakeMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), - "%s peerId=%s, reserved=%s", - NAME.c_str(), - util::percentEncode(peerId_, PEER_ID_LENGTH).c_str(), - util::toHex(reserved_, RESERVED_LENGTH).c_str()); - return buf; + return fmt("%s peerId=%s, reserved=%s", + NAME.c_str(), + util::percentEncode(peerId_, PEER_ID_LENGTH).c_str(), + util::toHex(reserved_, RESERVED_LENGTH).c_str()); } bool BtHandshakeMessage::isFastExtensionSupported() const { diff --git a/src/BtPieceMessage.cc b/src/BtPieceMessage.cc index 7b563699..2e0f0b3a 100644 --- a/src/BtPieceMessage.cc +++ b/src/BtPieceMessage.cc @@ -213,13 +213,11 @@ void BtPieceMessage::pushPieceData(off_t offset, size_t length) const std::string BtPieceMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "%s index=%lu, begin=%u, length=%lu", - NAME.c_str(), - static_cast(index_), - begin_, - static_cast(blockLength_)); - return buf; + return fmt("%s index=%lu, begin=%u, length=%lu", + NAME.c_str(), + static_cast(index_), + begin_, + static_cast(blockLength_)); } bool BtPieceMessage::checkPieceHash(const SharedHandle& piece) diff --git a/src/BtPortMessage.cc b/src/BtPortMessage.cc index 4f904245..27774b25 100644 --- a/src/BtPortMessage.cc +++ b/src/BtPortMessage.cc @@ -118,9 +118,7 @@ size_t BtPortMessage::getMessageLength() { } std::string BtPortMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "%s port=%u", NAME.c_str(), port_); - return buf; + return fmt("%s port=%u", NAME.c_str(), port_); } void BtPortMessage::setLocalNode(DHTNode* localNode) diff --git a/src/DHTAnnouncePeerMessage.cc b/src/DHTAnnouncePeerMessage.cc index 85c381a5..67f48d8e 100644 --- a/src/DHTAnnouncePeerMessage.cc +++ b/src/DHTAnnouncePeerMessage.cc @@ -129,13 +129,10 @@ void DHTAnnouncePeerMessage::setTokenTracker(DHTTokenTracker* tokenTracker) std::string DHTAnnouncePeerMessage::toStringOptional() const { - char buf[256]; - snprintf(buf, sizeof(buf), - "token=%s, info_hash=%s, tcpPort=%u", - util::toHex(token_).c_str(), - util::toHex(infoHash_, INFO_HASH_LENGTH).c_str(), - tcpPort_); - return buf; + return fmt("token=%s, info_hash=%s, tcpPort=%u", + util::toHex(token_).c_str(), + util::toHex(infoHash_, INFO_HASH_LENGTH).c_str(), + tcpPort_); } } // namespace aria2 diff --git a/src/DHTGetPeersReplyMessage.cc b/src/DHTGetPeersReplyMessage.cc index 7564df56..bb56c9de 100644 --- a/src/DHTGetPeersReplyMessage.cc +++ b/src/DHTGetPeersReplyMessage.cc @@ -158,13 +158,10 @@ void DHTGetPeersReplyMessage::accept(DHTMessageCallback* callback) std::string DHTGetPeersReplyMessage::toStringOptional() const { - char buf[256]; - snprintf(buf, sizeof(buf), - "token=%s, values=%lu, nodes=%lu", - util::toHex(token_).c_str(), - static_cast(values_.size()), - static_cast(closestKNodes_.size())); - return buf; + return fmt("token=%s, values=%lu, nodes=%lu", + util::toHex(token_).c_str(), + static_cast(values_.size()), + static_cast(closestKNodes_.size())); } } // namespace aria2 diff --git a/src/DHTNode.cc b/src/DHTNode.cc index 967bd9a8..4ef0f64a 100644 --- a/src/DHTNode.cc +++ b/src/DHTNode.cc @@ -117,15 +117,12 @@ void DHTNode::timeout() std::string DHTNode::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), - "DHTNode ID=%s, Host=%s(%u), Condition=%u, RTT=%u", - util::toHex(id_, DHT_ID_LENGTH).c_str(), - ipaddr_.c_str(), - port_, - condition_, - rtt_); - return buf; + return fmt("DHTNode ID=%s, Host=%s(%u), Condition=%u, RTT=%u", + util::toHex(id_, DHT_ID_LENGTH).c_str(), + ipaddr_.c_str(), + port_, + condition_, + rtt_); } void DHTNode::setID(const unsigned char* id) diff --git a/src/DHTQueryMessage.cc b/src/DHTQueryMessage.cc index 3f03fa97..308f7df0 100644 --- a/src/DHTQueryMessage.cc +++ b/src/DHTQueryMessage.cc @@ -68,17 +68,14 @@ bool DHTQueryMessage::isReply() const std::string DHTQueryMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), - "dht query %s TransactionID=%s Remote:%s(%u), id=%s, v=%s, %s", - getMessageType().c_str(), - util::toHex(getTransactionID()).c_str(), - getRemoteNode()->getIPAddress().c_str(), - getRemoteNode()->getPort(), - util::toHex(getRemoteNode()->getID(), DHT_ID_LENGTH).c_str(), - util::torrentPercentEncode(getVersion()).c_str(), - toStringOptional().c_str()); - return buf; + return fmt("dht query %s TransactionID=%s Remote:%s(%u), id=%s, v=%s, %s", + getMessageType().c_str(), + util::toHex(getTransactionID()).c_str(), + getRemoteNode()->getIPAddress().c_str(), + getRemoteNode()->getPort(), + util::toHex(getRemoteNode()->getID(), DHT_ID_LENGTH).c_str(), + util::torrentPercentEncode(getVersion()).c_str(), + toStringOptional().c_str()); } } // namespace aria2 diff --git a/src/DHTResponseMessage.cc b/src/DHTResponseMessage.cc index 906eef7d..952188f8 100644 --- a/src/DHTResponseMessage.cc +++ b/src/DHTResponseMessage.cc @@ -65,17 +65,14 @@ bool DHTResponseMessage::isReply() const std::string DHTResponseMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), - "dht response %s TransactionID=%s Remote:%s(%u), id=%s, v=%s, %s", - getMessageType().c_str(), - util::toHex(getTransactionID()).c_str(), - getRemoteNode()->getIPAddress().c_str(), - getRemoteNode()->getPort(), - util::toHex(getRemoteNode()->getID(), DHT_ID_LENGTH).c_str(), - util::torrentPercentEncode(getVersion()).c_str(), - toStringOptional().c_str()); - return buf; + return fmt("dht response %s TransactionID=%s Remote:%s(%u), id=%s, v=%s, %s", + getMessageType().c_str(), + util::toHex(getTransactionID()).c_str(), + getRemoteNode()->getIPAddress().c_str(), + getRemoteNode()->getPort(), + util::toHex(getRemoteNode()->getID(), DHT_ID_LENGTH).c_str(), + util::torrentPercentEncode(getVersion()).c_str(), + toStringOptional().c_str()); } } // namespace aria2 diff --git a/src/DHTUnknownMessage.cc b/src/DHTUnknownMessage.cc index 365f78d3..268b30a0 100644 --- a/src/DHTUnknownMessage.cc +++ b/src/DHTUnknownMessage.cc @@ -88,14 +88,10 @@ std::string DHTUnknownMessage::toString() const if(length_ < sampleLength) { sampleLength = length_; } - std::string sample(&data_[0], &data_[sampleLength]); - char buf[256]; - snprintf(buf, sizeof(buf), - "dht unknown Remote:%s(%u) length=%lu, first 8 bytes(hex)=%s", - ipaddr_.c_str(), port_, - static_cast(length_), - util::toHex(data_, sampleLength).c_str()); - return buf; + return fmt("dht unknown Remote:%s(%u) length=%lu, first 8 bytes(hex)=%s", + ipaddr_.c_str(), port_, + static_cast(length_), + util::toHex(data_, sampleLength).c_str()); } } // namespace aria2 diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc index 0fc22d4c..d96a9704 100644 --- a/src/FtpNegotiationCommand.cc +++ b/src/FtpNegotiationCommand.cc @@ -724,10 +724,8 @@ bool FtpNegotiationCommand::sendTunnelRequest() SharedHandle req(new Request()); // Construct fake URI in order to use HttpRequest // TODO Handle IPv6 address; it must be enclosed with []. - char fakeUriBuf[1024]; - snprintf(fakeUriBuf, sizeof(fakeUriBuf), "ftp://%s:%u", - dataConnAddr_.first.c_str(), dataConnAddr_.second); - req->setUri(fakeUriBuf); + req->setUri(fmt("ftp://%s:%u", + dataConnAddr_.first.c_str(), dataConnAddr_.second)); httpRequest->setRequest(req); httpRequest->setProxyRequest(createProxyRequest()); http_->sendProxyRequest(httpRequest); diff --git a/src/IndexBtMessage.cc b/src/IndexBtMessage.cc index 108d5840..8c680efd 100644 --- a/src/IndexBtMessage.cc +++ b/src/IndexBtMessage.cc @@ -60,11 +60,9 @@ size_t IndexBtMessage::getMessageLength() std::string IndexBtMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "%s index=%lu", - getName().c_str(), - static_cast(index_)); - return buf; + return fmt("%s index=%lu", + getName().c_str(), + static_cast(index_)); } } // namespace aria2 diff --git a/src/OptionHandlerImpl.cc b/src/OptionHandlerImpl.cc index 1f0d500c..72c25e5d 100644 --- a/src/OptionHandlerImpl.cc +++ b/src/OptionHandlerImpl.cc @@ -262,17 +262,13 @@ std::string FloatNumberOptionHandler::createPossibleValuesString() const if(min_ < 0) { valuesString += "*"; } else { - char buf[11]; - snprintf(buf, sizeof(buf), "%.1f", min_); - valuesString += buf; + valuesString += fmt("%.1f", min_); } valuesString += "-"; if(max_ < 0) { valuesString += "*"; } else { - char buf[11]; - snprintf(buf, sizeof(buf), "%.1f", max_); - valuesString += buf; + valuesString += fmt("%.1f", max_); } return valuesString; } diff --git a/src/Piece.cc b/src/Piece.cc index 29b4cade..27b352de 100644 --- a/src/Piece.cc +++ b/src/Piece.cc @@ -167,11 +167,9 @@ bool Piece::getAllMissingBlockIndexes } std::string Piece::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "piece: index=%lu, length=%lu", - static_cast(index_), - static_cast(length_)); - return buf; + return fmt("piece: index=%lu, length=%lu", + static_cast(index_), + static_cast(length_)); } void Piece::reconfigure(size_t length) diff --git a/src/RangeBtMessage.cc b/src/RangeBtMessage.cc index 51c75c2e..9e37e57d 100644 --- a/src/RangeBtMessage.cc +++ b/src/RangeBtMessage.cc @@ -72,14 +72,11 @@ size_t RangeBtMessage::getMessageLength() std::string RangeBtMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), - "%s index=%lu, begin=%u, length=%lu", - getName().c_str(), - static_cast(index_), - begin_, - static_cast(length_)); - return buf; + return fmt("%s index=%lu, begin=%u, length=%lu", + getName().c_str(), + static_cast(index_), + begin_, + static_cast(length_)); } } // namespace aria2 diff --git a/src/UTMetadataDataExtensionMessage.cc b/src/UTMetadataDataExtensionMessage.cc index e63264a3..fcbbbf3b 100644 --- a/src/UTMetadataDataExtensionMessage.cc +++ b/src/UTMetadataDataExtensionMessage.cc @@ -71,10 +71,8 @@ std::string UTMetadataDataExtensionMessage::getPayload() std::string UTMetadataDataExtensionMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "ut_metadata data piece=%lu", - static_cast(getIndex())); - return buf; + return fmt("ut_metadata data piece=%lu", + static_cast(getIndex())); } void UTMetadataDataExtensionMessage::doReceivedAction() diff --git a/src/UTMetadataRejectExtensionMessage.cc b/src/UTMetadataRejectExtensionMessage.cc index 8f65708a..887b44af 100644 --- a/src/UTMetadataRejectExtensionMessage.cc +++ b/src/UTMetadataRejectExtensionMessage.cc @@ -54,10 +54,8 @@ std::string UTMetadataRejectExtensionMessage::getPayload() std::string UTMetadataRejectExtensionMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "ut_metadata reject piece=%lu", - static_cast(getIndex())); - return buf; + return fmt("ut_metadata reject piece=%lu", + static_cast(getIndex())); } void UTMetadataRejectExtensionMessage::doReceivedAction() diff --git a/src/UTMetadataRequestExtensionMessage.cc b/src/UTMetadataRequestExtensionMessage.cc index 8a2fd496..04ace5a1 100644 --- a/src/UTMetadataRequestExtensionMessage.cc +++ b/src/UTMetadataRequestExtensionMessage.cc @@ -69,10 +69,8 @@ std::string UTMetadataRequestExtensionMessage::getPayload() std::string UTMetadataRequestExtensionMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "ut_metadata request piece=%lu", - static_cast(getIndex())); - return buf; + return fmt("ut_metadata request piece=%lu", + static_cast(getIndex())); } void UTMetadataRequestExtensionMessage::doReceivedAction() diff --git a/src/UTPexExtensionMessage.cc b/src/UTPexExtensionMessage.cc index f4cb0018..0e1446e0 100644 --- a/src/UTPexExtensionMessage.cc +++ b/src/UTPexExtensionMessage.cc @@ -121,11 +121,9 @@ UTPexExtensionMessage::createCompactPeerListAndFlag std::string UTPexExtensionMessage::toString() const { - char buf[256]; - snprintf(buf, sizeof(buf), "ut_pex added=%lu, dropped=%lu", - static_cast(freshPeers_.size()), - static_cast(droppedPeers_.size())); - return buf; + return fmt("ut_pex added=%lu, dropped=%lu", + static_cast(freshPeers_.size()), + static_cast(droppedPeers_.size())); } void UTPexExtensionMessage::doReceivedAction()