std::string getXlogCategoryNameForFile(StringPiece filename) { // Buck mangles the directory layout for header files. Rather than including // them from their original location, it moves them into deep directories // inside buck-out, and includes them from there. // // If this path looks like a buck header directory, try to strip off the // buck-specific portion. if (filename.startsWith("buck-out/")) { filename = stripBuckOutPrefix(filename); } std::string categoryName = filename.str(); // Translate slashes to dots, to turn the directory layout into // a category hierarchy. size_t lastDot = std::string::npos; for (size_t n = 0; n < categoryName.size(); ++n) { if (categoryName[n] == '/') { categoryName[n] = '.'; lastDot = std::string::npos; } else if (categoryName[n] == '.') { lastDot = n; } } // Strip off the filename extension, if one was present. if (lastDot != std::string::npos) { categoryName.resize(lastDot); } return categoryName; }
static StringPiece pathBasename(StringPiece path) { auto idx = path.rfind('/'); if (idx == StringPiece::npos) { return path.str(); } return path.subpiece(idx + 1); }
std::shared_ptr<ElfFile> ElfCache::getFile(StringPiece p) { auto path = p.str(); std::lock_guard<std::mutex> lock(mutex_); auto pos = files_.find(path); if (pos != files_.end()) { // Found, move to back (MRU) auto& entry = pos->second; lruList_.erase(lruList_.iterator_to(*entry)); lruList_.push_back(*entry); return filePtr(entry); } auto entry = std::make_shared<Entry>(); // No negative caching if (entry->file.openNoThrow(path.c_str()) == -1) { return nullptr; } if (files_.size() == capacity_) { // Evict LRU lruList_.pop_front(); } files_.emplace(std::move(path), entry); lruList_.push_back(*entry); return filePtr(entry); }
std::shared_ptr<ElfFile> ElfCache::getFile(StringPiece p) { std::lock_guard<std::mutex> lock(mutex_); auto pos = files_.find(p); if (pos != files_.end()) { // Found, move to back (MRU) auto& entry = pos->second; lruList_.erase(lruList_.iterator_to(*entry)); lruList_.push_back(*entry); return filePtr(entry); } auto entry = std::make_shared<Entry>(); entry->path = p.str(); auto& path = entry->path; // No negative caching const char* msg = ""; int r = entry->file.openNoThrow(path.c_str(), true, &msg); if (r != ElfFile::kSuccess) { return nullptr; } if (files_.size() == capacity_) { auto& e = lruList_.front(); lruList_.pop_front(); files_.erase(e.path); } files_.emplace(entry->path, entry); lruList_.push_back(*entry); return filePtr(entry); }
RocksHandles::RocksHandles( StringPiece dbPath, const Options& options, const std::vector<ColumnFamilyDescriptor>& columnDescriptors) { auto dbPathStr = dbPath.str(); DB* dbRaw; std::vector<ColumnFamilyHandle*> columnHandles; // This will create any newly defined column families automatically, // so we needn't make any special migration steps here; just define // a new family and start to use it. // If we remove column families in the future this call will fail // and shout at us for not opening up the database with them defined. // We will need to do "something smarter" if we ever decide to perform // that kind of a migration. auto status = DB::Open(options, dbPathStr, columnDescriptors, &columnHandles, &dbRaw); if (!status.ok()) { XLOG(ERR) << "Error opening RocksDB storage at " << dbPathStr << ": " << status.ToString(); throw RocksException::build( status, "error opening RocksDB storage at", dbPathStr); } db.reset(dbRaw); columns.reserve(columnHandles.size()); for (auto h : columnHandles) { columns.emplace_back(h); } }
// public string constructor IPAddressV6::IPAddressV6(StringPiece addr) { auto ip = addr.str(); // Allow addresses surrounded in brackets if (ip.size() < 2) { throw IPAddressFormatException( to<std::string>("Invalid IPv6 address '", ip, "': address too short")); } if (ip.front() == '[' && ip.back() == ']') { ip = ip.substr(1, ip.size() - 2); } struct addrinfo* result; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICHOST; if (!getaddrinfo(ip.c_str(), nullptr, &hints, &result)) { struct sockaddr_in6* ipAddr = (struct sockaddr_in6*)result->ai_addr; addr_.in6Addr_ = ipAddr->sin6_addr; scope_ = ipAddr->sin6_scope_id; freeaddrinfo(result); } else { throw IPAddressFormatException( to<std::string>("Invalid IPv6 address '", ip, "'")); } }
// public string constructor IPAddressV4::IPAddressV4(StringPiece addr) : addr_() { auto ip = addr.str(); if (inet_pton(AF_INET, ip.c_str(), &addr_.inAddr_) != 1) { throw IPAddressFormatException("Invalid IPv4 address '", addr, "'"); } }
void expect(LineReader& lr, const char* expected) { StringPiece line; size_t expectedLen = strlen(expected); EXPECT_EQ(expectedLen != 0 ? LineReader::kReading : LineReader::kEof, lr.readLine(line)); EXPECT_EQ(expectedLen, line.size()); EXPECT_EQ(std::string(expected, expectedLen), line.str()); }
// static public uint32_t IPAddressV4::toLong(StringPiece ip) { auto str = ip.str(); in_addr addr; if (inet_pton(AF_INET, str.c_str(), &addr) != 1) { throw IPAddressFormatException("Can't convert invalid IP '", ip, "' ", "to long"); } return addr.s_addr; }
void FunctionScheduler::addFunction(Function<void()>&& cb, milliseconds interval, StringPiece nameID, milliseconds startDelay) { addFunctionGenericDistribution( std::move(cb), ConstIntervalFunctor(interval), nameID.str(), to<std::string>(interval.count(), "ms"), startDelay); }
void writeFileAtomic( StringPiece filename, iovec* iov, int count, mode_t permissions) { auto rc = writeFileAtomicNoThrow(filename, iov, count, permissions); if (rc != 0) { auto msg = std::string(__func__) + "() failed to update " + filename.str(); throw std::system_error(rc, std::generic_category(), msg); } }
void FunctionScheduler::addFunctionUniformDistribution( Function<void()>&& cb, milliseconds minInterval, milliseconds maxInterval, StringPiece nameID, milliseconds startDelay) { addFunctionGenericDistribution( std::move(cb), UniformDistributionFunctor(minInterval, maxInterval), nameID.str(), to<std::string>( "[", minInterval.count(), " , ", maxInterval.count(), "] ms"), startDelay); }
void PktCaptureManager::stopCapture(StringPiece name) { std::lock_guard<std::mutex> g(mutex_); auto nameStr = name.str(); auto it = activeCaptures_.find(nameStr); if (it == activeCaptures_.end()) { throw FbossError("no active capture found with name \"", name, "\""); } LOG(INFO) << "stopping packet capture \"" << name << "\""; it->second->stop(); inactiveCaptures_[nameStr] = std::move(it->second); activeCaptures_.erase(it); capturesRunning_.store(!activeCaptures_.empty(), std::memory_order_release); }
// public string constructor IPAddressV6::IPAddressV6(StringPiece addr) { auto ip = addr.str(); // Allow addresses surrounded in brackets if (ip.size() < 2) { throw IPAddressFormatException("Invalid IPv6 address '", ip, "': address too short"); } if (ip.front() == '[' && ip.back() == ']') { ip = ip.substr(1, ip.size() - 2); } if (inet_pton(AF_INET6, ip.c_str(), &addr_.in6Addr_) != 1) { throw IPAddressFormatException("Invalid IPv6 address '", ip, "'"); } }
void FunctionScheduler::addFunction(Function<void()>&& cb, milliseconds interval, const LatencyDistribution& latencyDistr, StringPiece nameID, milliseconds startDelay) { if (latencyDistr.isPoisson) { addFunctionGenericDistribution( std::move(cb), PoissonDistributionFunctor(latencyDistr.poissonMean), nameID.str(), to<std::string>(latencyDistr.poissonMean, "ms (Poisson mean)"), startDelay); } else { addFunction(std::move(cb), interval, nameID, startDelay); } }
// public string constructor IPAddress::IPAddress(StringPiece addr) : addr_() , family_(AF_UNSPEC) { string ip = addr.str(); // inet_pton() needs NUL-terminated string auto throwFormatException = [&](const string& msg) { throw IPAddressFormatException( to<std::string>("Invalid IP '", ip, "': ", msg)); }; if (ip.size() < 2) { throwFormatException("address too short"); } if (ip.front() == '[' && ip.back() == ']') { ip = ip.substr(1, ip.size() - 2); } // need to check for V4 address second, since IPv4-mapped IPv6 addresses may // contain a period if (ip.find(':') != string::npos) { struct addrinfo* result; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICHOST; if (!getaddrinfo(ip.c_str(), nullptr, &hints, &result)) { struct sockaddr_in6* ipAddr = (struct sockaddr_in6*)result->ai_addr; addr_ = IPAddressV46(IPAddressV6(*ipAddr)); family_ = AF_INET6; freeaddrinfo(result); } else { throwFormatException("getsockaddr failed for V6 address"); } } else if (ip.find('.') != string::npos) { in_addr ipAddr; if (inet_pton(AF_INET, ip.c_str(), &ipAddr) != 1) { throwFormatException("inet_pton failed for V4 address"); } addr_ = IPAddressV46(IPAddressV4(ipAddr)); family_ = AF_INET; } else { throwFormatException("invalid address format"); } }
unique_ptr<PktCapture> PktCaptureManager::forgetCapture(StringPiece name) { std::lock_guard<std::mutex> g(mutex_); auto nameStr = name.str(); auto activeIt = activeCaptures_.find(nameStr); if (activeIt != activeCaptures_.end()) { LOG(INFO) << "stopping packet capture \"" << name << "\""; std::unique_ptr<PktCapture> capture = std::move(activeIt->second); activeCaptures_.erase(activeIt); capturesRunning_.store(!activeCaptures_.empty(), std::memory_order_release); capture->stop(); return capture; } auto inactiveIt = inactiveCaptures_.find(nameStr); if (inactiveIt != inactiveCaptures_.end()) { std::unique_ptr<PktCapture> capture = std::move(inactiveIt->second); inactiveCaptures_.erase(inactiveIt); return capture; } throw FbossError("no capture found with name \"", name, "\""); }
void addType(StringPiece value) { if (value == "array") { allowedTypes_.push_back(dynamic::Type::ARRAY); } else if (value == "boolean") { allowedTypes_.push_back(dynamic::Type::BOOL); } else if (value == "integer") { allowedTypes_.push_back(dynamic::Type::INT64); } else if (value == "number") { allowedTypes_.push_back(dynamic::Type::INT64); allowedTypes_.push_back(dynamic::Type::DOUBLE); } else if (value == "null") { allowedTypes_.push_back(dynamic::Type::NULLT); } else if (value == "object") { allowedTypes_.push_back(dynamic::Type::OBJECT); } else if (value == "string") { allowedTypes_.push_back(dynamic::Type::STRING); } else { return; } if (!typeStr_.empty()) { typeStr_ += ", "; } typeStr_ += value.str(); }
void operator()(StringPiece msg, double sec) { m = msg.str(); t = sec; }
FormatKeyNotFoundException::FormatKeyNotFoundException(StringPiece key) : std::out_of_range(kMessagePrefix.str() + key.str()) {}
void FunctionScheduler::setThreadName(StringPiece threadName) { std::unique_lock<std::mutex> l(mutex_); threadName_ = threadName.str(); }
void trailingData(StringPiece data) { trailingData_ = data.str(); }
void AsyncFileWriter::writeMessage(StringPiece buffer, uint32_t flags) { return writeMessage(buffer.str(), flags); }
LogLevel stringToLogLevel(StringPiece name) { string lowerNameStr; lowerNameStr.reserve(name.size()); for (char c : name) { lowerNameStr.push_back(static_cast<char>(std::tolower(c))); } StringPiece lowerName{lowerNameStr}; // If the string is of the form "LogLevel::foo" or "LogLevel(foo)" // strip it down just to "foo". This makes sure we can process both // the "LogLevel::WARN" and "LogLevel(1234)" formats produced by // logLevelToString(). constexpr StringPiece lowercasePrefix{"loglevel::"}; constexpr StringPiece wrapperPrefix{"loglevel("}; if (lowerName.startsWith(lowercasePrefix)) { lowerName.advance(lowercasePrefix.size()); } else if (lowerName.startsWith(wrapperPrefix) && lowerName.endsWith(")")) { lowerName.advance(wrapperPrefix.size()); lowerName.subtract(1); } if (lowerName == "uninitialized") { return LogLevel::UNINITIALIZED; } else if (lowerName == "none") { return LogLevel::NONE; } else if (lowerName == "debug" || lowerName == "dbg") { return LogLevel::DBG; } else if (lowerName == "info") { return LogLevel::INFO; } else if (lowerName == "warn" || lowerName == "warning") { return LogLevel::WARN; } else if (lowerName == "error" || lowerName == "err") { return LogLevel::ERR; } else if (lowerName == "critical") { return LogLevel::CRITICAL; } else if (lowerName == "dfatal") { return LogLevel::DFATAL; } else if (lowerName == "fatal") { return LogLevel::FATAL; } else if (lowerName == "max" || lowerName == "max_level") { return LogLevel::MAX_LEVEL; } for (const auto& info : numberedLogLevels) { if (!lowerName.startsWith(info.lowerPrefix)) { continue; } auto remainder = lowerName.subpiece(info.lowerPrefix.size()); auto level = folly::tryTo<int>(remainder).value_or(-1); if (level < 0 || static_cast<unsigned int>(level) > (static_cast<uint32_t>(info.max) - static_cast<uint32_t>(info.min))) { throw std::range_error(to<string>( "invalid ", info.lowerPrefix, " logger level: ", name.str())); } return info.max - level; } // Try as an plain integer if all else fails try { auto level = folly::to<uint32_t>(lowerName); return static_cast<LogLevel>(level); } catch (const std::exception&) { throw std::range_error("invalid logger level: " + name.str()); } }
File::File(StringPiece name, int flags, mode_t mode) : File(name.str(), flags, mode) {}
AsyncFileWriter::AsyncFileWriter(StringPiece path) : AsyncFileWriter{File{path.str(), O_WRONLY | O_APPEND | O_CREAT}} {}