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. 4 5 #include "base/debug/trace_event_argument.h" 6 7 #include "base/json/json_writer.h" 8 #include "base/values.h" 9 10 namespace base { 11 namespace debug { 12 13 TracedValue::TracedValue() : root_(new DictionaryValue()) { 14 stack_.push_back(root_.get()); 15 } 16 17 TracedValue::~TracedValue() { 18 DCHECK_EQ(1u, stack_.size()); 19 } 20 21 void TracedValue::SetInteger(const char* name, int value) { 22 GetCurrentDictionary()->SetInteger(name, value); 23 } 24 25 void TracedValue::SetDouble(const char* name, double value) { 26 GetCurrentDictionary()->SetDouble(name, value); 27 } 28 29 void TracedValue::SetBoolean(const char* name, bool value) { 30 GetCurrentDictionary()->SetBoolean(name, value); 31 } 32 33 void TracedValue::SetString(const char* name, const std::string& value) { 34 GetCurrentDictionary()->SetString(name, value); 35 } 36 37 void TracedValue::SetValue(const char* name, Value* value) { 38 GetCurrentDictionary()->Set(name, value); 39 } 40 41 void TracedValue::BeginDictionary(const char* name) { 42 DictionaryValue* dictionary = new DictionaryValue(); 43 GetCurrentDictionary()->Set(name, dictionary); 44 stack_.push_back(dictionary); 45 } 46 47 void TracedValue::BeginArray(const char* name) { 48 ListValue* array = new ListValue(); 49 GetCurrentDictionary()->Set(name, array); 50 stack_.push_back(array); 51 } 52 53 void TracedValue::EndDictionary() { 54 DCHECK_GT(stack_.size(), 1u); 55 DCHECK(GetCurrentDictionary()); 56 stack_.pop_back(); 57 } 58 59 void TracedValue::AppendInteger(int value) { 60 GetCurrentArray()->AppendInteger(value); 61 } 62 63 void TracedValue::AppendDouble(double value) { 64 GetCurrentArray()->AppendDouble(value); 65 } 66 67 void TracedValue::AppendBoolean(bool value) { 68 GetCurrentArray()->AppendBoolean(value); 69 } 70 71 void TracedValue::AppendString(const std::string& value) { 72 GetCurrentArray()->AppendString(value); 73 } 74 75 void TracedValue::BeginArray() { 76 ListValue* array = new ListValue(); 77 GetCurrentArray()->Append(array); 78 stack_.push_back(array); 79 } 80 81 void TracedValue::BeginDictionary() { 82 DictionaryValue* dictionary = new DictionaryValue(); 83 GetCurrentArray()->Append(dictionary); 84 stack_.push_back(dictionary); 85 } 86 87 void TracedValue::EndArray() { 88 DCHECK_GT(stack_.size(), 1u); 89 DCHECK(GetCurrentArray()); 90 stack_.pop_back(); 91 } 92 93 DictionaryValue* TracedValue::GetCurrentDictionary() { 94 DCHECK(!stack_.empty()); 95 DictionaryValue* dictionary = NULL; 96 stack_.back()->GetAsDictionary(&dictionary); 97 DCHECK(dictionary); 98 return dictionary; 99 } 100 101 ListValue* TracedValue::GetCurrentArray() { 102 DCHECK(!stack_.empty()); 103 ListValue* list = NULL; 104 stack_.back()->GetAsList(&list); 105 DCHECK(list); 106 return list; 107 } 108 109 void TracedValue::AppendAsTraceFormat(std::string* out) const { 110 std::string tmp; 111 JSONWriter::Write(stack_.front(), &tmp); 112 *out += tmp; 113 DCHECK_EQ(1u, stack_.size()) << tmp; 114 } 115 116 } // namespace debug 117 } // namespace base 118