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

Side by Side Diff: base/trace_event.cc

Issue 3086: * Change output of trace_event log to JSON to enable easier integration with ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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.h" 5 #include "base/trace_event.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/platform_thread.h" 9 #include "base/platform_thread.h"
10 #include "base/process_util.h" 10 #include "base/process_util.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/time.h" 12 #include "base/time.h"
13 13
14 #define USE_UNRELIABLE_NOW 14 #define USE_UNRELIABLE_NOW
15 15
16 namespace base { 16 namespace base {
17 17
18 static const char* kEventTypeNames[] = { 18 static const char* kEventTypeNames[] = {
19 "BEGIN", 19 "BEGIN",
20 "END", 20 "END",
21 "INSTANT" 21 "INSTANT"
22 }; 22 };
23 23
24 static const wchar_t* kLogFileName = L"trace_%d.log"; 24 static const wchar_t* kLogFileName = L"trace_%d.log";
25 25
26 TraceLog::TraceLog() : enabled_(false), log_file_(NULL) { 26 TraceLog::TraceLog() : enabled_(false), log_file_(NULL) {
27 ProcessHandle proc = process_util::GetCurrentProcessHandle();
28 process_metrics_.reset(process_util::ProcessMetrics::CreateProcessMetrics(proc ));
27 } 29 }
28 30
29 TraceLog::~TraceLog() { 31 TraceLog::~TraceLog() {
30 Stop(); 32 Stop();
31 } 33 }
32 34
33 // static 35 // static
34 bool TraceLog::IsTracing() { 36 bool TraceLog::IsTracing() {
35 TraceLog* trace = Singleton<TraceLog>::get(); 37 TraceLog* trace = Singleton<TraceLog>::get();
36 return trace->enabled_; 38 return trace->enabled_;
37 } 39 }
38 40
39 // static 41 // static
40 bool TraceLog::StartTracing() { 42 bool TraceLog::StartTracing() {
41 TraceLog* trace = Singleton<TraceLog>::get(); 43 TraceLog* trace = Singleton<TraceLog>::get();
42 return trace->Start(); 44 return trace->Start();
43 } 45 }
44 46
45 bool TraceLog::Start() { 47 bool TraceLog::Start() {
46 if (enabled_) 48 if (enabled_)
47 return true; 49 return true;
48 enabled_ = OpenLogFile(); 50 enabled_ = OpenLogFile();
49 if (enabled_) 51 if (enabled_) {
52 Log("var raw_trace_events = [\r\n");
50 trace_start_time_ = TimeTicks::Now(); 53 trace_start_time_ = TimeTicks::Now();
54 timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat);
55 }
51 return enabled_; 56 return enabled_;
52 } 57 }
53 58
54 // static 59 // static
55 void TraceLog::StopTracing() { 60 void TraceLog::StopTracing() {
56 TraceLog* trace = Singleton<TraceLog>::get(); 61 TraceLog* trace = Singleton<TraceLog>::get();
57 return trace->Stop(); 62 return trace->Stop();
58 } 63 }
59 64
60 void TraceLog::Stop() { 65 void TraceLog::Stop() {
61 if (enabled_) { 66 if (enabled_) {
62 enabled_ = false; 67 enabled_ = false;
68 Log("];\r\n");
63 CloseLogFile(); 69 CloseLogFile();
70 timer_.Stop();
64 } 71 }
65 } 72 }
66 73
74 void TraceLog::Heartbeat() {
75 std::string cpu = StringPrintf("%d", process_metrics_->GetCPUUsage());
76 TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu);
77 }
78
67 void TraceLog::CloseLogFile() { 79 void TraceLog::CloseLogFile() {
68 if (log_file_) { 80 if (log_file_) {
69 #if defined(OS_WIN) 81 #if defined(OS_WIN)
70 ::CloseHandle(log_file_); 82 ::CloseHandle(log_file_);
71 #elif defined(OS_POSIX) 83 #elif defined(OS_POSIX)
72 fclose(log_file_); 84 fclose(log_file_);
73 #endif 85 #endif
74 } 86 }
75 } 87 }
76 88
(...skipping 21 matching lines...) Expand all
98 #elif defined(OS_POSIX) 110 #elif defined(OS_POSIX)
99 log_file_ = fopen(WideToUTF8(log_file_name).c_str(), "a"); 111 log_file_ = fopen(WideToUTF8(log_file_name).c_str(), "a");
100 if (log_file_ == NULL) 112 if (log_file_ == NULL)
101 return false; 113 return false;
102 #endif 114 #endif
103 return true; 115 return true;
104 } 116 }
105 117
106 void TraceLog::Trace(const std::string& name, 118 void TraceLog::Trace(const std::string& name,
107 EventType type, 119 EventType type,
108 void* id, 120 const void* id,
109 const std::wstring& extra, 121 const std::wstring& extra,
110 const char* file, 122 const char* file,
111 int line) { 123 int line) {
112 if (!enabled_) 124 if (!enabled_)
113 return; 125 return;
114 Trace(name, type, id, WideToUTF8(extra), file, line); 126 Trace(name, type, id, WideToUTF8(extra), file, line);
115 } 127 }
116 128
117 void TraceLog::Trace(const std::string& name, 129 void TraceLog::Trace(const std::string& name,
118 EventType type, 130 EventType type,
119 void* id, 131 const void* id,
120 const std::string& extra, 132 const std::string& extra,
121 const char* file, 133 const char* file,
122 int line) { 134 int line) {
123 if (!enabled_) 135 if (!enabled_)
124 return; 136 return;
125 137
126 #ifdef USE_UNRELIABLE_NOW 138 #ifdef USE_UNRELIABLE_NOW
127 TimeTicks tick = TimeTicks::UnreliableHighResNow(); 139 TimeTicks tick = TimeTicks::UnreliableHighResNow();
128 #else 140 #else
129 TimeTicks tick = TimeTicks::Now(); 141 TimeTicks tick = TimeTicks::Now();
130 #endif 142 #endif
131 TimeDelta delta = tick - trace_start_time_; 143 TimeDelta delta = tick - trace_start_time_;
132 int64 usec = delta.InMicroseconds(); 144 int64 usec = delta.InMicroseconds();
133 std::string msg = 145 std::string msg =
134 StringPrintf("%I64d 0x%lx:0x%lx %s %s [0x%lx %s] <%s:%d>\r\n", 146 StringPrintf("{'pid':'0x%lx', 'tid':'0x%lx', 'type':'%s', "
135 usec, 147 "'name':'%s', 'id':'0x%lx', 'extra':'%s', 'file':'%s', "
148 "'line_number':'%d', 'usec_begin': %I64d},\r\n",
136 process_util::GetCurrentProcId(), 149 process_util::GetCurrentProcId(),
137 PlatformThread::CurrentId(), 150 PlatformThread::CurrentId(),
138 kEventTypeNames[type], 151 kEventTypeNames[type],
139 name.c_str(), 152 name.c_str(),
140 id, 153 id,
141 extra.c_str(), 154 extra.c_str(),
142 file, 155 file,
143 line); 156 line,
157 usec);
144 158
145 Log(msg); 159 Log(msg);
146 } 160 }
147 161
148 void TraceLog::Log(const std::string& msg) { 162 void TraceLog::Log(const std::string& msg) {
149 AutoLock lock(file_lock_); 163 AutoLock lock(file_lock_);
150 164
151 #if defined (OS_WIN) 165 #if defined (OS_WIN)
152 SetFilePointer(log_file_, 0, 0, SEEK_END); 166 SetFilePointer(log_file_, 0, 0, SEEK_END);
153 DWORD num; 167 DWORD num;
154 WriteFile(log_file_, (void*)msg.c_str(), (DWORD)msg.length(), &num, NULL); 168 WriteFile(log_file_, (void*)msg.c_str(), (DWORD)msg.length(), &num, NULL);
155 #elif defined (OS_POSIX) 169 #elif defined (OS_POSIX)
156 fprintf(log_file_, "%s", msg.c_str()); 170 fprintf(log_file_, "%s", msg.c_str());
157 #endif 171 #endif
158 } 172 }
159 173
160 } // namespace base 174 } // namespace base
161 175
OLDNEW
« no previous file with comments | « base/trace_event.h ('k') | net/base/tcp_client_socket.cc » ('j') | tools/trace/trace_data.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698