OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/debug/trace_event.h" | 5 #include "base/debug/trace_event.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 static const char* kEventTypeNames[] = { | 22 static const char* kEventTypeNames[] = { |
23 "BEGIN", | 23 "BEGIN", |
24 "END", | 24 "END", |
25 "INSTANT" | 25 "INSTANT" |
26 }; | 26 }; |
27 | 27 |
28 static const FilePath::CharType* kLogFileName = | 28 static const FilePath::CharType* kLogFileName = |
29 FILE_PATH_LITERAL("trace_%d.log"); | 29 FILE_PATH_LITERAL("trace_%d.log"); |
30 | 30 |
31 TraceLog::TraceLog() : enabled_(false), log_file_(NULL) { | |
32 base::ProcessHandle proc = base::GetCurrentProcessHandle(); | |
33 #if !defined(OS_MACOSX) | |
34 process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc)); | |
35 #else | |
36 // The default port provider is sufficient to get data for the current | |
37 // process. | |
38 process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc, | |
39 NULL)); | |
40 #endif | |
41 } | |
42 | |
43 TraceLog::~TraceLog() { | |
44 Stop(); | |
45 } | |
46 | |
47 // static | 31 // static |
48 TraceLog* TraceLog::GetInstance() { | 32 TraceLog* TraceLog::GetInstance() { |
49 return Singleton<TraceLog, DefaultSingletonTraits<TraceLog> >::get(); | 33 return Singleton<TraceLog, DefaultSingletonTraits<TraceLog> >::get(); |
50 } | 34 } |
51 | 35 |
52 // static | 36 // static |
53 bool TraceLog::IsTracing() { | 37 bool TraceLog::IsTracing() { |
54 return TraceLog::GetInstance()->enabled_; | 38 return TraceLog::GetInstance()->enabled_; |
55 } | 39 } |
56 | 40 |
57 // static | 41 // static |
58 bool TraceLog::StartTracing() { | 42 bool TraceLog::StartTracing() { |
59 return TraceLog::GetInstance()->Start(); | 43 return TraceLog::GetInstance()->Start(); |
60 } | 44 } |
61 | 45 |
62 bool TraceLog::Start() { | |
63 if (enabled_) | |
64 return true; | |
65 enabled_ = OpenLogFile(); | |
66 if (enabled_) { | |
67 Log("var raw_trace_events = [\n"); | |
68 trace_start_time_ = TimeTicks::Now(); | |
69 timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat); | |
70 } | |
71 return enabled_; | |
72 } | |
73 | |
74 // static | 46 // static |
75 void TraceLog::StopTracing() { | 47 void TraceLog::StopTracing() { |
76 return TraceLog::GetInstance()->Stop(); | 48 return TraceLog::GetInstance()->Stop(); |
77 } | 49 } |
78 | 50 |
79 void TraceLog::Stop() { | |
80 if (enabled_) { | |
81 enabled_ = false; | |
82 Log("];\n"); | |
83 CloseLogFile(); | |
84 timer_.Stop(); | |
85 } | |
86 } | |
87 | |
88 void TraceLog::Heartbeat() { | |
89 std::string cpu = StringPrintf("%.0f", process_metrics_->GetCPUUsage()); | |
90 TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu); | |
91 } | |
92 | |
93 void TraceLog::CloseLogFile() { | |
94 if (log_file_) { | |
95 file_util::CloseFile(log_file_); | |
96 } | |
97 } | |
98 | |
99 bool TraceLog::OpenLogFile() { | |
100 FilePath::StringType pid_filename = | |
101 StringPrintf(kLogFileName, base::GetCurrentProcId()); | |
102 FilePath log_file_path; | |
103 if (!PathService::Get(base::DIR_EXE, &log_file_path)) | |
104 return false; | |
105 log_file_path = log_file_path.Append(pid_filename); | |
106 log_file_ = file_util::OpenFile(log_file_path, "a"); | |
107 if (!log_file_) { | |
108 // try the current directory | |
109 log_file_ = file_util::OpenFile(FilePath(pid_filename), "a"); | |
110 if (!log_file_) { | |
111 return false; | |
112 } | |
113 } | |
114 return true; | |
115 } | |
116 | |
117 void TraceLog::Trace(const std::string& name, | 51 void TraceLog::Trace(const std::string& name, |
118 EventType type, | 52 EventType type, |
119 const void* id, | 53 const void* id, |
120 const std::wstring& extra, | 54 const std::wstring& extra, |
121 const char* file, | 55 const char* file, |
122 int line) { | 56 int line) { |
123 if (!enabled_) | 57 if (!enabled_) |
124 return; | 58 return; |
125 Trace(name, type, id, WideToUTF8(extra), file, line); | 59 Trace(name, type, id, WideToUTF8(extra), file, line); |
126 } | 60 } |
(...skipping 24 matching lines...) Expand all Loading... |
151 name.c_str(), | 85 name.c_str(), |
152 id, | 86 id, |
153 extra.c_str(), | 87 extra.c_str(), |
154 file, | 88 file, |
155 line, | 89 line, |
156 usec); | 90 usec); |
157 | 91 |
158 Log(msg); | 92 Log(msg); |
159 } | 93 } |
160 | 94 |
| 95 TraceLog::TraceLog() : enabled_(false), log_file_(NULL) { |
| 96 base::ProcessHandle proc = base::GetCurrentProcessHandle(); |
| 97 #if !defined(OS_MACOSX) |
| 98 process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc)); |
| 99 #else |
| 100 // The default port provider is sufficient to get data for the current |
| 101 // process. |
| 102 process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc, |
| 103 NULL)); |
| 104 #endif |
| 105 } |
| 106 |
| 107 TraceLog::~TraceLog() { |
| 108 Stop(); |
| 109 } |
| 110 |
| 111 bool TraceLog::OpenLogFile() { |
| 112 FilePath::StringType pid_filename = |
| 113 StringPrintf(kLogFileName, base::GetCurrentProcId()); |
| 114 FilePath log_file_path; |
| 115 if (!PathService::Get(base::DIR_EXE, &log_file_path)) |
| 116 return false; |
| 117 log_file_path = log_file_path.Append(pid_filename); |
| 118 log_file_ = file_util::OpenFile(log_file_path, "a"); |
| 119 if (!log_file_) { |
| 120 // try the current directory |
| 121 log_file_ = file_util::OpenFile(FilePath(pid_filename), "a"); |
| 122 if (!log_file_) { |
| 123 return false; |
| 124 } |
| 125 } |
| 126 return true; |
| 127 } |
| 128 |
| 129 void TraceLog::CloseLogFile() { |
| 130 if (log_file_) { |
| 131 file_util::CloseFile(log_file_); |
| 132 } |
| 133 } |
| 134 |
| 135 bool TraceLog::Start() { |
| 136 if (enabled_) |
| 137 return true; |
| 138 enabled_ = OpenLogFile(); |
| 139 if (enabled_) { |
| 140 Log("var raw_trace_events = [\n"); |
| 141 trace_start_time_ = TimeTicks::Now(); |
| 142 timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat); |
| 143 } |
| 144 return enabled_; |
| 145 } |
| 146 |
| 147 void TraceLog::Stop() { |
| 148 if (enabled_) { |
| 149 enabled_ = false; |
| 150 Log("];\n"); |
| 151 CloseLogFile(); |
| 152 timer_.Stop(); |
| 153 } |
| 154 } |
| 155 |
| 156 void TraceLog::Heartbeat() { |
| 157 std::string cpu = StringPrintf("%.0f", process_metrics_->GetCPUUsage()); |
| 158 TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu); |
| 159 } |
| 160 |
161 void TraceLog::Log(const std::string& msg) { | 161 void TraceLog::Log(const std::string& msg) { |
162 AutoLock lock(file_lock_); | 162 AutoLock lock(file_lock_); |
163 | 163 |
164 fprintf(log_file_, "%s", msg.c_str()); | 164 fprintf(log_file_, "%s", msg.c_str()); |
165 } | 165 } |
166 | 166 |
167 } // namespace debug | 167 } // namespace debug |
168 } // namespace base | 168 } // namespace base |
OLD | NEW |