• 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 
29   static std::unique_ptr<TracedValue> Create();
30 
31   void EndDictionary();
32   void EndArray();
33 
34   // These methods assume that |name| is a long lived "quoted" string.
35   void SetInteger(const char* name, int value);
36   void SetDouble(const char* name, double value);
37   void SetBoolean(const char* name, bool value);
38   void SetString(const char* name, const char* value);
SetString(const char * name,const std::string & value)39   void SetString(const char* name, const std::string& value) {
40     SetString(name, value.c_str());
41   }
SetString(const char * name,std::unique_ptr<char[]> value)42   void SetString(const char* name, std::unique_ptr<char[]> value) {
43     SetString(name, value.get());
44   }
45   void SetValue(const char* name, TracedValue* value);
SetValue(const char * name,std::unique_ptr<TracedValue> value)46   void SetValue(const char* name, std::unique_ptr<TracedValue> value) {
47     SetValue(name, value.get());
48   }
49   void BeginDictionary(const char* name);
50   void BeginArray(const char* name);
51 
52   void AppendInteger(int);
53   void AppendDouble(double);
54   void AppendBoolean(bool);
55   void AppendString(const char*);
AppendString(const std::string & value)56   void AppendString(const std::string& value) { AppendString(value.c_str()); }
57   void BeginArray();
58   void BeginDictionary();
59 
60   // ConvertableToTraceFormat implementation.
61   void AppendAsTraceFormat(std::string* out) const override;
62 
63 #ifdef V8_USE_PERFETTO
64   // DebugAnnotation implementation.
65   void Add(perfetto::protos::pbzero::DebugAnnotation*) const override;
66 #endif  // V8_USE_PERFETTO
67 
68  private:
69   TracedValue();
70 
71   void WriteComma();
72   void WriteName(const char* name);
73 
74 #ifdef DEBUG
75   // In debug builds checks the pairings of {Begin,End}{Dictionary,Array}
76   std::vector<bool> nesting_stack_;
77 #endif
78 
79   std::string data_;
80   bool first_item_;
81 
82   DISALLOW_COPY_AND_ASSIGN(TracedValue);
83 };
84 
85 }  // namespace tracing
86 }  // namespace v8
87 
88 #endif  // V8_TRACING_TRACED_VALUE_H_
89