Fix potential segfault when init changes enabled

This commit is contained in:
mpilman 2019-03-14 22:15:27 -07:00
parent c008e16c81
commit ae84f5424a
1 changed files with 5 additions and 9 deletions

View File

@ -211,8 +211,7 @@ struct TraceEvent {
template<class T> template<class T>
typename std::enable_if<Traceable<T>::value, TraceEvent&>::type typename std::enable_if<Traceable<T>::value, TraceEvent&>::type
detail( std::string&& key, const T& value ) { detail( std::string&& key, const T& value ) {
if (enabled) { if (enabled && init()) {
init();
auto s = Traceable<T>::toString(value); auto s = Traceable<T>::toString(value);
addMetric(key.c_str(), value, s); addMetric(key.c_str(), value, s);
return detailImpl(std::move(key), std::move(s)); return detailImpl(std::move(key), std::move(s));
@ -223,8 +222,7 @@ struct TraceEvent {
template<class T> template<class T>
typename std::enable_if<Traceable<T>::value, TraceEvent&>::type typename std::enable_if<Traceable<T>::value, TraceEvent&>::type
detail( const char* key, const T& value ) { detail( const char* key, const T& value ) {
if (enabled) { if (enabled && init()) {
init();
auto s = Traceable<T>::toString(value); auto s = Traceable<T>::toString(value);
addMetric(key, value, s); addMetric(key, value, s);
return detailImpl(std::string(key), std::move(s), false); return detailImpl(std::string(key), std::move(s), false);
@ -234,15 +232,14 @@ struct TraceEvent {
template<class T> template<class T>
typename std::enable_if<std::is_enum<T>::value, TraceEvent&>::type typename std::enable_if<std::is_enum<T>::value, TraceEvent&>::type
detail(const char* key, T value) { detail(const char* key, T value) {
if (enabled) { if (enabled && init()) {
init();
setField(key, int64_t(value)); setField(key, int64_t(value));
return detailImpl(std::string(key), format("%d", value), false); return detailImpl(std::string(key), format("%d", value), false);
} }
return *this; return *this;
} }
TraceEvent& detail(std::string key, std::string value) { TraceEvent& detail(std::string key, std::string value) {
if (enabled) { if (enabled && init()) {
init(); init();
addMetric(key.c_str(), value, value); addMetric(key.c_str(), value, value);
return detailImpl(std::string(key), std::move(value), false); return detailImpl(std::string(key), std::move(value), false);
@ -250,8 +247,7 @@ struct TraceEvent {
return *this; return *this;
} }
TraceEvent& detail(const char* key, std::string value) { TraceEvent& detail(const char* key, std::string value) {
if (enabled) { if (enabled && init()) {
init();
addMetric(key, value, value); addMetric(key, value, value);
return detailImpl(std::string(key), std::move(value), false); return detailImpl(std::string(key), std::move(value), false);
} }