• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 V8_TRACING_TRACED_VALUE_H_
6 #define V8_TRACING_TRACED_VALUE_H_
7 
8 #include <stddef.h>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include "include/v8-platform.h"
14 #include "src/base/macros.h"
15 #include "src/tracing/trace-event.h"
16 
17 namespace v8 {
18 namespace tracing {
19 
20 class V8_EXPORT_PRIVATE TracedValue : public ConvertableToTraceFormat
21 #ifdef V8_USE_PERFETTO
22     ,
23                                       public perfetto::DebugAnnotation
24 #endif  // V8_USE_PERFETTO
25 {
26  public:
27   ~TracedValue() override;
28   TracedValue(const TracedValue&) = delete;
29   TracedValue& operator=(const TracedValue&) = delete;
30 
31   static std::unique_ptr<TracedValue> Create();
32 
33   void EndDictionary();
34   void EndArray();
35 
36   // These methods assume that |name| is a long lived "quoted" string.
37   void SetInteger(const char* name, int value);
38   void SetDouble(const char* name, double value);
39   void SetBoolean(const char* name, bool value);
40   void SetString(const char* name, const char* value);
SetString(const char * name,const std::string & value)41   void SetString(const char* name, const std::string& value) {
42     SetString(name, value.c_str());
43   }
SetString(const char * name,std::unique_ptr<char[]> value)44   void SetString(const char* name, std::unique_ptr<char[]> value) {
45     SetString(name, value.get());
46   }
47   void SetValue(const char* name, TracedValue* value);
SetValue(const char * name,std::unique_ptr<TracedValue> value)48   void SetValue(const char* name, std::unique_ptr<TracedValue> value) {
49     SetValue(name, value.get());
50   }
51   void BeginDictionary(const char* name);
52   void BeginArray(const char* name);
53 
54   void AppendInteger(int);
55   void AppendDouble(double);
56   void AppendBoolean(bool);
57   void AppendString(const char*);
AppendString(const std::string & value)58   void AppendString(const std::string& value) { AppendString(value.c_str()); }
59   void BeginArray();
60   void BeginDictionary();
61 
62   // ConvertableToTraceFormat implementation.
63   void AppendAsTraceFormat(std::string* out) const override;
64 
65 #ifdef V8_USE_PERFETTO
66   // DebugAnnotation implementation.
67   void Add(perfetto::protos::pbzero::DebugAnnotation*) const override;
68 #endif  // V8_USE_PERFETTO
69 
70  private:
71   TracedValue();
72 
73   void WriteComma();
74   void WriteName(const char* name);
75 
76 #ifdef DEBUG
77   // In debug builds checks the pairings of {Begin,End}{Dictionary,Array}
78   std::vector<bool> nesting_stack_;
79 #endif
80 
81   std::string data_;
82   bool first_item_;
83 };
84 
85 }  // namespace tracing
86 }  // namespace v8
87 
88 #endif  // V8_TRACING_TRACED_VALUE_H_
89