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

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: address comments Created 4 years, 1 month 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 2802 matching lines...) Expand 10 before | Expand all | Expand 10 after
2813 BeginTrace(); 2813 BeginTrace();
2814 thread.task_runner()->PostTask( 2814 thread.task_runner()->PostTask(
2815 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event)); 2815 FROM_HERE, Bind(&TraceWithAllMacroVariants, &task_complete_event));
2816 task_complete_event.Wait(); 2816 task_complete_event.Wait();
2817 task_complete_event.Reset(); 2817 task_complete_event.Reset();
2818 EndTraceAndFlushInThreadWithMessageLoop(); 2818 EndTraceAndFlushInThreadWithMessageLoop();
2819 ValidateAllTraceMacrosCreatedData(trace_parsed_); 2819 ValidateAllTraceMacrosCreatedData(trace_parsed_);
2820 } 2820 }
2821 2821
2822 std::string* g_log_buffer = NULL; 2822 std::string* g_log_buffer = NULL;
2823 bool MockLogMessageHandler(int, const char*, int, size_t, 2823 class MockLogMessageListener : logging::LogMessageListener {
2824 const std::string& str) { 2824 public:
2825 if (!g_log_buffer) 2825 void OnMessage(int severity,
2826 g_log_buffer = new std::string(); 2826 const char* file,
2827 g_log_buffer->append(str); 2827 int line,
2828 return false; 2828 size_t message_start,
2829 } 2829 const std::string& str) override {
2830 if (!g_log_buffer)
2831 g_log_buffer = new std::string();
2832 g_log_buffer->append(str);
2833 }
2834 };
2830 2835
2831 TEST_F(TraceEventTestFixture, EchoToConsole) { 2836 TEST_F(TraceEventTestFixture, EchoToConsole) {
2832 logging::LogMessageHandlerFunction old_log_message_handler = 2837 MockLogMessageListener l;
2833 logging::GetLogMessageHandler();
2834 logging::SetLogMessageHandler(MockLogMessageHandler);
2835 2838
2836 TraceLog::GetInstance()->SetEnabled( 2839 TraceLog::GetInstance()->SetEnabled(
2837 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE), 2840 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE),
2838 TraceLog::RECORDING_MODE); 2841 TraceLog::RECORDING_MODE);
2839 TRACE_EVENT_BEGIN0("a", "begin_end"); 2842 TRACE_EVENT_BEGIN0("a", "begin_end");
2840 { 2843 {
2841 TRACE_EVENT0("b", "duration"); 2844 TRACE_EVENT0("b", "duration");
2842 TRACE_EVENT0("b1", "duration1"); 2845 TRACE_EVENT0("b1", "duration1");
2843 } 2846 }
2844 TRACE_EVENT_INSTANT0("c", "instant", TRACE_EVENT_SCOPE_GLOBAL); 2847 TRACE_EVENT_INSTANT0("c", "instant", TRACE_EVENT_SCOPE_GLOBAL);
2845 TRACE_EVENT_END0("a", "begin_end"); 2848 TRACE_EVENT_END0("a", "begin_end");
2846 2849
2847 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a]\x1b")); 2850 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a]\x1b"));
2848 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b]\x1b")); 2851 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b]\x1b"));
2849 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1]\x1b")); 2852 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1]\x1b"));
2850 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1] (")); 2853 EXPECT_NE(std::string::npos, g_log_buffer->find("| | duration1[b1] ("));
2851 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b] (")); 2854 EXPECT_NE(std::string::npos, g_log_buffer->find("| duration[b] ("));
2852 EXPECT_NE(std::string::npos, g_log_buffer->find("| instant[c]\x1b")); 2855 EXPECT_NE(std::string::npos, g_log_buffer->find("| instant[c]\x1b"));
2853 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a] (")); 2856 EXPECT_NE(std::string::npos, g_log_buffer->find("begin_end[a] ("));
2854 2857
2855 EndTraceAndFlush(); 2858 EndTraceAndFlush();
2856 delete g_log_buffer; 2859 delete g_log_buffer;
2857 logging::SetLogMessageHandler(old_log_message_handler);
2858 g_log_buffer = NULL; 2860 g_log_buffer = NULL;
2859 } 2861 }
2860 2862
2861 bool LogMessageHandlerWithTraceEvent(int, const char*, int, size_t, 2863 class LogMessageListenerWithTraceEvent : logging::LogMessageListener {
2862 const std::string&) { 2864 public:
2863 TRACE_EVENT0("log", "trace_event"); 2865 void OnMessage(int severity,
2864 return false; 2866 const char* file,
2865 } 2867 int line,
2868 size_t message_start,
2869 const std::string& str) override {
2870 TRACE_EVENT0("log", "trace_event");
2871 }
2872 };
2866 2873
2867 TEST_F(TraceEventTestFixture, EchoToConsoleTraceEventRecursion) { 2874 TEST_F(TraceEventTestFixture, EchoToConsoleTraceEventRecursion) {
2868 logging::LogMessageHandlerFunction old_log_message_handler = 2875 LogMessageListenerWithTraceEvent l;
2869 logging::GetLogMessageHandler();
2870 logging::SetLogMessageHandler(LogMessageHandlerWithTraceEvent);
2871 2876
2872 TraceLog::GetInstance()->SetEnabled( 2877 TraceLog::GetInstance()->SetEnabled(
2873 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE), 2878 TraceConfig(kRecordAllCategoryFilter, ECHO_TO_CONSOLE),
2874 TraceLog::RECORDING_MODE); 2879 TraceLog::RECORDING_MODE);
2875 { 2880 {
2876 // This should not cause deadlock or infinite recursion. 2881 // This should not cause deadlock or infinite recursion.
2877 TRACE_EVENT0("b", "duration"); 2882 TRACE_EVENT0("b", "duration");
2878 } 2883 }
2879 2884
2880 EndTraceAndFlush(); 2885 EndTraceAndFlush();
2881 logging::SetLogMessageHandler(old_log_message_handler);
2882 } 2886 }
2883 2887
2884 TEST_F(TraceEventTestFixture, TimeOffset) { 2888 TEST_F(TraceEventTestFixture, TimeOffset) {
2885 BeginTrace(); 2889 BeginTrace();
2886 // Let TraceLog timer start from 0. 2890 // Let TraceLog timer start from 0.
2887 TimeDelta time_offset = TimeTicks::Now() - TimeTicks(); 2891 TimeDelta time_offset = TimeTicks::Now() - TimeTicks();
2888 TraceLog::GetInstance()->SetTimeOffset(time_offset); 2892 TraceLog::GetInstance()->SetTimeOffset(time_offset);
2889 2893
2890 { 2894 {
2891 TRACE_EVENT0("all", "duration1"); 2895 TRACE_EVENT0("all", "duration1");
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
3203 3207
3204 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) { 3208 TEST_F(TraceEventTestFixture, ClockSyncEventsAreAlwaysAddedToTrace) {
3205 BeginSpecificTrace("-*"); 3209 BeginSpecificTrace("-*");
3206 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1); 3210 TRACE_EVENT_CLOCK_SYNC_RECEIVER(1);
3207 EndTraceAndFlush(); 3211 EndTraceAndFlush();
3208 EXPECT_TRUE(FindNamePhase("clock_sync", "c")); 3212 EXPECT_TRUE(FindNamePhase("clock_sync", "c"));
3209 } 3213 }
3210 3214
3211 } // namespace trace_event 3215 } // namespace trace_event
3212 } // namespace base 3216 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698