1 // Copyright 2019 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 #ifndef FCP_TRACING_TRACING_TRAITS_H_ 16 #define FCP_TRACING_TRACING_TRAITS_H_ 17 18 #include <memory> 19 #include <string> 20 21 #include "absl/base/attributes.h" 22 #include "fcp/tracing/tracing_severity.h" 23 #include "fcp/tracing/tracing_tag.h" 24 #include "flatbuffers/flatbuffers.h" 25 26 namespace fcp { 27 28 // Base class for tracing traits, allows working with generated tracing traits 29 // (see below) in polymorphic way at runtime without knowledge of a type. 30 class TracingTraitsBase { 31 public: 32 // Returns printable name of the FlatBuffers table 33 ABSL_MUST_USE_RESULT virtual const char* Name() const = 0; 34 // Returns printable severity of the event represented by this table. 35 ABSL_MUST_USE_RESULT virtual TracingSeverity Severity() const = 0; 36 // Formats a serialized flatbuffer into human readable format. 37 ABSL_MUST_USE_RESULT virtual std::string TextFormat( 38 const flatbuffers::DetachedBuffer& buf) const = 0; 39 // Formats a serialized flatbuffer into a Json string. 40 ABSL_MUST_USE_RESULT virtual std::string JsonStringFormat( 41 const uint8_t* flatbuf_bytes) const = 0; 42 43 // Allows to lookup FlatBuffers table traits by its 4-character tag. 44 // For unknown tag returns a stub. 45 static TracingTraitsBase const* Lookup(TracingTag tag); 46 47 // Registers runtime trait information (to be used for generated code only). 48 // Multiple compilation units are allowed to register the same traits, 49 // last registration wins. 50 static void Register(TracingTag tag, 51 std::unique_ptr<TracingTraitsBase> trait); 52 SeverityString(const TracingSeverity tracing_severity)53 static std::string SeverityString(const TracingSeverity tracing_severity) { 54 switch (tracing_severity) { 55 case TracingSeverity::kInfo: 56 return "INFO"; 57 case TracingSeverity::kWarning: 58 return "WARNING"; 59 case TracingSeverity::kError: 60 return "ERROR"; 61 } 62 } 63 64 virtual ~TracingTraitsBase() = default; 65 }; 66 67 // Specializations of TracingTraits used by TracingSpan are typically included 68 // into user code together with the definitions of concrete FlatBufferTable via 69 // "tracing_schema.h" header. The latter is auto-generated by 70 // tracing_traits_generator tool from "tracing_schema.fbs". 71 template <class FlatBufferTable> 72 class TracingTraits; 73 74 namespace internal { 75 76 // Helper class to for automatic registration of traits for runtime use. 77 // This is intended to be used from autogenerated code only. 78 template<class FlatBufferTable> 79 struct TracingTraitsRegistrar { TracingTraitsRegistrarTracingTraitsRegistrar80 TracingTraitsRegistrar() { 81 TracingTraitsBase::Register( 82 TracingTraits<FlatBufferTable>::kTag, 83 std::make_unique<TracingTraits<FlatBufferTable>>()); 84 } 85 }; 86 87 } // namespace internal 88 89 } // namespace fcp 90 91 #endif // FCP_TRACING_TRACING_TRAITS_H_ 92