Commit 5295ccef authored by Victor Zverovich's avatar Victor Zverovich Committed by Facebook GitHub Bot

Migrate from Folly Format to fmt

Summary: Migrate a couple of uses of Folly Format to fmt.

Reviewed By: simpkins

Differential Revision: D20152350

fbshipit-source-id: fb9798f5ff458ee03e1c01f839dc1c55796d0eaa
parent 99728475
...@@ -16,9 +16,10 @@ ...@@ -16,9 +16,10 @@
#include <folly/File.h> #include <folly/File.h>
#include <fmt/core.h>
#include <folly/Exception.h> #include <folly/Exception.h>
#include <folly/FileUtil.h> #include <folly/FileUtil.h>
#include <folly/Format.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/portability/Fcntl.h> #include <folly/portability/Fcntl.h>
#include <folly/portability/SysFile.h> #include <folly/portability/SysFile.h>
...@@ -41,8 +42,7 @@ File::File(const char* name, int flags, mode_t mode) ...@@ -41,8 +42,7 @@ File::File(const char* name, int flags, mode_t mode)
: fd_(::open(name, flags, mode)), ownsFd_(false) { : fd_(::open(name, flags, mode)), ownsFd_(false) {
if (fd_ == -1) { if (fd_ == -1) {
throwSystemError( throwSystemError(
folly::format("open(\"{}\", {:#o}, 0{:#o}) failed", name, flags, mode) fmt::format("open(\"{}\", {:#o}, 0{:#o}) failed", name, flags, mode));
.fbstr());
} }
ownsFd_ = true; ownsFd_ = true;
} }
......
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <folly/Format.h> #include <fmt/core.h>
#include <folly/String.h> #include <folly/String.h>
#include <folly/detail/IPAddressSource.h> #include <folly/detail/IPAddressSource.h>
...@@ -93,7 +94,7 @@ CIDRNetwork IPAddress::createNetwork( ...@@ -93,7 +94,7 @@ CIDRNetwork IPAddress::createNetwork(
} }
if (ret.error() == CIDRNetworkError::INVALID_IP_SLASH_CIDR) { if (ret.error() == CIDRNetworkError::INVALID_IP_SLASH_CIDR) {
throw IPAddressFormatException(sformat( throw IPAddressFormatException(fmt::format(
"Invalid ipSlashCidr specified. Expected IP/CIDR format, got '{}'", "Invalid ipSlashCidr specified. Expected IP/CIDR format, got '{}'",
ipSlashCidr)); ipSlashCidr));
} }
...@@ -106,17 +107,17 @@ CIDRNetwork IPAddress::createNetwork( ...@@ -106,17 +107,17 @@ CIDRNetwork IPAddress::createNetwork(
case CIDRNetworkError::INVALID_IP: case CIDRNetworkError::INVALID_IP:
CHECK_GE(vec.size(), 1); CHECK_GE(vec.size(), 1);
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("Invalid IP address {}", vec.at(0))); fmt::format("Invalid IP address {}", vec.at(0)));
case CIDRNetworkError::INVALID_CIDR: case CIDRNetworkError::INVALID_CIDR:
CHECK_GE(vec.size(), 2); CHECK_GE(vec.size(), 2);
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("Mask value '{}' not a valid mask", vec.at(1))); fmt::format("Mask value '{}' not a valid mask", vec.at(1)));
case CIDRNetworkError::CIDR_MISMATCH: { case CIDRNetworkError::CIDR_MISMATCH: {
auto const subnet = IPAddress::tryFromString(vec.at(0)).value(); auto const subnet = IPAddress::tryFromString(vec.at(0)).value();
auto cidr = static_cast<uint8_t>( auto cidr = static_cast<uint8_t>(
(defaultCidr > -1) ? defaultCidr : (subnet.isV4() ? 32 : 128)); (defaultCidr > -1) ? defaultCidr : (subnet.isV4() ? 32 : 128));
throw IPAddressFormatException(sformat( throw IPAddressFormatException(fmt::format(
"CIDR value '{}' is > network bit count '{}'", "CIDR value '{}' is > network bit count '{}'",
vec.size() == 2 ? vec.at(1) : to<string>(cidr), vec.size() == 2 ? vec.at(1) : to<string>(cidr),
subnet.bitCount())); subnet.bitCount()));
...@@ -176,7 +177,7 @@ Expected<CIDRNetwork, CIDRNetworkError> IPAddress::tryCreateNetwork( ...@@ -176,7 +177,7 @@ Expected<CIDRNetwork, CIDRNetworkError> IPAddress::tryCreateNetwork(
// public static // public static
std::string IPAddress::networkToString(const CIDRNetwork& network) { std::string IPAddress::networkToString(const CIDRNetwork& network) {
return sformat("{}/{}", network.first.str(), network.second); return fmt::format("{}/{}", network.first.str(), network.second);
} }
// public static // public static
...@@ -188,7 +189,7 @@ IPAddress IPAddress::fromBinary(ByteRange bytes) { ...@@ -188,7 +189,7 @@ IPAddress IPAddress::fromBinary(ByteRange bytes) {
} else { } else {
string hexval = detail::Bytes::toHex(bytes.data(), bytes.size()); string hexval = detail::Bytes::toHex(bytes.data(), bytes.size());
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("Invalid address with hex value '{}'", hexval)); fmt::format("Invalid address with hex value '{}'", hexval));
} }
} }
...@@ -363,7 +364,7 @@ bool IPAddress::inSubnetWithMask(const IPAddress& subnet, ByteRange mask) ...@@ -363,7 +364,7 @@ bool IPAddress::inSubnetWithMask(const IPAddress& subnet, ByteRange mask)
uint8_t IPAddress::getNthMSByte(size_t byteIndex) const { uint8_t IPAddress::getNthMSByte(size_t byteIndex) const {
const auto highestIndex = byteCount() - 1; const auto highestIndex = byteCount() - 1;
if (byteIndex > highestIndex) { if (byteIndex > highestIndex) {
throw std::invalid_argument(sformat( throw std::invalid_argument(fmt::format(
"Byte index must be <= {} for addresses of type: {}", "Byte index must be <= {} for addresses of type: {}",
highestIndex, highestIndex,
detail::familyNameStr(family()))); detail::familyNameStr(family())));
...@@ -440,7 +441,7 @@ CIDRNetwork IPAddress::longestCommonPrefix( ...@@ -440,7 +441,7 @@ CIDRNetwork IPAddress::longestCommonPrefix(
const CIDRNetwork& one, const CIDRNetwork& one,
const CIDRNetwork& two) { const CIDRNetwork& two) {
if (one.first.family() != two.first.family()) { if (one.first.family() != two.first.family()) {
throw std::invalid_argument(sformat( throw std::invalid_argument(fmt::format(
"Can't compute longest common prefix between addresses of different" "Can't compute longest common prefix between addresses of different"
"families. Passed: {} and {}", "families. Passed: {} and {}",
detail::familyNameStr(one.first.family()), detail::familyNameStr(one.first.family()),
...@@ -463,13 +464,13 @@ CIDRNetwork IPAddress::longestCommonPrefix( ...@@ -463,13 +464,13 @@ CIDRNetwork IPAddress::longestCommonPrefix(
[[noreturn]] void IPAddress::asV4Throw() const { [[noreturn]] void IPAddress::asV4Throw() const {
auto fam = detail::familyNameStr(family()); auto fam = detail::familyNameStr(family());
throw InvalidAddressFamilyException( throw InvalidAddressFamilyException(
sformat("Can't convert address with family {} to AF_INET address", fam)); fmt::format("Can't convert address with family {} to AF_INET address", fam));
} }
[[noreturn]] void IPAddress::asV6Throw() const { [[noreturn]] void IPAddress::asV6Throw() const {
auto fam = detail::familyNameStr(family()); auto fam = detail::familyNameStr(family());
throw InvalidAddressFamilyException( throw InvalidAddressFamilyException(
sformat("Can't convert address with family {} to AF_INET6 address", fam)); fmt::format("Can't convert address with family {} to AF_INET6 address", fam));
} }
// clang-format on // clang-format on
......
...@@ -19,9 +19,12 @@ ...@@ -19,9 +19,12 @@
#include <ostream> #include <ostream>
#include <string> #include <string>
#include <folly/Format.h> #include <fmt/core.h>
#include <folly/Conv.h>
#include <folly/IPAddress.h> #include <folly/IPAddress.h>
#include <folly/IPAddressV6.h> #include <folly/IPAddressV6.h>
#include <folly/String.h>
#include <folly/detail/IPAddressSource.h> #include <folly/detail/IPAddressSource.h>
using std::ostream; using std::ostream;
...@@ -67,7 +70,7 @@ uint32_t IPAddressV4::toLong(StringPiece ip) { ...@@ -67,7 +70,7 @@ uint32_t IPAddressV4::toLong(StringPiece ip) {
in_addr addr; in_addr addr;
if (inet_pton(AF_INET, str.c_str(), &addr) != 1) { if (inet_pton(AF_INET, str.c_str(), &addr) != 1) {
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("Can't convert invalid IP '{}' to long", ip)); fmt::format("Can't convert invalid IP '{}' to long", ip));
} }
return addr.s_addr; return addr.s_addr;
} }
...@@ -140,12 +143,12 @@ IPAddressV4 IPAddressV4::fromInverseArpaName(const std::string& arpaname) { ...@@ -140,12 +143,12 @@ IPAddressV4 IPAddressV4::fromInverseArpaName(const std::string& arpaname) {
// input must be something like 1.0.168.192.in-addr.arpa // input must be something like 1.0.168.192.in-addr.arpa
if (!piece.removeSuffix(".in-addr.arpa")) { if (!piece.removeSuffix(".in-addr.arpa")) {
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("input does not end with '.in-addr.arpa': '{}'", arpaname)); fmt::format("input does not end with '.in-addr.arpa': '{}'", arpaname));
} }
std::vector<StringPiece> pieces; std::vector<StringPiece> pieces;
split(".", piece, pieces); split(".", piece, pieces);
if (pieces.size() != 4) { if (pieces.size() != 4) {
throw IPAddressFormatException(sformat("Invalid input. Got {}", piece)); throw IPAddressFormatException(fmt::format("Invalid input. Got {}", piece));
} }
// reverse 1.0.168.192 -> 192.168.0.1 // reverse 1.0.168.192 -> 192.168.0.1
return IPAddressV4(join(".", pieces.rbegin(), pieces.rend())); return IPAddressV4(join(".", pieces.rbegin(), pieces.rend()));
...@@ -169,7 +172,7 @@ IPAddressV6 IPAddressV4::getIPv6For6To4() const { ...@@ -169,7 +172,7 @@ IPAddressV6 IPAddressV4::getIPv6For6To4() const {
// public // public
string IPAddressV4::toJson() const { string IPAddressV4::toJson() const {
return sformat("{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash()); return fmt::format("{{family:'AF_INET', addr:'{}', hash:{}}}", str(), hash());
} }
// public // public
...@@ -178,7 +181,7 @@ bool IPAddressV4::inSubnet(StringPiece cidrNetwork) const { ...@@ -178,7 +181,7 @@ bool IPAddressV4::inSubnet(StringPiece cidrNetwork) const {
auto addr = subnetInfo.first; auto addr = subnetInfo.first;
if (!addr.isV4()) { if (!addr.isV4()) {
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("Address '{}' is not a V4 address", addr.toJson())); fmt::format("Address '{}' is not a V4 address", addr.toJson()));
} }
return inSubnetWithMask(addr.asV4(), fetchMask(subnetInfo.second)); return inSubnetWithMask(addr.asV4(), fetchMask(subnetInfo.second));
} }
...@@ -240,7 +243,7 @@ IPAddressV4 IPAddressV4::mask(size_t numBits) const { ...@@ -240,7 +243,7 @@ IPAddressV4 IPAddressV4::mask(size_t numBits) const {
static const auto bits = bitCount(); static const auto bits = bitCount();
if (numBits > bits) { if (numBits > bits) {
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("numBits({}) > bitsCount({})", numBits, bits)); fmt::format("numBits({}) > bitsCount({})", numBits, bits));
} }
ByteArray4 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_); ByteArray4 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_);
...@@ -259,7 +262,7 @@ void IPAddressV4::toFullyQualifiedAppend(std::string& out) const { ...@@ -259,7 +262,7 @@ void IPAddressV4::toFullyQualifiedAppend(std::string& out) const {
// public // public
string IPAddressV4::toInverseArpaName() const { string IPAddressV4::toInverseArpaName() const {
return sformat( return fmt::format(
"{}.{}.{}.{}.in-addr.arpa", "{}.{}.{}.{}.in-addr.arpa",
addr_.bytes_[3], addr_.bytes_[3],
addr_.bytes_[2], addr_.bytes_[2],
...@@ -271,7 +274,7 @@ string IPAddressV4::toInverseArpaName() const { ...@@ -271,7 +274,7 @@ string IPAddressV4::toInverseArpaName() const {
uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const { uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
const auto highestIndex = byteCount() - 1; const auto highestIndex = byteCount() - 1;
if (byteIndex > highestIndex) { if (byteIndex > highestIndex) {
throw std::invalid_argument(sformat( throw std::invalid_argument(fmt::format(
"Byte index must be <= {} for addresses of type: {}", "Byte index must be <= {} for addresses of type: {}",
highestIndex, highestIndex,
detail::familyNameStr(AF_INET))); detail::familyNameStr(AF_INET)));
......
...@@ -20,10 +20,13 @@ ...@@ -20,10 +20,13 @@
#include <ostream> #include <ostream>
#include <string> #include <string>
#include <folly/Format.h> #include <fmt/core.h>
#include <folly/IPAddress.h> #include <folly/IPAddress.h>
#include <folly/IPAddressV4.h> #include <folly/IPAddressV4.h>
#include <folly/MacAddress.h> #include <folly/MacAddress.h>
#include <folly/ScopeGuard.h>
#include <folly/String.h>
#include <folly/detail/IPAddressSource.h> #include <folly/detail/IPAddressSource.h>
#ifndef _WIN32 #ifndef _WIN32
...@@ -203,13 +206,14 @@ Expected<Unit, IPAddressFormatError> IPAddressV6::trySetFromBinary( ...@@ -203,13 +206,14 @@ Expected<Unit, IPAddressFormatError> IPAddressV6::trySetFromBinary(
IPAddressV6 IPAddressV6::fromInverseArpaName(const std::string& arpaname) { IPAddressV6 IPAddressV6::fromInverseArpaName(const std::string& arpaname) {
auto piece = StringPiece(arpaname); auto piece = StringPiece(arpaname);
if (!piece.removeSuffix(".ip6.arpa")) { if (!piece.removeSuffix(".ip6.arpa")) {
throw IPAddressFormatException(sformat( throw IPAddressFormatException(fmt::format(
"Invalid input. Should end with 'ip6.arpa'. Got '{}'", arpaname)); "Invalid input. Should end with 'ip6.arpa'. Got '{}'", arpaname));
} }
std::vector<StringPiece> pieces; std::vector<StringPiece> pieces;
split(".", piece, pieces); split(".", piece, pieces);
if (pieces.size() != 32) { if (pieces.size() != 32) {
throw IPAddressFormatException(sformat("Invalid input. Got '{}'", piece)); throw IPAddressFormatException(
fmt::format("Invalid input. Got '{}'", piece));
} }
std::array<char, IPAddressV6::kToFullyQualifiedSize> ip; std::array<char, IPAddressV6::kToFullyQualifiedSize> ip;
size_t pos = 0; size_t pos = 0;
...@@ -256,7 +260,7 @@ unpackInto(const unsigned char* src, uint16_t* dest, size_t count) { ...@@ -256,7 +260,7 @@ unpackInto(const unsigned char* src, uint16_t* dest, size_t count) {
IPAddressV4 IPAddressV6::getIPv4For6To4() const { IPAddressV4 IPAddressV6::getIPv4For6To4() const {
if (!is6To4()) { if (!is6To4()) {
throw IPAddressV6::TypeError( throw IPAddressV6::TypeError(
sformat("Invalid IP '{}': not a 6to4 address", str())); fmt::format("Invalid IP '{}': not a 6to4 address", str()));
} }
// convert 16x8 bytes into first 4x16 bytes // convert 16x8 bytes into first 4x16 bytes
uint16_t ints[4] = {0, 0, 0, 0}; uint16_t ints[4] = {0, 0, 0, 0};
...@@ -308,7 +312,8 @@ IPAddressV6::Type IPAddressV6::type() const { ...@@ -308,7 +312,8 @@ IPAddressV6::Type IPAddressV6::type() const {
// public // public
string IPAddressV6::toJson() const { string IPAddressV6::toJson() const {
return sformat("{{family:'AF_INET6', addr:'{}', hash:{}}}", str(), hash()); return fmt::format(
"{{family:'AF_INET6', addr:'{}', hash:{}}}", str(), hash());
} }
// public // public
...@@ -332,7 +337,7 @@ bool IPAddressV6::inSubnet(StringPiece cidrNetwork) const { ...@@ -332,7 +337,7 @@ bool IPAddressV6::inSubnet(StringPiece cidrNetwork) const {
auto addr = subnetInfo.first; auto addr = subnetInfo.first;
if (!addr.isV6()) { if (!addr.isV6()) {
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("Address '{}' is not a V6 address", addr.toJson())); fmt::format("Address '{}' is not a V6 address", addr.toJson()));
} }
return inSubnetWithMask(addr.asV6(), fetchMask(subnetInfo.second)); return inSubnetWithMask(addr.asV6(), fetchMask(subnetInfo.second));
} }
...@@ -429,7 +434,7 @@ IPAddressV6 IPAddressV6::mask(size_t numBits) const { ...@@ -429,7 +434,7 @@ IPAddressV6 IPAddressV6::mask(size_t numBits) const {
static const auto bits = bitCount(); static const auto bits = bitCount();
if (numBits > bits) { if (numBits > bits) {
throw IPAddressFormatException( throw IPAddressFormatException(
sformat("numBits({}) > bitCount({})", numBits, bits)); fmt::format("numBits({}) > bitCount({})", numBits, bits));
} }
ByteArray16 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_); ByteArray16 ba = detail::Bytes::mask(fetchMask(numBits), addr_.bytes_);
return IPAddressV6(ba); return IPAddressV6(ba);
...@@ -440,7 +445,7 @@ string IPAddressV6::str() const { ...@@ -440,7 +445,7 @@ string IPAddressV6::str() const {
char buffer[INET6_ADDRSTRLEN + IFNAMSIZ + 1]; char buffer[INET6_ADDRSTRLEN + IFNAMSIZ + 1];
if (!inet_ntop(AF_INET6, toAddr().s6_addr, buffer, INET6_ADDRSTRLEN)) { if (!inet_ntop(AF_INET6, toAddr().s6_addr, buffer, INET6_ADDRSTRLEN)) {
throw IPAddressFormatException(sformat( throw IPAddressFormatException(fmt::format(
"Invalid address with hex '{}' with error {}", "Invalid address with hex '{}' with error {}",
detail::Bytes::toHex(bytes(), 16), detail::Bytes::toHex(bytes(), 16),
errnoStr(errno))); errnoStr(errno)));
...@@ -483,14 +488,14 @@ string IPAddressV6::toInverseArpaName() const { ...@@ -483,14 +488,14 @@ string IPAddressV6::toInverseArpaName() const {
a[j + 1] = (lut[bytes()[i] >> 4]); a[j + 1] = (lut[bytes()[i] >> 4]);
j += 2; j += 2;
} }
return sformat("{}.ip6.arpa", join(".", a)); return fmt::format("{}.ip6.arpa", join(".", a));
} }
// public // public
uint8_t IPAddressV6::getNthMSByte(size_t byteIndex) const { uint8_t IPAddressV6::getNthMSByte(size_t byteIndex) const {
const auto highestIndex = byteCount() - 1; const auto highestIndex = byteCount() - 1;
if (byteIndex > highestIndex) { if (byteIndex > highestIndex) {
throw std::invalid_argument(sformat( throw std::invalid_argument(fmt::format(
"Byte index must be <= {} for addresses of type: {}", "Byte index must be <= {} for addresses of type: {}",
highestIndex, highestIndex,
detail::familyNameStr(AF_INET6))); detail::familyNameStr(AF_INET6)));
......
...@@ -28,8 +28,9 @@ ...@@ -28,8 +28,9 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <fmt/core.h>
#include <folly/Demangle.h> #include <folly/Demangle.h>
#include <folly/Format.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/detail/SingletonStackTrace.h> #include <folly/detail/SingletonStackTrace.h>
#include <folly/portability/Config.h> #include <folly/portability/Config.h>
...@@ -158,9 +159,9 @@ void singletonPrintDestructionStackTrace(const TypeDescriptor& type) { ...@@ -158,9 +159,9 @@ void singletonPrintDestructionStackTrace(const TypeDescriptor& type) {
} }
[[noreturn]] void singletonThrowNullCreator(const std::type_info& type) { [[noreturn]] void singletonThrowNullCreator(const std::type_info& type) {
auto const msg = sformat( auto const msg = fmt::format(
"nullptr_t should be passed if you want {} to be default constructed", "nullptr_t should be passed if you want {} to be default constructed",
demangle(type)); folly::StringPiece(demangle(type)));
throw std::logic_error(msg); throw std::logic_error(msg);
} }
......
...@@ -27,12 +27,14 @@ ...@@ -27,12 +27,14 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <system_error> #include <system_error>
#include <type_traits>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <fmt/core.h>
#include <folly/CppAttributes.h> #include <folly/CppAttributes.h>
#include <folly/Exception.h> #include <folly/Exception.h>
#include <folly/Format.h>
#include <folly/hash/Hash.h> #include <folly/hash/Hash.h>
#include <folly/net/NetOps.h> #include <folly/net/NetOps.h>
#include <folly/net/NetworkSocket.h> #include <folly/net/NetworkSocket.h>
...@@ -103,6 +105,22 @@ struct HostAndPort { ...@@ -103,6 +105,22 @@ struct HostAndPort {
char* allocated; char* allocated;
}; };
struct GetAddrInfoError {
#ifdef _WIN32
std::string error;
const char* str() const { return error.c_str(); }
explicit GetAddrInfoError(int errorCode) {
auto s = gai_strerror(errorCode);
using Char = std::remove_reference_t<decltype(*s)>;
error.assign(s, s + std::char_traits<Char>::length(s));
}
#else
const char* error;
const char* str() const { return error; }
explicit GetAddrInfoError(int errorCode) : error(gai_strerror(errorCode)) {}
#endif
};
} // namespace } // namespace
namespace folly { namespace folly {
...@@ -617,10 +635,10 @@ SocketAddress::getAddrInfo(const char* host, const char* port, int flags) { ...@@ -617,10 +635,10 @@ SocketAddress::getAddrInfo(const char* host, const char* port, int flags) {
struct addrinfo* results; struct addrinfo* results;
int error = getaddrinfo(host, port, &hints, &results); int error = getaddrinfo(host, port, &hints, &results);
if (error != 0) { if (error != 0) {
auto os = folly::sformat( auto os = fmt::format(
"Failed to resolve address for '{}': {} (error={})", "Failed to resolve address for '{}': {} (error={})",
host, host,
gai_strerror(error), GetAddrInfoError(error).str(),
error); error);
throw std::system_error(error, std::generic_category(), os); throw std::system_error(error, std::generic_category(), os);
} }
...@@ -684,8 +702,9 @@ void SocketAddress::getIpString(char* buf, size_t buflen, int flags) const { ...@@ -684,8 +702,9 @@ void SocketAddress::getIpString(char* buf, size_t buflen, int flags) const {
0, 0,
flags); flags);
if (rc != 0) { if (rc != 0) {
auto os = sformat( auto os = fmt::format(
"getnameinfo() failed in getIpString() error = {}", gai_strerror(rc)); "getnameinfo() failed in getIpString() error = {}",
GetAddrInfoError(rc).str());
throw std::system_error(rc, std::generic_category(), os); throw std::system_error(rc, std::generic_category(), os);
} }
} }
......
...@@ -22,10 +22,11 @@ ...@@ -22,10 +22,11 @@
#endif #endif
#include <fstream> #include <fstream>
#include <fmt/core.h>
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/Exception.h> #include <folly/Exception.h>
#include <folly/FileUtil.h> #include <folly/FileUtil.h>
#include <folly/Format.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
namespace folly { namespace folly {
...@@ -108,8 +109,8 @@ CacheLocality CacheLocality::readFromSysfsTree( ...@@ -108,8 +109,8 @@ CacheLocality CacheLocality::readFromSysfsTree(
auto cpu = cpus.size(); auto cpu = cpus.size();
std::vector<size_t> levels; std::vector<size_t> levels;
for (size_t index = 0;; ++index) { for (size_t index = 0;; ++index) {
auto dir = auto dir = fmt::format(
sformat("/sys/devices/system/cpu/cpu{}/cache/index{}/", cpu, index); "/sys/devices/system/cpu/cpu{}/cache/index{}/", cpu, index);
auto cacheType = mapping(dir + "type"); auto cacheType = mapping(dir + "type");
auto equivStr = mapping(dir + "shared_cpu_list"); auto equivStr = mapping(dir + "shared_cpu_list");
if (cacheType.empty() || equivStr.empty()) { if (cacheType.empty() || equivStr.empty()) {
......
...@@ -24,7 +24,8 @@ ...@@ -24,7 +24,8 @@
#include <boost/interprocess/allocators/adaptive_pool.hpp> #include <boost/interprocess/allocators/adaptive_pool.hpp>
#include <boost/interprocess/managed_shared_memory.hpp> #include <boost/interprocess/managed_shared_memory.hpp>
#include <folly/Format.h> #include <fmt/core.h>
#include <folly/Random.h> #include <folly/Random.h>
#include <folly/Traits.h> #include <folly/Traits.h>
#include <folly/container/F14Map.h> #include <folly/container/F14Map.h>
...@@ -108,7 +109,7 @@ using ShmF14VectorI2VVI = folly::F14VectorMap< ...@@ -108,7 +109,7 @@ using ShmF14VectorI2VVI = folly::F14VectorMap<
namespace { namespace {
std::string makeRandomName() { std::string makeRandomName() {
return folly::sformat("f14test_{}", folly::Random::rand64()); return fmt::format("f14test_{}", folly::Random::rand64());
} }
std::shared_ptr<managed_shared_memory> makeShmSegment( std::shared_ptr<managed_shared_memory> makeShmSegment(
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <random> #include <random>
#include <vector> #include <vector>
#include <fmt/core.h>
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/Format.h> #include <folly/Format.h>
#include <folly/portability/GFlags.h> #include <folly/portability/GFlags.h>
...@@ -117,23 +118,23 @@ void setup_rand_bench() { ...@@ -117,23 +118,23 @@ void setup_rand_bench() {
tie(size_add, size_contains) = kvp; tie(size_add, size_contains) = kvp;
addBenchmark( addBenchmark(
__FILE__, __FILE__,
sformat("bitset_rand_bench({}, {})", size_add, size_contains).c_str(), fmt::format("bitset_rand_bench({}, {})", size_add, size_contains),
[=](int iters) { [=](int iters) {
rand_bench<BitSetWrapper>(iters, size_add, size_contains); rand_bench<BitSetWrapper>(iters, size_add, size_contains);
return iters; return iters;
}); });
addBenchmark( addBenchmark(
__FILE__, __FILE__,
sformat("%bool_array_set_rand_bench({}, {})", size_add, size_contains) fmt::format(
.c_str(), "%bool_array_set_rand_bench({}, {})", size_add, size_contains),
[=](int iters) { [=](int iters) {
rand_bench<BoolArraySet>(iters, size_add, size_contains); rand_bench<BoolArraySet>(iters, size_add, size_contains);
return iters; return iters;
}); });
addBenchmark( addBenchmark(
__FILE__, __FILE__,
sformat("%sparse_byte_set_rand_bench({}, {})", size_add, size_contains) fmt::format(
.c_str(), "%sparse_byte_set_rand_bench({}, {})", size_add, size_contains),
[=](int iters) { [=](int iters) {
rand_bench<SparseByteSet>(iters, size_add, size_contains); rand_bench<SparseByteSet>(iters, size_add, size_contains);
return iters; return iters;
......
...@@ -16,17 +16,18 @@ ...@@ -16,17 +16,18 @@
#include <folly/detail/IPAddress.h> #include <folly/detail/IPAddress.h>
#include <folly/Format.h> #include <fmt/core.h>
#include <stdexcept>
namespace folly { namespace folly {
namespace detail { namespace detail {
std::string familyNameStrDefault(sa_family_t family) { std::string familyNameStrDefault(sa_family_t family) {
return sformat("sa_family_t({})", family); return fmt::format("sa_family_t({})", family);
} }
[[noreturn]] void getNthMSBitImplThrow(size_t bitCount, sa_family_t family) { [[noreturn]] void getNthMSBitImplThrow(size_t bitCount, sa_family_t family) {
throw std::invalid_argument(sformat( throw std::invalid_argument(fmt::format(
"Bit index must be < {} for addresses of type: {}", "Bit index must be < {} for addresses of type: {}",
bitCount, bitCount,
familyNameStr(family))); familyNameStr(family)));
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <folly/Format.h> #include <fmt/core.h>
#include <folly/detail/IPAddress.h> #include <folly/detail/IPAddress.h>
// BSDish platforms don't provide standard access to s6_addr16 // BSDish platforms don't provide standard access to s6_addr16
...@@ -76,7 +76,7 @@ struct Bytes { ...@@ -76,7 +76,7 @@ struct Bytes {
0xff // /8 0xff // /8
}}; }};
if (oneMask > kBitCount || twoMask > kBitCount) { if (oneMask > kBitCount || twoMask > kBitCount) {
throw std::invalid_argument(sformat( throw std::invalid_argument(fmt::format(
"Invalid mask length: {}. Mask length must be <= {}", "Invalid mask length: {}. Mask length must be <= {}",
std::max(oneMask, twoMask), std::max(oneMask, twoMask),
kBitCount)); kBitCount));
......
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
#include <folly/fibers/detail/AtomicBatchDispatcher.h> #include <folly/fibers/detail/AtomicBatchDispatcher.h>
#include <folly/Format.h> #include <fmt/core.h>
#include <cassert>
namespace folly { namespace folly {
namespace fibers { namespace fibers {
...@@ -28,15 +29,14 @@ std::string createABDTokenNotDispatchedExMsg( ...@@ -28,15 +29,14 @@ std::string createABDTokenNotDispatchedExMsg(
assert(numTokensNotDispatched > 0); assert(numTokensNotDispatched > 0);
size_t numSeqNumToPrint = size_t numSeqNumToPrint =
(numTokensNotDispatched > 10 ? 10 : numTokensNotDispatched); (numTokensNotDispatched > 10 ? 10 : numTokensNotDispatched);
std::string strInputsNotFound = std::string strInputsNotFound = fmt::format("{}", vecTokensNotDispatched[0]);
folly::sformat("{}", vecTokensNotDispatched[0]);
for (size_t i = 1; i < numSeqNumToPrint; ++i) { for (size_t i = 1; i < numSeqNumToPrint; ++i) {
strInputsNotFound += folly::sformat(", {}", vecTokensNotDispatched[i]); strInputsNotFound += fmt::format(", {}", vecTokensNotDispatched[i]);
} }
if (numSeqNumToPrint < numTokensNotDispatched) { if (numSeqNumToPrint < numTokensNotDispatched) {
strInputsNotFound += "..."; strInputsNotFound += "...";
} }
return folly::sformat( return fmt::format(
"{} input tokens (seq nums: {}) destroyed before calling dispatch", "{} input tokens (seq nums: {}) destroyed before calling dispatch",
numTokensNotDispatched, numTokensNotDispatched,
strInputsNotFound); strInputsNotFound);
...@@ -45,7 +45,7 @@ std::string createABDTokenNotDispatchedExMsg( ...@@ -45,7 +45,7 @@ std::string createABDTokenNotDispatchedExMsg(
std::string createUnexpectedNumResultsABDUsageExMsg( std::string createUnexpectedNumResultsABDUsageExMsg(
size_t numExpectedResults, size_t numExpectedResults,
size_t numActualResults) { size_t numActualResults) {
return folly::sformat( return fmt::format(
"Unexpected number of results ({}) returned from dispatch function, " "Unexpected number of results ({}) returned from dispatch function, "
"expected ({})", "expected ({})",
numActualResults, numActualResults,
......
...@@ -23,10 +23,10 @@ ...@@ -23,10 +23,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <fmt/core.h>
#include <glog/logging.h> #include <glog/logging.h>
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/Format.h>
#include <folly/Preprocessor.h> #include <folly/Preprocessor.h>
#include <folly/portability/GFlags.h> #include <folly/portability/GFlags.h>
...@@ -59,7 +59,7 @@ void addHashBenchmark(const std::string& name) { ...@@ -59,7 +59,7 @@ void addHashBenchmark(const std::string& name) {
for (size_t i = 0; i < 16; ++i) { for (size_t i = 0; i < 16; ++i) {
auto k = size_t(1) << i; auto k = size_t(1) << i;
names.emplace_back(folly::sformat("{}: k=2^{}", name, i)); names.emplace_back(fmt::format("{}: k=2^{}", name, i));
folly::addBenchmark(__FILE__, names.back().c_str(), [=](unsigned iters) { folly::addBenchmark(__FILE__, names.back().c_str(), [=](unsigned iters) {
Hasher hasher; Hasher hasher;
bmHasher(hasher, k, iters); bmHasher(hasher, k, iters);
......
...@@ -16,10 +16,11 @@ ...@@ -16,10 +16,11 @@
#include <folly/IPAddress.h> #include <folly/IPAddress.h>
#include <fmt/core.h>
#include <glog/logging.h> #include <glog/logging.h>
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/Format.h> #include <folly/Conv.h>
using namespace folly; using namespace folly;
using std::string; using std::string;
...@@ -50,7 +51,7 @@ BENCHMARK_DRAW_LINE(); ...@@ -50,7 +51,7 @@ BENCHMARK_DRAW_LINE();
BENCHMARK(ipv4_to_fully_qualified_port, iters) { BENCHMARK(ipv4_to_fully_qualified_port, iters) {
IPAddressV4 ip("255.255.255.255"); IPAddressV4 ip("255.255.255.255");
while (iters--) { while (iters--) {
string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535); string outputString = fmt::format("{}:{}", ip.toFullyQualified(), 65535);
folly::doNotOptimizeAway(outputString); folly::doNotOptimizeAway(outputString);
folly::doNotOptimizeAway(outputString.data()); folly::doNotOptimizeAway(outputString.data());
} }
...@@ -97,7 +98,7 @@ BENCHMARK_DRAW_LINE(); ...@@ -97,7 +98,7 @@ BENCHMARK_DRAW_LINE();
BENCHMARK(ipv6_to_fully_qualified_port, iters) { BENCHMARK(ipv6_to_fully_qualified_port, iters) {
IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725"); IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
while (iters--) { while (iters--) {
string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535); string outputString = fmt::format("{}:{}", ip.toFullyQualified(), 65535);
folly::doNotOptimizeAway(outputString); folly::doNotOptimizeAway(outputString);
folly::doNotOptimizeAway(outputString.data()); folly::doNotOptimizeAway(outputString.data());
} }
......
...@@ -20,7 +20,8 @@ ...@@ -20,7 +20,8 @@
#include <string> #include <string>
#include <folly/Format.h> #include <fmt/core.h>
#include <folly/MacAddress.h> #include <folly/MacAddress.h>
#include <folly/String.h> #include <folly/String.h>
#include <folly/container/BitIterator.h> #include <folly/container/BitIterator.h>
...@@ -661,7 +662,7 @@ TEST(IPaddress, toInverseArpaName) { ...@@ -661,7 +662,7 @@ TEST(IPaddress, toInverseArpaName) {
EXPECT_EQ("1.0.0.10.in-addr.arpa", addr_ipv4.toInverseArpaName()); EXPECT_EQ("1.0.0.10.in-addr.arpa", addr_ipv4.toInverseArpaName());
IPAddressV6 addr_ipv6("2620:0000:1cfe:face:b00c:0000:0000:0003"); IPAddressV6 addr_ipv6("2620:0000:1cfe:face:b00c:0000:0000:0003");
EXPECT_EQ( EXPECT_EQ(
sformat( fmt::format(
"{}.ip6.arpa", "{}.ip6.arpa",
"3.0.0.0.0.0.0.0.0.0.0.0.c.0.0.b.e.c.a.f.e.f.c.1.0.0.0.0.0.2.6.2"), "3.0.0.0.0.0.0.0.0.0.0.0.c.0.0.b.e.c.a.f.e.f.c.1.0.0.0.0.0.2.6.2"),
addr_ipv6.toInverseArpaName()); addr_ipv6.toInverseArpaName());
...@@ -673,7 +674,7 @@ TEST(IPaddress, fromInverseArpaName) { ...@@ -673,7 +674,7 @@ TEST(IPaddress, fromInverseArpaName) {
IPAddressV4::fromInverseArpaName("1.0.0.10.in-addr.arpa")); IPAddressV4::fromInverseArpaName("1.0.0.10.in-addr.arpa"));
EXPECT_EQ( EXPECT_EQ(
IPAddressV6("2620:0000:1cfe:face:b00c:0000:0000:0003"), IPAddressV6("2620:0000:1cfe:face:b00c:0000:0000:0003"),
IPAddressV6::fromInverseArpaName(sformat( IPAddressV6::fromInverseArpaName(fmt::format(
"{}.ip6.arpa", "{}.ip6.arpa",
"3.0.0.0.0.0.0.0.0.0.0.0.c.0.0.b.e.c.a.f.e.f.c.1.0.0.0.0.0.2.6.2"))); "3.0.0.0.0.0.0.0.0.0.0.0.c.0.0.b.e.c.a.f.e.f.c.1.0.0.0.0.0.2.6.2")));
} }
......
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
#include <folly/MacAddress.h> #include <folly/MacAddress.h>
#include <folly/Format.h> #include <fmt/core.h>
#include <folly/IPAddressV6.h> #include <folly/IPAddressV6.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
...@@ -141,7 +142,7 @@ TEST(MacAddress, createMulticast) { ...@@ -141,7 +142,7 @@ TEST(MacAddress, createMulticast) {
} }
void testCmp(const char* str1, const char* str2) { void testCmp(const char* str1, const char* str2) {
SCOPED_TRACE(folly::sformat("{} < {}", str1, str2)); SCOPED_TRACE(fmt::format("{} < {}", str1, str2));
MacAddress m1(str1); MacAddress m1(str1);
MacAddress m2(str2); MacAddress m2(str2);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment