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_TEXT_TRACING_RECORDER_IMPL_H_ 16 #define FCP_TRACING_TEXT_TRACING_RECORDER_IMPL_H_ 17 18 #include <flatbuffers/flatbuffers.h> 19 20 #include <fstream> 21 #include <iostream> 22 #include <optional> 23 #include <string> 24 #include <vector> 25 26 #include "absl/time/time.h" 27 #include "fcp/tracing/tracing_recorder.h" 28 #include "fcp/tracing/tracing_recorder_impl.h" 29 30 namespace fcp { 31 namespace tracing_internal { 32 33 class TextTracingSpanImpl; 34 // Basic tracing API implementation that writes begin span, end span, and log 35 // events to a stream in a human-readable text format. 36 class TextTracingRecorderImpl : public TracingRecorderImpl { 37 public: 38 TextTracingRecorderImpl(const std::string& filename, 39 absl::TimeZone time_zone); 40 41 ~TextTracingRecorderImpl() override; 42 43 // Constructs a recorder implementation that writes tracing events to stderr. 44 explicit TextTracingRecorderImpl(absl::TimeZone time_zone); 45 46 // Creates a root span from which child tracing spans can be created. 47 TracingSpanImpl* GetRootSpan() override; 48 49 // Trace an event represented by the flatbuffer. 50 void TraceImpl(TracingSpanId span_id, flatbuffers::DetachedBuffer&& buf, 51 const TracingTraitsBase& traits) override; 52 53 // Log that the tracing span represented by the flatbuffer is starting. 54 void BeginSpan(TracingSpanId id, TracingSpanId parent_id, 55 absl::string_view name, absl::string_view text_format); 56 57 // Log that the tracing span represented by the provided flatbuf is finished. 58 void EndSpan(TracingSpanId id, absl::string_view name, 59 absl::string_view text_format); 60 61 // Creates instance of the child tracing span with the parent span ID and 62 // tracing data provided 63 std::unique_ptr<TracingSpanImpl> CreateChildSpan( 64 TracingSpanId parent_span_id, flatbuffers::DetachedBuffer&& buf, 65 const TracingTraitsBase& traits) override; 66 67 private: 68 // Log a timestamp of the current time to the filestream. 69 void LogTime(); 70 // Common method for logging begin or end of a span. 71 void LogSpan(bool begin, TracingSpanId id, TracingSpanId parent_id, 72 absl::string_view name, absl::string_view text_format); 73 74 // File stream which is present only if this tracing recorder was constructed 75 // to write to a file. Should not be written to directly. This field is 76 // present because this class must own the filestream, since an instance of 77 // this class can be shared by many tracing spans, some of which may outlive 78 // the function that originally created the root tracing span. 79 std::optional<std::ofstream> fstream_; 80 81 // Pointer to an output stream to which tracing events are written. 82 std::ostream* stream_; 83 84 absl::TimeZone time_zone_; 85 std::unique_ptr<TextTracingSpanImpl> root_span_; 86 }; 87 88 } // namespace tracing_internal 89 } // namespace fcp 90 91 #endif // FCP_TRACING_TEXT_TRACING_RECORDER_IMPL_H_ 92