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 // Begins a dictionary, that is a value within another dictionary. | |
| 35 void PushDictionary(const char* key_in_parent_dict); | |
|
dsinclair
2013/08/08 14:07:28
nit: Move the two Push* methods down below the End
rterrazas
2013/08/11 21:05:08
Done.
| |
| 36 // Used for both kinds of BeginDictionary functions. | |
| 37 void EndDictionary(); | |
| 38 // Begin an array, that is either a top-level structure or a value in an | |
| 39 // array. | |
| 40 void BeginArray(); | |
| 41 // Begins an array, that is a value within a dictonary. | |
| 42 void PushArray(const char* key_in_parent_dict); | |
| 43 // Used for both kinds of BeginArray functions. | |
| 44 void EndArray(); | |
| 45 | |
| 46 // Put stuff in dictionary using statically allocated string keys. | |
| 47 #define TRACE_PUSH(type) \ | |
| 48 void Push(const char* key, type value) { \ | |
| 49 DCHECK_EQ(BEGIN_DICTIONARY, begin_opcodes_.top()); \ | |
| 50 PushKeyToPickle(key); \ | |
| 51 PushValueToPickle(value); \ | |
| 52 } | |
| 53 TRACE_PUSH(const char*) | |
| 54 TRACE_PUSH(const std::string&) | |
| 55 TRACE_PUSH(int32) | |
| 56 TRACE_PUSH(int64) | |
| 57 TRACE_PUSH(uint32) | |
| 58 TRACE_PUSH(uint64) | |
| 59 TRACE_PUSH(float) | |
| 60 TRACE_PUSH(bool) | |
| 61 TRACE_PUSH(const TracedObject&) | |
| 62 #undef TRACE_PUSH | |
| 63 | |
| 64 // Put stuff in an array. | |
| 65 #define TRACE_PUSH(type) \ | |
| 66 void Push(type value) { \ | |
| 67 DCHECK_EQ(BEGIN_ARRAY, begin_opcodes_.top()); \ | |
| 68 PushValueToPickle(value); \ | |
| 69 } | |
| 70 TRACE_PUSH(const char*) | |
| 71 TRACE_PUSH(const std::string&) | |
| 72 TRACE_PUSH(int32) | |
| 73 TRACE_PUSH(int64) | |
| 74 TRACE_PUSH(uint32) | |
| 75 TRACE_PUSH(uint64) | |
| 76 TRACE_PUSH(float) | |
| 77 TRACE_PUSH(bool) | |
| 78 TRACE_PUSH(const TracedObject&) | |
| 79 #undef TRACE_PUSH | |
| 80 | |
| 81 private: | |
| 82 // Push key/value should not be exposed, instead, the Push methods are | |
| 83 // exposed, to have them assert that we are in the right state before writing | |
| 84 // to the pickle. | |
| 85 void PushKeyToPickle(const char* key); | |
| 86 void PushValueToPickle(const char* value); | |
| 87 void PushValueToPickle(const std::string& value); | |
| 88 void PushValueToPickle(int value); | |
| 89 void PushValueToPickle(int64 value); | |
| 90 void PushValueToPickle(uint32 value); | |
| 91 void PushValueToPickle(uint64 value); | |
| 92 void PushValueToPickle(float value); | |
| 93 void PushValueToPickle(bool value); | |
| 94 void PushValueToPickle(const TracedObject& value); | |
| 95 | |
| 96 // Helper methods used in AppendAsTraceFormat(). | |
| 97 void AppendKeyAsRawPointer(std::string* out, PickleIterator* iter) const; | |
| 98 void AppendValueAsRawPointer(std::string* out, PickleIterator* iter) const; | |
| 99 void AppendValueAsString(std::string* out, PickleIterator* iter) const; | |
| 100 void AppendValueAsInt32(std::string* out, PickleIterator* iter) const; | |
| 101 void AppendValueAsInt64(std::string* out, PickleIterator* iter) const; | |
| 102 void AppendValueAsUInt32(std::string* out, PickleIterator* iter) const; | |
| 103 void AppendValueAsUInt64(std::string* out, PickleIterator* iter) const; | |
| 104 void AppendValueAsFloat(std::string* out, PickleIterator* iter) const; | |
| 105 void AppendValueAsBool(std::string* out, PickleIterator* iter) const; | |
| 106 | |
| 107 Pickle pickle_; | |
| 108 enum Opcode { | |
| 109 BEGIN_DICTIONARY, | |
| 110 END_DICTIONARY, | |
| 111 BEGIN_ARRAY, | |
| 112 END_ARRAY, | |
| 113 KEY_AS_RAW_POINTER, | |
| 114 VALUE_AS_RAW_POINTER, | |
| 115 VALUE_AS_STRING, | |
| 116 INT32_VALUE, | |
| 117 INT64_VALUE, | |
| 118 UINT32_VALUE, | |
| 119 UINT64_VALUE, | |
| 120 FLOAT_VALUE, | |
| 121 BOOL_VALUE | |
| 122 }; | |
| 123 | |
| 124 // This method is in charge of preppending a comma before a value within an | |
| 125 // array, or a dictionary. If necessary, the element count will be | |
| 126 // incremented. | |
| 127 static void AppendComma(std::string* out, Opcode begin_opcode, | |
| 128 Opcode current_opcode, int* element_count); | |
| 129 | |
| 130 // This is used to keep track of the operations that need to be closed, in | |
| 131 // in order to enforce consistency in the operations. The only opcodes that | |
| 132 // need to be kept in this datastructure are BEGIN_* opcodes, such as | |
| 133 // BEGIN_ARRAY or BEGIN_DICTIONARY, using this stack, we know that we can't | |
| 134 // do an array push, when we have a dictionary, and we end a dictionary, when | |
| 135 // we were working on an array. | |
| 136 std::stack<Opcode> begin_opcodes_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(TracedValue); | |
| 139 }; | |
| 140 | |
| 141 } // namespace debug | |
| 142 } // namespace base | |
| 143 | |
| 144 #endif | |
| 145 | |
| OLD | NEW |