From 3d16a39e9ed13754b2cf1c59f2b0329ef0104853 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 31 May 2013 13:25:38 +0000 Subject: [PATCH] Use scoped class to implement CEF_TRACE_EVENTx (issue #971). git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@1261 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- include/cef_trace_event.h | 50 +++++++++++++++++++++++------ libcef/browser/trace_impl.cc | 21 ------------ tests/unittests/tracing_unittest.cc | 12 +++---- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/include/cef_trace_event.h b/include/cef_trace_event.h index 7fee2183f..54162b236 100644 --- a/include/cef_trace_event.h +++ b/include/cef_trace_event.h @@ -157,12 +157,6 @@ extern "C" { // - |id| is used to disambiguate counters with the same name, or match async // trace events -CEF_EXPORT void cef_trace_event(const char* category, - const char* name, - const char* arg1_name, - uint64 arg1_val, - const char* arg2_name, - uint64 arg2_val); CEF_EXPORT void cef_trace_event_instant(const char* category, const char* name, const char* arg1_name, @@ -233,12 +227,31 @@ CEF_EXPORT void cef_trace_event_async_end(const char* category, // - category and name strings must have application lifetime (statics or // literals). They may not include " chars. #define CEF_TRACE_EVENT0(category, name) \ - cef_trace_event(category, name, NULL, 0, NULL, 0) + cef_trace_event_begin(category, name, NULL, 0, NULL, 0, false); \ + CEF_INTERNAL_TRACE_END_ON_SCOPE_CLOSE(category, name) #define CEF_TRACE_EVENT1(category, name, arg1_name, arg1_val) \ - cef_trace_event(category, name, arg1_name, arg1_val, NULL, 0) + cef_trace_event_begin(category, name, arg1_name, arg1_val, NULL, 0, false); \ + CEF_INTERNAL_TRACE_END_ON_SCOPE_CLOSE(category, name) #define CEF_TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, \ arg2_val) \ - cef_trace_event(category, name, arg1_name, arg1_val, arg2_name, arg2_val) + cef_trace_event_begin(category, name, arg1_name, arg1_val, \ + arg2_name, arg2_val, false); \ + CEF_INTERNAL_TRACE_END_ON_SCOPE_CLOSE(category, name) + +// Implementation detail: trace event macros create temporary variable names. +// These macros give each temporary variable a unique name based on the line +// number to prevent name collisions. +#define CEF_INTERNAL_TRACE_EVENT_UID3(a,b) \ + cef_trace_event_unique_##a##b +#define CEF_INTERNAL_TRACE_EVENT_UID2(a,b) \ + CEF_INTERNAL_TRACE_EVENT_UID3(a,b) +#define CEF_INTERNAL_TRACE_EVENT_UID(name_prefix) \ + CEF_INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) + +// Implementation detail: internal macro to end end event when the scope ends. +#define CEF_INTERNAL_TRACE_END_ON_SCOPE_CLOSE(category, name) \ + cef_trace_event_internal::CefTraceEndOnScopeClose \ + CEF_INTERNAL_TRACE_EVENT_UID(profileScope)(category, name) // Records a single event called "name" immediately, with 0, 1 or 2 // associated arguments. If the category is not enabled, then this @@ -436,4 +449,23 @@ CEF_EXPORT void cef_trace_event_async_end(const char* category, cef_trace_event_async_end(category, name, id, arg1_name, arg1_val, \ arg2_name, arg2_val, true) +namespace cef_trace_event_internal { + +// Used by CEF_TRACE_EVENTx macro. Do not use directly. +class CefTraceEndOnScopeClose { + public: + CefTraceEndOnScopeClose(const char* category, const char* name) + : category_(category), name_(name) { + } + ~CefTraceEndOnScopeClose() { + cef_trace_event_end(category_, name_, NULL, 0, NULL, 0, false); + } + + private: + const char* category_; + const char* name_; +}; + +} // cef_trace_event_internal + #endif // CEF_INCLUDE_CEF_TRACE_EVENT_H_ diff --git a/libcef/browser/trace_impl.cc b/libcef/browser/trace_impl.cc index 14aa66669..0c15c9460 100644 --- a/libcef/browser/trace_impl.cc +++ b/libcef/browser/trace_impl.cc @@ -72,27 +72,6 @@ int64 CefNowFromSystemTraceTime() { // The below functions can be called from any process. -CEF_EXPORT void cef_trace_event(const char* category, - const char* name, - const char* arg1_name, - uint64 arg1_val, - const char* arg2_name, - uint64 arg2_val) { - DCHECK(category); - DCHECK(name); - if (!category || !name) - return; - - if (arg1_name == NULL && arg2_name == NULL) { - TRACE_EVENT0(category, name); - } else if (arg2_name == NULL) { - TRACE_EVENT1(category, name, arg1_name, arg1_val); - } else { - TRACE_EVENT2(category, name, arg1_name, arg1_val, - arg2_name, arg2_val); - } -} - CEF_EXPORT void cef_trace_event_instant(const char* category, const char* name, const char* arg1_name, diff --git a/tests/unittests/tracing_unittest.cc b/tests/unittests/tracing_unittest.cc index fbeeaa01b..a9dba2fe1 100644 --- a/tests/unittests/tracing_unittest.cc +++ b/tests/unittests/tracing_unittest.cc @@ -121,16 +121,16 @@ class TracingTestHandler : public CefTraceClient { WaitForUIThread(); switch (type_) { - case CEF_TRACE_EVENT0: + case CEF_TRACE_EVENT0: { CEF_TRACE_EVENT0(kTraceTestCategory, "CEF_TRACE_EVENT0"); - break; - case CEF_TRACE_EVENT1: + } break; + case CEF_TRACE_EVENT1: { CEF_TRACE_EVENT1(kTraceTestCategory, "CEF_TRACE_EVENT1", "arg1", 1); - break; - case CEF_TRACE_EVENT2: + } break; + case CEF_TRACE_EVENT2: { CEF_TRACE_EVENT2(kTraceTestCategory, "CEF_TRACE_EVENT2", "arg1", 1, "arg2", 2); - break; + } break; case CEF_TRACE_EVENT_INSTANT0: CEF_TRACE_EVENT_INSTANT0(kTraceTestCategory, "CEF_TRACE_EVENT_INSTANT0");