OLD | NEW |
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_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/platform_thread.h" | 10 #include "base/platform_thread.h" |
11 #include "base/process_util.h" | 11 #include "base/process_util.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 | 14 |
15 #define USE_UNRELIABLE_NOW | 15 #define USE_UNRELIABLE_NOW |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 static const char* kEventTypeNames[] = { | 19 static const char* kEventTypeNames[] = { |
20 "BEGIN", | 20 "BEGIN", |
21 "END", | 21 "END", |
22 "INSTANT" | 22 "INSTANT" |
23 }; | 23 }; |
24 | 24 |
25 static const FilePath::CharType* kLogFileName = | 25 static const FilePath::CharType* kLogFileName = |
26 FILE_PATH_LITERAL("trace_%d.log"); | 26 FILE_PATH_LITERAL("trace_%d.log"); |
27 | 27 |
28 TraceLog::TraceLog() : enabled_(false), log_file_(NULL) { | 28 TraceLog::TraceLog() : enabled_(false), log_file_(NULL) { |
29 ProcessHandle proc = process_util::GetCurrentProcessHandle(); | 29 base::ProcessHandle proc = base::GetCurrentProcessHandle(); |
30 process_metrics_.reset(process_util::ProcessMetrics::CreateProcessMetrics(proc
)); | 30 process_metrics_.reset(base::ProcessMetrics::CreateProcessMetrics(proc)); |
31 } | 31 } |
32 | 32 |
33 TraceLog::~TraceLog() { | 33 TraceLog::~TraceLog() { |
34 Stop(); | 34 Stop(); |
35 } | 35 } |
36 | 36 |
37 // static | 37 // static |
38 bool TraceLog::IsTracing() { | 38 bool TraceLog::IsTracing() { |
39 TraceLog* trace = Singleton<TraceLog>::get(); | 39 TraceLog* trace = Singleton<TraceLog>::get(); |
40 return trace->enabled_; | 40 return trace->enabled_; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 79 } |
80 | 80 |
81 void TraceLog::CloseLogFile() { | 81 void TraceLog::CloseLogFile() { |
82 if (log_file_) { | 82 if (log_file_) { |
83 file_util::CloseFile(log_file_); | 83 file_util::CloseFile(log_file_); |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 bool TraceLog::OpenLogFile() { | 87 bool TraceLog::OpenLogFile() { |
88 FilePath::StringType pid_filename = | 88 FilePath::StringType pid_filename = |
89 StringPrintf(kLogFileName, process_util::GetCurrentProcId()); | 89 StringPrintf(kLogFileName, base::GetCurrentProcId()); |
90 FilePath log_file_path; | 90 FilePath log_file_path; |
91 if (!PathService::Get(base::DIR_EXE, &log_file_path)) | 91 if (!PathService::Get(base::DIR_EXE, &log_file_path)) |
92 return false; | 92 return false; |
93 log_file_path = log_file_path.Append(pid_filename); | 93 log_file_path = log_file_path.Append(pid_filename); |
94 log_file_ = file_util::OpenFile(log_file_path, "a"); | 94 log_file_ = file_util::OpenFile(log_file_path, "a"); |
95 if (!log_file_) { | 95 if (!log_file_) { |
96 // try the current directory | 96 // try the current directory |
97 log_file_ = file_util::OpenFile(FilePath(pid_filename), "a"); | 97 log_file_ = file_util::OpenFile(FilePath(pid_filename), "a"); |
98 if (!log_file_) { | 98 if (!log_file_) { |
99 return false; | 99 return false; |
(...skipping 26 matching lines...) Expand all Loading... |
126 TimeTicks tick = TimeTicks::HighResNow(); | 126 TimeTicks tick = TimeTicks::HighResNow(); |
127 #else | 127 #else |
128 TimeTicks tick = TimeTicks::Now(); | 128 TimeTicks tick = TimeTicks::Now(); |
129 #endif | 129 #endif |
130 TimeDelta delta = tick - trace_start_time_; | 130 TimeDelta delta = tick - trace_start_time_; |
131 int64 usec = delta.InMicroseconds(); | 131 int64 usec = delta.InMicroseconds(); |
132 std::string msg = | 132 std::string msg = |
133 StringPrintf("{'pid':'0x%lx', 'tid':'0x%lx', 'type':'%s', " | 133 StringPrintf("{'pid':'0x%lx', 'tid':'0x%lx', 'type':'%s', " |
134 "'name':'%s', 'id':'0x%lx', 'extra':'%s', 'file':'%s', " | 134 "'name':'%s', 'id':'0x%lx', 'extra':'%s', 'file':'%s', " |
135 "'line_number':'%d', 'usec_begin': %I64d},\r\n", | 135 "'line_number':'%d', 'usec_begin': %I64d},\r\n", |
136 process_util::GetCurrentProcId(), | 136 base::GetCurrentProcId(), |
137 PlatformThread::CurrentId(), | 137 PlatformThread::CurrentId(), |
138 kEventTypeNames[type], | 138 kEventTypeNames[type], |
139 name.c_str(), | 139 name.c_str(), |
140 id, | 140 id, |
141 extra.c_str(), | 141 extra.c_str(), |
142 file, | 142 file, |
143 line, | 143 line, |
144 usec); | 144 usec); |
145 | 145 |
146 Log(msg); | 146 Log(msg); |
147 } | 147 } |
148 | 148 |
149 void TraceLog::Log(const std::string& msg) { | 149 void TraceLog::Log(const std::string& msg) { |
150 AutoLock lock(file_lock_); | 150 AutoLock lock(file_lock_); |
151 | 151 |
152 fprintf(log_file_, "%s", msg.c_str()); | 152 fprintf(log_file_, "%s", msg.c_str()); |
153 } | 153 } |
154 | 154 |
155 } // namespace base | 155 } // namespace base |
OLD | NEW |