/src/node/src/tracing/traced_value.h
Line  | Count  | Source  | 
1  |  | // Copyright 2016 the V8 project authors. All rights reserved.  | 
2  |  | // Use of this source code is governed by a BSD-style license that can be  | 
3  |  | // found in the LICENSE file.  | 
4  |  |  | 
5  |  | #ifndef SRC_TRACING_TRACED_VALUE_H_  | 
6  |  | #define SRC_TRACING_TRACED_VALUE_H_  | 
7  |  |  | 
8  |  | #include "v8-platform.h"  | 
9  |  |  | 
10  |  | #include <cstdint>  | 
11  |  | #include <span>  | 
12  |  | #include <string>  | 
13  |  |  | 
14  |  | namespace node { | 
15  |  | namespace tracing { | 
16  |  |  | 
17  |  | template <typename T>  | 
18  | 0  | std::unique_ptr<v8::ConvertableToTraceFormat> CastTracedValue(const T& value) { | 
19  | 0  |   return value.Cast();  | 
20  | 0  | } Unexecuted instantiation: std::__1::unique_ptr<v8::ConvertableToTraceFormat, std::__1::default_delete<v8::ConvertableToTraceFormat> > node::tracing::CastTracedValue<node::tracing::ProcessMeta>(node::tracing::ProcessMeta const&) Unexecuted instantiation: std::__1::unique_ptr<v8::ConvertableToTraceFormat, std::__1::default_delete<v8::ConvertableToTraceFormat> > node::tracing::CastTracedValue<node::tracing::AsyncWrapArgs>(node::tracing::AsyncWrapArgs const&) Unexecuted instantiation: std::__1::unique_ptr<v8::ConvertableToTraceFormat, std::__1::default_delete<v8::ConvertableToTraceFormat> > node::tracing::CastTracedValue<node::tracing::EnvironmentArgs>(node::tracing::EnvironmentArgs const&)  | 
21  |  |  | 
22  |  | class EnvironmentArgs { | 
23  |  |  public:  | 
24  |  |   EnvironmentArgs(std::span<const std::string> args,  | 
25  |  |                   std::span<const std::string> exec_args)  | 
26  | 0  |       : args_(args), exec_args_(exec_args) {} | 
27  |  |  | 
28  |  |   std::unique_ptr<v8::ConvertableToTraceFormat> Cast() const;  | 
29  |  |  | 
30  |  |  private:  | 
31  |  |   std::span<const std::string> args_;  | 
32  |  |   std::span<const std::string> exec_args_;  | 
33  |  | };  | 
34  |  |  | 
35  |  | class AsyncWrapArgs { | 
36  |  |  public:  | 
37  |  |   AsyncWrapArgs(int64_t execution_async_id, int64_t trigger_async_id)  | 
38  | 0  |       : execution_async_id_(execution_async_id),  | 
39  | 0  |         trigger_async_id_(trigger_async_id) {} | 
40  |  |  | 
41  |  |   std::unique_ptr<v8::ConvertableToTraceFormat> Cast() const;  | 
42  |  |  | 
43  |  |  private:  | 
44  |  |   int64_t execution_async_id_;  | 
45  |  |   int64_t trigger_async_id_;  | 
46  |  | };  | 
47  |  |  | 
48  |  | class ProcessMeta { | 
49  |  |  public:  | 
50  |  |   std::unique_ptr<v8::ConvertableToTraceFormat> Cast() const;  | 
51  |  | };  | 
52  |  |  | 
53  |  | // Do not use this class directly. Define a custom structured class to provide  | 
54  |  | // a conversion method so that the class can be used with both V8 legacy  | 
55  |  | // trace API and perfetto API.  | 
56  |  | //  | 
57  |  | // These classes provide a JSON-inspired way to write structed data into traces.  | 
58  |  | //  | 
59  |  | // To define how a custom class should be written into the trace, users should  | 
60  |  | // define one of the two following functions:  | 
61  |  | // - Foo::Cast(TracedValue) const  | 
62  |  | //   (preferred for code which depends on perfetto directly)  | 
63  |  | //  | 
64  |  | // After defining a conversion method, the object can be used as a  | 
65  |  | // TRACE_EVENT argument:  | 
66  |  | //  | 
67  |  | // Foo foo;  | 
68  |  | // TRACE_EVENT("cat", "Event", "arg", CastTracedValue(foo)); | 
69  |  | //  | 
70  |  | // class Foo { | 
71  |  | //   std::unique_ptr<v8::ConvertableToTraceFormat> Cast() const { | 
72  |  | //     auto traced_value = tracing::TracedValue::Create();  | 
73  |  | //     dict->SetInteger("key", 42); | 
74  |  | //     dict->SetString("foo", "bar"); | 
75  |  | //     return traced_value;  | 
76  |  | //   }  | 
77  |  | // }  | 
78  |  | class TracedValue : public v8::ConvertableToTraceFormat { | 
79  |  |  public:  | 
80  | 0  |   ~TracedValue() override = default;  | 
81  |  |  | 
82  |  |   static std::unique_ptr<TracedValue> Create();  | 
83  |  |   static std::unique_ptr<TracedValue> CreateArray();  | 
84  |  |  | 
85  |  |   void EndDictionary();  | 
86  |  |   void EndArray();  | 
87  |  |  | 
88  |  |   // These methods assume that |name| is a long lived "quoted" string.  | 
89  |  |   void SetInteger(const char* name, int value);  | 
90  |  |   void SetDouble(const char* name, double value);  | 
91  |  |   void SetBoolean(const char* name, bool value);  | 
92  |  |   void SetNull(const char* name);  | 
93  |  |   void SetString(const char* name, const char* value);  | 
94  | 0  |   void SetString(const char* name, const std::string& value) { | 
95  | 0  |     SetString(name, value.c_str());  | 
96  | 0  |   }  | 
97  |  |   void BeginDictionary(const char* name);  | 
98  |  |   void BeginArray(const char* name);  | 
99  |  |  | 
100  |  |   void AppendInteger(int);  | 
101  |  |   void AppendDouble(double);  | 
102  |  |   void AppendBoolean(bool);  | 
103  |  |   void AppendNull();  | 
104  |  |   void AppendString(const char*);  | 
105  | 0  |   void AppendString(const std::string& value) { AppendString(value.c_str()); } | 
106  |  |   void BeginArray();  | 
107  |  |   void BeginDictionary();  | 
108  |  |  | 
109  |  |   // ConvertableToTraceFormat implementation.  | 
110  |  |   void AppendAsTraceFormat(std::string* out) const override;  | 
111  |  |  | 
112  |  |   TracedValue(const TracedValue&) = delete;  | 
113  |  |   TracedValue& operator=(const TracedValue&) = delete;  | 
114  |  |  | 
115  |  |  private:  | 
116  |  |   explicit TracedValue(bool root_is_array = false);  | 
117  |  |  | 
118  |  |   void WriteComma();  | 
119  |  |   void WriteName(const char* name);  | 
120  |  |  | 
121  |  |   std::string data_;  | 
122  |  |   bool first_item_;  | 
123  |  |   bool root_is_array_;  | 
124  |  | };  | 
125  |  |  | 
126  |  | }  // namespace tracing  | 
127  |  | }  // namespace node  | 
128  |  |  | 
129  |  | #endif  // SRC_TRACING_TRACED_VALUE_H_  |