1 // Copyright (c) 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.
5 #include "base/debug/trace_event_argument.h"
7 #include "base/json/json_writer.h"
8 #include "base/values.h"
13 TracedValue::TracedValue() : root_(new DictionaryValue()) {
14 stack_
.push_back(root_
.get());
17 TracedValue::~TracedValue() {
18 DCHECK_EQ(1u, stack_
.size());
21 void TracedValue::SetInteger(const char* name
, int value
) {
22 GetCurrentDictionary()->SetInteger(name
, value
);
25 void TracedValue::SetDouble(const char* name
, double value
) {
26 GetCurrentDictionary()->SetDouble(name
, value
);
29 void TracedValue::SetBoolean(const char* name
, bool value
) {
30 GetCurrentDictionary()->SetBoolean(name
, value
);
33 void TracedValue::SetString(const char* name
, const std::string
& value
) {
34 GetCurrentDictionary()->SetString(name
, value
);
37 void TracedValue::SetValue(const char* name
, Value
* value
) {
38 GetCurrentDictionary()->Set(name
, value
);
41 void TracedValue::BeginDictionary(const char* name
) {
42 DictionaryValue
* dictionary
= new DictionaryValue();
43 GetCurrentDictionary()->Set(name
, dictionary
);
44 stack_
.push_back(dictionary
);
47 void TracedValue::BeginArray(const char* name
) {
48 ListValue
* array
= new ListValue();
49 GetCurrentDictionary()->Set(name
, array
);
50 stack_
.push_back(array
);
53 void TracedValue::EndDictionary() {
54 DCHECK_GT(stack_
.size(), 1u);
55 DCHECK(GetCurrentDictionary());
59 void TracedValue::AppendInteger(int value
) {
60 GetCurrentArray()->AppendInteger(value
);
63 void TracedValue::AppendDouble(double value
) {
64 GetCurrentArray()->AppendDouble(value
);
67 void TracedValue::AppendBoolean(bool value
) {
68 GetCurrentArray()->AppendBoolean(value
);
71 void TracedValue::AppendString(const std::string
& value
) {
72 GetCurrentArray()->AppendString(value
);
75 void TracedValue::BeginArray() {
76 ListValue
* array
= new ListValue();
77 GetCurrentArray()->Append(array
);
78 stack_
.push_back(array
);
81 void TracedValue::BeginDictionary() {
82 DictionaryValue
* dictionary
= new DictionaryValue();
83 GetCurrentArray()->Append(dictionary
);
84 stack_
.push_back(dictionary
);
87 void TracedValue::EndArray() {
88 DCHECK_GT(stack_
.size(), 1u);
89 DCHECK(GetCurrentArray());
93 DictionaryValue
* TracedValue::GetCurrentDictionary() {
94 DCHECK(!stack_
.empty());
95 DictionaryValue
* dictionary
= NULL
;
96 stack_
.back()->GetAsDictionary(&dictionary
);
101 ListValue
* TracedValue::GetCurrentArray() {
102 DCHECK(!stack_
.empty());
103 ListValue
* list
= NULL
;
104 stack_
.back()->GetAsList(&list
);
109 void TracedValue::AppendAsTraceFormat(std::string
* out
) const {
111 JSONWriter::Write(stack_
.front(), &tmp
);
113 DCHECK_EQ(1u, stack_
.size()) << tmp
;