sapling/eden/fs/telemetry/LogEvent.cpp
Chad Austin b26418ef01 migrate to throw_ and throwf
Summary:
I've always been annoyed at needing to include folly/Conv.h or fmt to
throw a formatted exception. And while exceptions are rarely in the
hot path, it's still worth having the option to optimize the
formatting and exception allocation step at some point in the future.

I'm sure I missed some, but this diff migrates most of our
`throw T{fmt::format(...)}` patterns to `throwf` and
`throw T{folly::to<std::string>(...)}` to `throw_`.

Reviewed By: genevievehelsel

Differential Revision: D37229822

fbshipit-source-id: c053b5cdaed99e58c485afd2a99be98828f07657
2022-07-08 13:30:53 -07:00

56 lines
1.6 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "eden/fs/telemetry/LogEvent.h"
#include <folly/Conv.h>
#include <folly/Unicode.h>
#include <folly/logging/xlog.h>
#include "eden/fs/utils/Throw.h"
namespace {
void validateUtf8(folly::StringPiece sp) {
auto* p = reinterpret_cast<const unsigned char*>(sp.begin());
auto* const end = reinterpret_cast<const unsigned char*>(sp.end());
while (p < end) {
(void)folly::utf8ToCodePoint(p, end, false);
}
}
} // namespace
namespace facebook::eden {
void DynamicEvent::addInt(std::string name, int64_t value) {
auto [iter, inserted] = ints_.emplace(std::move(name), value);
if (!inserted) {
throw_<std::logic_error>(
"Attempted to insert duplicate int: ", iter->first);
}
}
void DynamicEvent::addString(std::string name, std::string value) {
validateUtf8(value);
auto [iter, inserted] = strings_.emplace(std::move(name), std::move(value));
if (!inserted) {
throw_<std::logic_error>(
"Attempted to insert duplicate string: ", iter->first);
}
}
void DynamicEvent::addDouble(std::string name, double value) {
XCHECK(std::isfinite(value))
<< "Attempted to insert double-precision value that cannot be represented in JSON: "
<< name;
auto [iter, inserted] = doubles_.emplace(std::move(name), value);
if (!inserted) {
throw_<std::logic_error>(
"Attempted to insert duplicate double: ", iter->first);
}
}
} // namespace facebook::eden