traced_value.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2014 The Chromium 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. #ifndef BASE_TRACE_EVENT_TRACED_VALUE_H_
  5. #define BASE_TRACE_EVENT_TRACED_VALUE_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/macros.h"
  11. #include "base/pickle.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/trace_event/trace_event_impl.h"
  14. namespace base {
  15. class Value;
  16. namespace trace_event {
  17. class BASE_EXPORT TracedValue : public ConvertableToTraceFormat {
  18. public:
  19. // TODO(oysteine): |capacity| is not used in any production code. Consider
  20. // removing it.
  21. explicit TracedValue(size_t capacity = 0);
  22. ~TracedValue() override;
  23. void EndDictionary();
  24. void EndArray();
  25. // These methods assume that |name| is a long lived "quoted" string.
  26. void SetInteger(const char* name, int value);
  27. void SetDouble(const char* name, double value);
  28. void SetBoolean(const char* name, bool value);
  29. void SetString(const char* name, base::StringPiece value);
  30. void SetValue(const char* name, TracedValue* value);
  31. void BeginDictionary(const char* name);
  32. void BeginArray(const char* name);
  33. // These, instead, can be safely passed a temporary string.
  34. void SetIntegerWithCopiedName(base::StringPiece name, int value);
  35. void SetDoubleWithCopiedName(base::StringPiece name, double value);
  36. void SetBooleanWithCopiedName(base::StringPiece name, bool value);
  37. void SetStringWithCopiedName(base::StringPiece name, base::StringPiece value);
  38. void SetValueWithCopiedName(base::StringPiece name, TracedValue* value);
  39. void BeginDictionaryWithCopiedName(base::StringPiece name);
  40. void BeginArrayWithCopiedName(base::StringPiece name);
  41. void AppendInteger(int);
  42. void AppendDouble(double);
  43. void AppendBoolean(bool);
  44. void AppendString(base::StringPiece);
  45. void BeginArray();
  46. void BeginDictionary();
  47. // ConvertableToTraceFormat implementation.
  48. void AppendAsTraceFormat(std::string* out) const override;
  49. bool AppendToProto(ProtoAppender* appender) override;
  50. void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead) override;
  51. // A custom serialization class can be supplied by implementing the
  52. // Writer interface and supplying a factory class to SetWriterFactoryCallback.
  53. // Primarily used by Perfetto to write TracedValues directly into its proto
  54. // format, which lets us do a direct memcpy() in AppendToProto() rather than
  55. // a JSON serialization step in AppendAsTraceFormat.
  56. class BASE_EXPORT Writer {
  57. public:
  58. virtual ~Writer() = default;
  59. virtual void BeginArray() = 0;
  60. virtual void BeginDictionary() = 0;
  61. virtual void EndDictionary() = 0;
  62. virtual void EndArray() = 0;
  63. // These methods assume that |name| is a long lived "quoted" string.
  64. virtual void SetInteger(const char* name, int value) = 0;
  65. virtual void SetDouble(const char* name, double value) = 0;
  66. virtual void SetBoolean(const char* name, bool value) = 0;
  67. virtual void SetString(const char* name, base::StringPiece value) = 0;
  68. virtual void SetValue(const char* name, Writer* value) = 0;
  69. virtual void BeginDictionary(const char* name) = 0;
  70. virtual void BeginArray(const char* name) = 0;
  71. // These, instead, can be safely passed a temporary string.
  72. virtual void SetIntegerWithCopiedName(base::StringPiece name,
  73. int value) = 0;
  74. virtual void SetDoubleWithCopiedName(base::StringPiece name,
  75. double value) = 0;
  76. virtual void SetBooleanWithCopiedName(base::StringPiece name,
  77. bool value) = 0;
  78. virtual void SetStringWithCopiedName(base::StringPiece name,
  79. base::StringPiece value) = 0;
  80. virtual void SetValueWithCopiedName(base::StringPiece name,
  81. Writer* value) = 0;
  82. virtual void BeginDictionaryWithCopiedName(base::StringPiece name) = 0;
  83. virtual void BeginArrayWithCopiedName(base::StringPiece name) = 0;
  84. virtual void AppendInteger(int) = 0;
  85. virtual void AppendDouble(double) = 0;
  86. virtual void AppendBoolean(bool) = 0;
  87. virtual void AppendString(base::StringPiece) = 0;
  88. virtual void AppendAsTraceFormat(std::string* out) const = 0;
  89. virtual bool AppendToProto(ProtoAppender* appender);
  90. virtual void EstimateTraceMemoryOverhead(
  91. TraceEventMemoryOverhead* overhead) = 0;
  92. virtual bool IsPickleWriter() const = 0;
  93. virtual bool IsProtoWriter() const = 0;
  94. };
  95. typedef std::unique_ptr<Writer> (*WriterFactoryCallback)(size_t capacity);
  96. static void SetWriterFactoryCallback(WriterFactoryCallback callback);
  97. protected:
  98. TracedValue(size_t capacity, bool forced_json);
  99. std::unique_ptr<base::Value> ToBaseValue() const;
  100. private:
  101. std::unique_ptr<Writer> writer_;
  102. #ifndef NDEBUG
  103. // In debug builds checks the pairings of {Start,End}{Dictionary,Array}
  104. std::vector<bool> nesting_stack_;
  105. #endif
  106. DISALLOW_COPY_AND_ASSIGN(TracedValue);
  107. };
  108. // TracedValue that is convertable to JSON format. This has lower performance
  109. // than the default TracedValue in production code, and should be used only for
  110. // testing and debugging. Should be avoided in tracing. It's for
  111. // testing/debugging code calling value dumping function designed for tracing,
  112. // like the following:
  113. //
  114. // TracedValueJSON value;
  115. // AsValueInto(&value); // which is designed for tracing.
  116. // return value.ToJSON();
  117. //
  118. // If the code is merely for testing/debugging, base::Value should be used
  119. // instead.
  120. class BASE_EXPORT TracedValueJSON : public TracedValue {
  121. public:
  122. explicit TracedValueJSON(size_t capacity = 0)
  123. : TracedValue(capacity, /*forced_josn*/ true) {}
  124. using TracedValue::ToBaseValue;
  125. // Converts the value into a JSON string without formatting. Suitable for
  126. // printing a simple value or printing a value in a single line context.
  127. std::string ToJSON() const;
  128. // Converts the value into a formatted JSON string, with indentation, spaces
  129. // and new lines for better human readability of complex values.
  130. std::string ToFormattedJSON() const;
  131. };
  132. } // namespace trace_event
  133. } // namespace base
  134. #endif // BASE_TRACE_EVENT_TRACED_VALUE_H_