Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 #ifndef BASE_DEBUG_TRACE_EVENT_VALUE_H_ | |
| 6 #define BASE_DEBUG_TRACE_EVENT_VALUE_H_ | |
| 7 | |
| 8 #include <stack> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/debug/trace_event.h" | |
| 12 #include "base/debug/trace_event_object.h" | |
| 13 #include "base/format_macros.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/pickle.h" | |
| 16 #include "base/threading/non_thread_safe.h" | |
| 17 | |
| 18 namespace base { | |
| 19 namespace debug { | |
| 20 | |
| 21 // TracedValue has several functions to push data that will be converted to | |
| 22 // trace event format without depending on base::Value objects. | |
| 23 class BASE_EXPORT TracedValue : public base::debug::ConvertableToTraceFormat, | |
| 24 public base::NonThreadSafe { | |
| 25 public: | |
| 26 TracedValue(); | |
| 27 virtual ~TracedValue(); | |
| 28 | |
| 29 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE; | |
| 30 | |
| 31 // Begin a dictionary, it can either be a top-level structure or a value in | |
| 32 // an array. | |
| 33 void BeginDictionary(); | |
| 34 // Used for both BeginDictionary() and PushDictionary(). | |
| 35 void EndDictionary(); | |
| 36 // Begin an array, that is either a top-level structure or a value in an | |
| 37 // array. | |
| 38 void BeginArray(); | |
| 39 // Used for both BeginArray() and PushArray(). | |
| 40 void EndArray(); | |
| 41 | |
| 42 // Put stuff in dictionary using statically allocated string keys. | |
| 43 // Begins a dictionary, that is an entry within another dictionary. | |
| 44 void PushDictionary(const char* key_in_parent_dict); | |
| 45 // Begins an array, that is an entry within a dictonary. | |
| 46 void PushArray(const char* key_in_parent_dict); | |
| 47 #define TRACE_PUSH(type) \ | |
| 48 void Push(const char* key, type value) { \ | |
| 49 DCHECK_EQ(BEGIN_DICTIONARY, begin_opcodes_.top()); \ | |
| 50 BeginDictionaryEntry(); \ | |
| 51 PushKeyToPickle(key); \ | |
| 52 PushValueToPickle(value); \ | |
| 53 EndDictionaryEntry(); \ | |
| 54 } | |
| 55 TRACE_PUSH(const char*) | |
| 56 TRACE_PUSH(const std::string&) | |
| 57 TRACE_PUSH(int32) | |
| 58 TRACE_PUSH(int64) | |
| 59 TRACE_PUSH(uint32) | |
| 60 TRACE_PUSH(uint64) | |
| 61 TRACE_PUSH(float) | |
| 62 TRACE_PUSH(bool) | |
| 63 TRACE_PUSH(const TracedObject&) | |
| 64 #undef TRACE_PUSH | |
| 65 | |
| 66 // Put stuff in an array. | |
| 67 #define TRACE_PUSH(type) \ | |
| 68 void Push(type value) { \ | |
| 69 DCHECK_EQ(BEGIN_ARRAY, begin_opcodes_.top()); \ | |
| 70 PushValueToPickle(value); \ | |
| 71 } | |
| 72 TRACE_PUSH(const char*) | |
| 73 TRACE_PUSH(const std::string&) | |
| 74 TRACE_PUSH(int32) | |
| 75 TRACE_PUSH(int64) | |
| 76 TRACE_PUSH(uint32) | |
| 77 TRACE_PUSH(uint64) | |
| 78 TRACE_PUSH(float) | |
| 79 TRACE_PUSH(bool) | |
| 80 TRACE_PUSH(const TracedObject&) | |
| 81 #undef TRACE_PUSH | |
| 82 | |
| 83 private: | |
| 84 // Begin and end dictionary entry methods allow us to know that we're writing | |
| 85 // a dictionary entry, this is needed for scenarios where we don't write the | |
| 86 // value write away, but defer to some other implementation how to write the | |
|
dsinclair
2013/08/12 13:34:40
s/write/right
| |
| 87 // entry's value for example with TracedObject.PushInto(). | |
| 88 void BeginDictionaryEntry(); | |
| 89 void EndDictionaryEntry(); | |
| 90 // Push key/value should not be exposed, instead, the Push methods are | |
| 91 // exposed, to have them assert that we are in the right state before writing | |
| 92 // to the pickle. | |
| 93 void PushKeyToPickle(const char* key); | |
| 94 void PushValueToPickle(const char* value); | |
| 95 void PushValueToPickle(const std::string& value); | |
| 96 void PushValueToPickle(int value); | |
| 97 void PushValueToPickle(int64 value); | |
| 98 void PushValueToPickle(uint32 value); | |
| 99 void PushValueToPickle(uint64 value); | |
| 100 void PushValueToPickle(float value); | |
| 101 void PushValueToPickle(bool value); | |
| 102 void PushValueToPickle(const TracedObject& value); | |
| 103 | |
| 104 // Helper methods used in AppendAsTraceFormat(). | |
| 105 void AppendKeyAsRawPointer(std::string* out, PickleIterator* iter) const; | |
| 106 void AppendValueAsRawPointer(std::string* out, PickleIterator* iter) const; | |
| 107 void AppendValueAsString(std::string* out, PickleIterator* iter) const; | |
| 108 void AppendValueAsInt32(std::string* out, PickleIterator* iter) const; | |
| 109 void AppendValueAsInt64(std::string* out, PickleIterator* iter) const; | |
| 110 void AppendValueAsUInt32(std::string* out, PickleIterator* iter) const; | |
| 111 void AppendValueAsUInt64(std::string* out, PickleIterator* iter) const; | |
| 112 void AppendValueAsFloat(std::string* out, PickleIterator* iter) const; | |
| 113 void AppendValueAsBool(std::string* out, PickleIterator* iter) const; | |
| 114 | |
| 115 void CheckNotWithinDictionary(); | |
| 116 | |
| 117 Pickle pickle_; | |
| 118 enum Opcode { | |
| 119 BEGIN_DICTIONARY, | |
| 120 END_DICTIONARY, | |
| 121 BEGIN_ARRAY, | |
| 122 END_ARRAY, | |
| 123 KEY_AS_RAW_POINTER, | |
| 124 // BEGIN_DICTIONARY_ENTRY is only used to know that we are in the middle | |
| 125 // of writing a dictionary entry to the pickle, however, it is not | |
| 126 // written into the pickle, only into the stack where we keep track of | |
| 127 // the state. | |
| 128 BEGIN_DICTIONARY_ENTRY, | |
| 129 VALUE_AS_RAW_POINTER, | |
| 130 VALUE_AS_STRING, | |
| 131 INT32_VALUE, | |
| 132 INT64_VALUE, | |
| 133 UINT32_VALUE, | |
| 134 UINT64_VALUE, | |
| 135 FLOAT_VALUE, | |
| 136 BOOL_VALUE | |
| 137 }; | |
| 138 | |
| 139 // This method is in charge of preppending a comma before a value within an | |
| 140 // array, or a dictionary. If necessary, the element count will be | |
| 141 // incremented. | |
| 142 static void AppendComma(std::string* out, Opcode begin_opcode, | |
| 143 Opcode current_opcode, int* element_count); | |
| 144 | |
| 145 // This is used to keep track of the operations that need to be closed, in | |
| 146 // in order to enforce consistency in the operations. The only opcodes that | |
| 147 // need to be kept in this datastructure are BEGIN_* opcodes, such as | |
| 148 // BEGIN_ARRAY or BEGIN_DICTIONARY, using this stack, we know that we can't | |
| 149 // do an array push, when we have a dictionary, and we end a dictionary, when | |
| 150 // we were working on an array. | |
| 151 std::stack<Opcode> begin_opcodes_; | |
| 152 | |
| 153 DISALLOW_COPY_AND_ASSIGN(TracedValue); | |
| 154 }; | |
| 155 | |
| 156 } // namespace debug | |
| 157 } // namespace base | |
| 158 | |
| 159 #endif | |
| 160 | |
| OLD | NEW |