1 // Copyright 2021 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "fcp/tracing/tracing_traits.h" 16 17 #include <memory> 18 #include <string> 19 #include <utility> 20 21 #include "absl/base/attributes.h" 22 #include "absl/container/flat_hash_map.h" 23 24 namespace fcp { 25 26 class UnknownTracingTraits : public TracingTraitsBase { 27 public: Name() const28 ABSL_MUST_USE_RESULT const char* Name() const override { return "?Unknown?"; } Severity() const29 ABSL_MUST_USE_RESULT TracingSeverity Severity() const override { 30 return TracingSeverity::kWarning; 31 }; TextFormat(const flatbuffers::DetachedBuffer & buf) const32 ABSL_MUST_USE_RESULT std::string TextFormat( 33 const flatbuffers::DetachedBuffer& buf) const override { 34 return ""; 35 }; JsonStringFormat(const uint8_t * flatbuf_bytes) const36 ABSL_MUST_USE_RESULT std::string JsonStringFormat( 37 const uint8_t* flatbuf_bytes) const override { 38 return ""; 39 }; 40 }; 41 42 absl::flat_hash_map<TracingTag, std::unique_ptr<TracingTraitsBase>>& GetTracingTraitsRegistry()43GetTracingTraitsRegistry() { 44 static auto tracing_traits_registry = 45 new absl::flat_hash_map<TracingTag, std::unique_ptr<TracingTraitsBase>>(); 46 return *tracing_traits_registry; 47 } 48 Lookup(TracingTag tag)49TracingTraitsBase const* TracingTraitsBase::Lookup(TracingTag tag) { 50 auto it = GetTracingTraitsRegistry().find(tag); 51 if (it == GetTracingTraitsRegistry().end()) { 52 static auto unknown_tracing_traits = new UnknownTracingTraits(); 53 return unknown_tracing_traits; 54 } 55 return it->second.get(); 56 } 57 Register(TracingTag tag,std::unique_ptr<TracingTraitsBase> traits)58void TracingTraitsBase::Register(TracingTag tag, 59 std::unique_ptr<TracingTraitsBase> traits) { 60 GetTracingTraitsRegistry()[tag] = std::move(traits); 61 } 62 63 } // namespace fcp 64