Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: base/trace_event/trace_event_unittest.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use ReadWriteLock, add comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/trace_event.h" 5 #include "base/trace_event/trace_event.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
(...skipping 3063 matching lines...) Expand 10 before | Expand all | Expand 10 after
3074 BeginTrace(); 3074 BeginTrace();
3075 thread.task_runner()->PostTask( 3075 thread.task_runner()->PostTask(
3076 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); 3076 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event));
3077 task_complete_event.Wait(); 3077 task_complete_event.Wait();
3078 task_complete_event.Reset(); 3078 task_complete_event.Reset();
3079 EndTraceAndFlushInThreadWithMessageLoop(); 3079 EndTraceAndFlushInThreadWithMessageLoop();
3080 ValidateAllTraceMacrosCreatedData(trace_parsed_); 3080 ValidateAllTraceMacrosCreatedData(trace_parsed_);
3081 } 3081 }
3082 3082
3083 std::string* g_log_buffer = NULL; 3083 std::string* g_log_buffer = NULL;
3084 bool MockLogMessageHandler(int, const char*, int, size_t, 3084 class MockLogMessageListener : logging::LogMessageListener {
3085 const std::string& str) { 3085 public:
3086 if (!g_log_buffer) 3086 void OnMessage(int severity,
3087 g_log_buffer = new std::string(); 3087 const char* file,
3088 g_log_buffer->append(str); 3088 int line,
3089 return false; 3089 size_t message_start,
3090 } 3090 const std::string& str) override {
3091 if (!g_log_buffer)
3092 g_log_buffer = new std::string();
3093 g_log_buffer->append(str);
3094 }
3095 };
3091 3096
3092 TEST_F(TraceEventTestFixture, EchoToConsole) { 3097 TEST_F(TraceEventTestFixture, EchoToConsole) {
3093 logging::LogMessageHandlerFunction old_log_message_handler = 3098 MockLogMessageListener l;
3094 logging::GetLogMessageHandler();
3095 logging::SetLogMessageHandler(MockLogMessageHandler);
3096 3099
3097 TraceLog::GetInstance()->SetEnabled( 3100 TraceLog::GetInstance()->SetEnabled(
3098 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE), 3101 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE),
3099 TraceLog::RECORDING_MODE); 3102 TraceLog::RECORDING_MODE);
3100 TRACE_EVENT_BEGIN0("a", "begin_end"); 3103 TRACE_EVENT_BEGIN0("a", "begin_end");
3101 { 3104 {
3102 TRACE_EVENT0("b", "duration"); 3105 TRACE_EVENT0("b", "duration");
3103 TRACE_EVENT0("b1", "duration1"); 3106 TRACE_EVENT0("b1", "duration1");
3104 } 3107 }
3105 TRACE_EVENT_INSTANT0("c", "instant", TRACE_EVENT_SCOPE_GLOBAL); 3108 TRACE_EVENT_INSTANT0("c", "instant", TRACE_EVENT_SCOPE_GLOBAL);
3106 TRACE_EVENT_END0("a", "begin_end"); 3109 TRACE_EVENT_END0("a", "begin_end");
3107 3110
3108 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a]\x1b")); 3111 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a]\x1b"));
3109 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b]\x1b")); 3112 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b]\x1b"));
3110 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1]\x1b")); 3113 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1]\x1b"));
3111 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1] (")); 3114 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1] ("));
3112 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b] (")); 3115 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b] ("));
3113 EXPECT_NE(std::string::npos, g_log_buffer->find("| instant[c]\x1b")); 3116 EXPECT_NE(std::string::npos, g_log_buffer->find("| instant[c]\x1b"));
3114 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a] (")); 3117 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a] ("));
3115 3118
3116 EndTraceAndFlush(); 3119 EndTraceAndFlush();
3117 delete g_log_buffer; 3120 delete g_log_buffer;
3118 logging::SetLogMessageHandler(old_log_message_handler);
3119 g_log_buffer = NULL; 3121 g_log_buffer = NULL;
3120 } 3122 }
3121 3123
3122 bool LogMessageHandlerWithTraceEvent(int, const char*, int, size_t, 3124 class LogMessageListenerWithTraceEvent : logging::LogMessageListener {
3123 const std::string&) { 3125 public:
3124 TRACE_EVENT0("log", "trace_event"); 3126 void OnMessage(int severity,
3125 return false; 3127 const char* file,
3126 } 3128 int line,
3129 size_t message_start,
3130 const std::string& str) override {
3131 TRACE_EVENT0("log", "trace_event");
3132 }
3133 };
3127 3134
3128 TEST_F(TraceEventTestFixture, EchoToConsoleTraceEventRecursion) { 3135 TEST_F(TraceEventTestFixture, EchoToConsoleTraceEventRecursion) {
3129 logging::LogMessageHandlerFunction old_log_message_handler = 3136 LogMessageListenerWithTraceEvent l;
3130 logging::GetLogMessageHandler();
3131 logging::SetLogMessageHandler(LogMessageHandlerWithTraceEvent);
3132 3137
3133 TraceLog::GetInstance()->SetEnabled( 3138 TraceLog::GetInstance()->SetEnabled(
3134 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE), 3139 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE),
3135 TraceLog::RECORDING_MODE); 3140 TraceLog::RECORDING_MODE);
3136 { 3141 {
3137 // This should not cause deadlock or infinite recursion. 3142 // This should not cause deadlock or infinite recursion.
3138 TRACE_EVENT0("b", "duration"); 3143 TRACE_EVENT0("b", "duration");
3139 } 3144 }
3140 3145
3141 EndTraceAndFlush(); 3146 EndTraceAndFlush();
3142 logging::SetLogMessageHandler(old_log_message_handler);
3143 } 3147 }
3144 3148
3145 TEST_F(TraceEventTestFixture, TimeOffset) { 3149 TEST_F(TraceEventTestFixture, TimeOffset) {
3146 BeginTrace(); 3150 BeginTrace();
3147 // Let TraceLog timer start from 0. 3151 // Let TraceLog timer start from 0.
3148 TimeDelta time_offset = TimeTicks::Now() - TimeTicks(); 3152 TimeDelta time_offset = TimeTicks::Now() - TimeTicks();
3149 TraceLog::GetInstance()->SetTimeOffset(time_offset); 3153 TraceLog::GetInstance()->SetTimeOffset(time_offset);
3150 3154
3151 { 3155 {
3152 TRACE_EVENT0("all", "duration1"); 3156 TRACE_EVENT0("all", "duration1");
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
3219 3223
3220 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { 3224 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) {
3221 BeginSpecificTrace("-*"); 3225 BeginSpecificTrace("-*");
3222 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); 3226 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1);
3223 EndTraceAndFlush(); 3227 EndTraceAndFlush();
3224 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); 3228 EXPECT_TRUE(FindNamePhase("clock_sync", "c"));
3225 } 3229 }
3226 3230
3227 } // namespace trace_event 3231 } // namespace trace_event
3228 } // namespace base 3232 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698