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