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

Side by Side Diff: base/trace_event.cc

Issue 6005: Cross-platform equivalent of fopen, _wfopen_s etc.... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 12 years, 2 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"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 bool TraceLog::StartTracing() { 42 bool TraceLog::StartTracing() {
43 TraceLog* trace = Singleton<TraceLog>::get(); 43 TraceLog* trace = Singleton<TraceLog>::get();
44 return trace->Start(); 44 return trace->Start();
45 } 45 }
46 46
47 bool TraceLog::Start() { 47 bool TraceLog::Start() {
48 if (enabled_) 48 if (enabled_)
49 return true; 49 return true;
50 enabled_ = OpenLogFile(); 50 enabled_ = OpenLogFile();
51 if (enabled_) { 51 if (enabled_) {
52 Log("var raw_trace_events = [\r\n"); 52 Log("var raw_trace_events = [\r\n");
Mark Mentovai 2008/10/01 17:40:55 Followup patch to get rid of the \r or only emit i
53 trace_start_time_ = TimeTicks::Now(); 53 trace_start_time_ = TimeTicks::Now();
54 timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat); 54 timer_.Start(TimeDelta::FromMilliseconds(250), this, &TraceLog::Heartbeat);
55 } 55 }
56 return enabled_; 56 return enabled_;
57 } 57 }
58 58
59 // static 59 // static
60 void TraceLog::StopTracing() { 60 void TraceLog::StopTracing() {
61 TraceLog* trace = Singleton<TraceLog>::get(); 61 TraceLog* trace = Singleton<TraceLog>::get();
62 return trace->Stop(); 62 return trace->Stop();
63 } 63 }
64 64
65 void TraceLog::Stop() { 65 void TraceLog::Stop() {
66 if (enabled_) { 66 if (enabled_) {
67 enabled_ = false; 67 enabled_ = false;
68 Log("];\r\n"); 68 Log("];\r\n");
69 CloseLogFile(); 69 CloseLogFile();
70 timer_.Stop(); 70 timer_.Stop();
71 } 71 }
72 } 72 }
73 73
74 void TraceLog::Heartbeat() { 74 void TraceLog::Heartbeat() {
75 std::string cpu = StringPrintf("%d", process_metrics_->GetCPUUsage()); 75 std::string cpu = StringPrintf("%d", process_metrics_->GetCPUUsage());
76 TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu); 76 TRACE_EVENT_INSTANT("heartbeat.cpu", 0, cpu);
77 } 77 }
78 78
79 void TraceLog::CloseLogFile() { 79 void TraceLog::CloseLogFile() {
80 if (log_file_) { 80 if (log_file_) {
81 #if defined(OS_WIN) 81 file_util::CloseFile(log_file_);
Mark Mentovai 2008/10/01 17:40:55 log_file_ is declared as a FileHandle, which is no
82 ::CloseHandle(log_file_);
83 #elif defined(OS_POSIX)
84 fclose(log_file_);
85 #endif
86 } 82 }
87 } 83 }
88 84
89 bool TraceLog::OpenLogFile() { 85 bool TraceLog::OpenLogFile() {
90 std::wstring pid_filename = 86 std::wstring pid_filename =
91 StringPrintf(kLogFileName, process_util::GetCurrentProcId()); 87 StringPrintf(kLogFileName, process_util::GetCurrentProcId());
92 std::wstring log_file_name; 88 std::wstring log_file_name;
93 PathService::Get(base::DIR_EXE, &log_file_name); 89 PathService::Get(base::DIR_EXE, &log_file_name);
94 file_util::AppendToPath(&log_file_name, pid_filename); 90 file_util::AppendToPath(&log_file_name, pid_filename);
95 #if defined(OS_WIN) 91 log_file_ = file_util::OpenFile(log_file_name, "a");
96 log_file_ = ::CreateFile(log_file_name.c_str(), GENERIC_WRITE, 92 if (!log_file_) {
97 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
98 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
99 if (log_file_ == INVALID_HANDLE_VALUE || log_file_ == NULL) {
100 // try the current directory 93 // try the current directory
101 log_file_ = ::CreateFile(pid_filename.c_str(), GENERIC_WRITE, 94 log_file_ = file_util::OpenFile(pid_filename, "a");
102 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, 95 if (!log_file_) {
103 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
104 if (log_file_ == INVALID_HANDLE_VALUE || log_file_ == NULL) {
105 log_file_ = NULL;
106 return false; 96 return false;
107 } 97 }
108 } 98 }
109 ::SetFilePointer(log_file_, 0, 0, FILE_END);
110 #elif defined(OS_POSIX)
111 log_file_ = fopen(WideToUTF8(log_file_name).c_str(), "a");
112 if (log_file_ == NULL)
113 return false;
114 #endif
115 return true; 99 return true;
116 } 100 }
117 101
118 void TraceLog::Trace(const std::string& name, 102 void TraceLog::Trace(const std::string& name,
119 EventType type, 103 EventType type,
120 const void* id, 104 const void* id,
121 const std::wstring& extra, 105 const std::wstring& extra,
122 const char* file, 106 const char* file,
123 int line) { 107 int line) {
124 if (!enabled_) 108 if (!enabled_)
(...skipping 30 matching lines...) Expand all
155 file, 139 file,
156 line, 140 line,
157 usec); 141 usec);
158 142
159 Log(msg); 143 Log(msg);
160 } 144 }
161 145
162 void TraceLog::Log(const std::string& msg) { 146 void TraceLog::Log(const std::string& msg) {
163 AutoLock lock(file_lock_); 147 AutoLock lock(file_lock_);
164 148
165 #if defined (OS_WIN)
166 SetFilePointer(log_file_, 0, 0, SEEK_END);
167 DWORD num;
168 WriteFile(log_file_, (void*)msg.c_str(), (DWORD)msg.length(), &num, NULL);
169 #elif defined (OS_POSIX)
170 fprintf(log_file_, "%s", msg.c_str()); 149 fprintf(log_file_, "%s", msg.c_str());
171 #endif
172 } 150 }
173 151
174 } // namespace base 152 } // namespace base
175 153
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698