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 0 : 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 0 : line_number_);
20 :
21 0 : if (display_function_name) {
22 0 : WriteFunctionName(output);
23 0 : output->push_back(' ');
24 : }
25 0 : }
26 :
27 0 : 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 0 : for (const char *p = function_name_; *p; p++) {
31 0 : switch (*p) {
32 : case '<':
33 0 : output->append("<");
34 0 : break;
35 :
36 : case '>':
37 0 : output->append(">");
38 0 : break;
39 :
40 : default:
41 0 : output->push_back(*p);
42 0 : break;
43 : }
44 : }
45 0 : }
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 1420 : Tracked::Tracked() : tracked_births_(NULL), tracked_birth_time_(Time::Now()) {
60 1420 : if (!ThreadData::IsActive())
61 1420 : return;
62 0 : SetBirthPlace(Location("NoFunctionName", "NeedToSetBirthPlace", -1));
63 : }
64 :
65 1419 : Tracked::~Tracked() {
66 1419 : if (!ThreadData::IsActive() || !tracked_births_)
67 1419 : return;
68 : ThreadData::current()->TallyADeath(*tracked_births_,
69 0 : Time::Now() - tracked_birth_time_);
70 2838 : }
71 :
72 1419 : void Tracked::SetBirthPlace(const Location& from_here) {
73 1419 : if (!ThreadData::IsActive())
74 1419 : return;
75 0 : if (tracked_births_)
76 0 : tracked_births_->ForgetBirth();
77 0 : ThreadData* current_thread_data = ThreadData::current();
78 0 : if (!current_thread_data)
79 0 : return; // Shutdown started, and this thread wasn't registered.
80 0 : tracked_births_ = current_thread_data->FindLifetime(from_here);
81 0 : tracked_births_->RecordBirth();
82 : }
83 :
84 0 : void Tracked::ResetBirthTime() {
85 0 : tracked_birth_time_ = Time::Now();
86 0 : }
87 :
88 0 : bool Tracked::MissingBirthplace() const {
89 0 : return -1 == tracked_births_->location().line_number();
90 : }
91 :
92 : #endif // NDEBUG
93 :
94 : } // namespace tracked_objects
|