blob: b47a4e4753e6feff099ee6487b6b72693454d13f [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.commit3f4a7322008-07-27 06:49:38 +09004
5#include "base/tracked.h"
6
7#include "base/string_util.h"
8#include "base/tracked_objects.h"
9
10namespace tracked_objects {
11
12//------------------------------------------------------------------------------
13void 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
25void Location::WriteFunctionName(std::string* output) const {
26 // Translate "<" to "&lt;" 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("&lt;");
32 break;
33
34 case '>':
35 output->append("&gt;");
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
49Tracked::Tracked() {}
50Tracked::~Tracked() {}
51void Tracked::SetBirthPlace(const Location& from_here) {}
52bool Tracked::MissingBirthplace() const { return false; }
jar@google.com374ac1f2008-08-02 07:14:06 +090053void Tracked::ResetBirthTime() {}
initial.commit3f4a7322008-07-27 06:49:38 +090054
55#else
56
57Tracked::Tracked() : tracked_births_(NULL), tracked_birth_time_(Time::Now()) {
58 if (!ThreadData::IsActive())
59 return;
60 SetBirthPlace(Location("NoFunctionName", "NeedToSetBirthPlace", -1));
61}
62
63Tracked::~Tracked() {
64 if (!ThreadData::IsActive() || !tracked_births_)
65 return;
66 ThreadData::current()->TallyADeath(*tracked_births_,
67 Time::Now() - tracked_birth_time_);
68}
69
70void 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.com09856292008-08-02 06:55:17 +090082void Tracked::ResetBirthTime() {
83 tracked_birth_time_ = Time::Now();
84}
85
initial.commit3f4a7322008-07-27 06:49:38 +090086bool Tracked::MissingBirthplace() const {
87 return -1 == tracked_births_->location().line_number();
88}
89
90#endif // NDEBUG
91
92} // namespace tracked_objects
93
license.botf003cfe2008-08-24 09:55:55 +090094