Chromium Code Reviews| Index: base/debug/trace_event_value.h |
| diff --git a/base/debug/trace_event_value.h b/base/debug/trace_event_value.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b1881366c18bf19913bc911dc60ef028aaf69fc4 |
| --- /dev/null |
| +++ b/base/debug/trace_event_value.h |
| @@ -0,0 +1,146 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef BASE_TRACE_EVENT_VALUE_H_ |
|
dsinclair
2013/08/07 15:40:20
This should be BASE_DEBUG_TRACE_EVENT_VALUE_H_.
rterrazas
2013/08/08 06:21:30
Done.
|
| +#define BASE_TRACE_EVENT_VALUE_H_ |
| + |
| +#include <stack> |
| +#include <string> |
| + |
| +#include "base/debug/trace_event.h" |
| +#include "base/debug/traced_object.h" |
| +#include "base/format_macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/pickle.h" |
| +#include "base/threading/non_thread_safe.h" |
| + |
| +namespace base { |
| +namespace debug { |
| + |
| +// TracedValue has several functions to push data that will be converted to |
| +// trace event format without depending on base::Value objects. |
| +class BASE_EXPORT TracedValue : public base::debug::ConvertableToTraceFormat, |
| + public base::NonThreadSafe { |
| + public: |
| + TracedValue(); |
| + virtual ~TracedValue(); |
| + |
| + virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE; |
| + |
| + // Begin a dictionary, that is a value within another dictionary. |
| + void BeginDictionary(const char* key_in_parent_dict); |
|
dsinclair
2013/08/07 15:40:20
What about calling the two begins that take a key:
rterrazas
2013/08/08 06:21:30
Done.
|
| + // Begin a dictionary, it can either be a top-level structure or a value in |
| + // an array. |
| + void BeginDictionary(); |
| + // Used for both kinds of BeginDictionary functions. |
| + void EndDictionary(); |
| + // Begin an array, that is a value within a dictonary. |
| + void BeginArray(const char* key_in_parent_dict); |
| + // Begin an array, that is either a top-level structure or a value in an |
| + // array. |
| + void BeginArray(); |
| + // Used for both kinds of BeginArray functions. |
| + void EndArray(); |
| + |
| +// Put stuff in dictionary using statically allocated string keys. |
| +#define TRACE_PUSH(type) \ |
|
rterrazas
2013/08/05 02:58:29
Linter think's we're using a C-style cast here, ca
|
| +void Push(const char* key, type value) { \ |
|
dsinclair
2013/08/07 15:40:20
nit: indent 2
rterrazas
2013/08/08 06:21:30
Done.
|
| + PushKeyToPickle(key); \ |
|
dsinclair
2013/08/07 15:40:20
Should this have a: DCHECK_EQ(BEGIN_DICTIONARY, be
rterrazas
2013/08/08 06:21:30
Yes, I had it in PushKeyToPickle() but I agree, ma
|
| + PushValueToPickle(value); \ |
| +} |
| +TRACE_PUSH(const char*) |
| +TRACE_PUSH(const std::string&) |
| +TRACE_PUSH(int32) |
| +TRACE_PUSH(int64) |
| +TRACE_PUSH(uint32) |
| +TRACE_PUSH(uint64) |
| +TRACE_PUSH(float) |
| +TRACE_PUSH(bool) |
| +TRACE_PUSH(const TracedObject&) |
| +#undef TRACE_PUSH |
| + |
| +// Put stuff in an array. |
| +#define TRACE_PUSH(type) \ |
| +void Push(type value) { \ |
|
dsinclair
2013/08/07 15:40:20
nit: indent 2
rterrazas
2013/08/08 06:21:30
Done.
|
| + DCHECK_EQ(BEGIN_ARRAY, begin_opcodes_.top()); \ |
| + PushValueToPickle(value); \ |
| +} |
| +TRACE_PUSH(const char*) |
| +TRACE_PUSH(const std::string&) |
| +TRACE_PUSH(int32) |
| +TRACE_PUSH(int64) |
| +TRACE_PUSH(uint32) |
| +TRACE_PUSH(uint64) |
| +TRACE_PUSH(float) |
| +TRACE_PUSH(bool) |
| +TRACE_PUSH(const TracedObject&) |
| +#undef TRACE_PUSH |
| + |
| + private: |
| + // Push key/value should not be exposed, instead, the Push methods are |
| + // exposed, to have them assert that we are in the right state before writing |
| + // to the pickle. |
| + void PushKeyToPickle(const char* key); |
| + void PushValueToPickle(const char* value); |
| + void PushValueToPickle(const std::string& value); |
| + void PushValueToPickle(int value); |
| + void PushValueToPickle(int64 value); |
| + void PushValueToPickle(uint32 value); |
| + void PushValueToPickle(uint64 value); |
| + void PushValueToPickle(float value); |
| + void PushValueToPickle(bool value); |
| + void PushValueToPickle(const TracedObject& value); |
| + |
| + // Helper methods used in AppendAsTraceFormat(). |
| + void AppendKeyAsRawPointer(std::string* out, PickleIterator* iter) const; |
| + void AppendKeyAsString(std::string* out, |
| + PickleIterator* iter) const; |
| + void AppendValueAsRawPointer(std::string* out, PickleIterator* iter) const; |
| + void AppendValueAsString(std::string* out, PickleIterator* iter) const; |
| + void AppendValueAsInt32(std::string* out, PickleIterator* iter) const; |
| + void AppendValueAsInt64(std::string* out, PickleIterator* iter) const; |
| + void AppendValueAsUInt32(std::string* out, PickleIterator* iter) const; |
| + void AppendValueAsUInt64(std::string* out, PickleIterator* iter) const; |
| + void AppendValueAsFloat(std::string* out, PickleIterator* iter) const; |
| + void AppendValueAsBool(std::string* out, PickleIterator* iter) const; |
| + |
| + Pickle pickle_; |
| + enum Opcode { |
| + BEGIN_DICTIONARY, |
| + END_DICTIONARY, |
| + BEGIN_ARRAY, |
| + END_ARRAY, |
| + KEY_AS_RAW_POINTER, |
| + VALUE_AS_RAW_POINTER, |
| + VALUE_AS_STRING, |
| + INT32_VALUE, |
| + INT64_VALUE, |
| + UINT32_VALUE, |
| + UINT64_VALUE, |
| + FLOAT_VALUE, |
| + BOOL_VALUE |
| + }; |
| + |
| + // This method is in charge of preppending a comma before a value within an |
| + // array, or a dictionary. If necessary, the element count will be |
| + // incremented. |
| + static void AppendComma(std::string* out, Opcode begin_opcode, |
| + Opcode current_opcode, int* element_count); |
| + |
| + // This is used to keep track of the operations that need to be closed, in |
| + // in order to enforce consistency in the operations. The only opcodes that |
| + // need to be kept in this datastructure are BEGIN_* opcodes, such as |
| + // BEGIN_ARRAY or BEGIN_DICTIONARY, using this stack, we know that we can't |
| + // do an array push, when we have a dictionary, and we end a dictionary, when |
| + // we were working on an array. |
| + std::stack<Opcode> begin_opcodes_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TracedValue); |
| +}; |
| + |
| +} // namespace debug |
| +} // namespace base |
| + |
| +#endif |
| + |