|
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 |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #include "base/tracked.h" |
|
6 |
|
7 #include "base/string_util.h" |
|
8 #include "base/tracked_objects.h" |
|
9 |
|
10 using base::Time; |
|
11 |
|
12 namespace tracked_objects { |
|
13 |
|
14 //------------------------------------------------------------------------------ |
|
15 void Location::Write(bool display_filename, bool display_function_name, |
|
16 std::string* output) const { |
|
17 StringAppendF(output, "%s[%d] ", |
|
18 display_filename ? file_name_ : "line", |
|
19 line_number_); |
|
20 |
|
21 if (display_function_name) { |
|
22 WriteFunctionName(output); |
|
23 output->push_back(' '); |
|
24 } |
|
25 } |
|
26 |
|
27 void Location::WriteFunctionName(std::string* output) const { |
|
28 // Translate "<" to "<" for HTML safety. |
|
29 // TODO(jar): Support ASCII or html for logging in ASCII. |
|
30 for (const char *p = function_name_; *p; p++) { |
|
31 switch (*p) { |
|
32 case '<': |
|
33 output->append("<"); |
|
34 break; |
|
35 |
|
36 case '>': |
|
37 output->append(">"); |
|
38 break; |
|
39 |
|
40 default: |
|
41 output->push_back(*p); |
|
42 break; |
|
43 } |
|
44 } |
|
45 } |
|
46 |
|
47 //------------------------------------------------------------------------------ |
|
48 |
|
49 #ifndef TRACK_ALL_TASK_OBJECTS |
|
50 |
|
51 Tracked::Tracked() {} |
|
52 Tracked::~Tracked() {} |
|
53 void Tracked::SetBirthPlace(const Location& from_here) {} |
|
54 bool Tracked::MissingBirthplace() const { return false; } |
|
55 void Tracked::ResetBirthTime() {} |
|
56 |
|
57 #else |
|
58 |
|
59 Tracked::Tracked() : tracked_births_(NULL), tracked_birth_time_(Time::Now()) { |
|
60 if (!ThreadData::IsActive()) |
|
61 return; |
|
62 SetBirthPlace(Location("NoFunctionName", "NeedToSetBirthPlace", -1)); |
|
63 } |
|
64 |
|
65 Tracked::~Tracked() { |
|
66 if (!ThreadData::IsActive() || !tracked_births_) |
|
67 return; |
|
68 ThreadData::current()->TallyADeath(*tracked_births_, |
|
69 Time::Now() - tracked_birth_time_); |
|
70 } |
|
71 |
|
72 void Tracked::SetBirthPlace(const Location& from_here) { |
|
73 if (!ThreadData::IsActive()) |
|
74 return; |
|
75 if (tracked_births_) |
|
76 tracked_births_->ForgetBirth(); |
|
77 ThreadData* current_thread_data = ThreadData::current(); |
|
78 if (!current_thread_data) |
|
79 return; // Shutdown started, and this thread wasn't registered. |
|
80 tracked_births_ = current_thread_data->FindLifetime(from_here); |
|
81 tracked_births_->RecordBirth(); |
|
82 } |
|
83 |
|
84 void Tracked::ResetBirthTime() { |
|
85 tracked_birth_time_ = Time::Now(); |
|
86 } |
|
87 |
|
88 bool Tracked::MissingBirthplace() const { |
|
89 return -1 == tracked_births_->location().line_number(); |
|
90 } |
|
91 |
|
92 #endif // NDEBUG |
|
93 |
|
94 } // namespace tracked_objects |