OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/location.h" | |
6 #include "base/debug/trace_event.h" | |
7 #include "base/json/string_escape.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/strings/stringprintf.h" | |
5 #include "build/build_config.h" | 10 #include "build/build_config.h" |
6 | 11 |
7 #if defined(COMPILER_MSVC) | 12 #if defined(COMPILER_MSVC) |
8 // MSDN says to #include <intrin.h>, but that breaks the VS2005 build. | 13 // MSDN says to #include <intrin.h>, but that breaks the VS2005 build. |
9 extern "C" { | 14 extern "C" { |
10 void* _ReturnAddress(); | 15 void* _ReturnAddress(); |
11 } | 16 } |
12 #endif | 17 #endif |
13 | 18 |
14 #include "base/location.h" | 19 namespace { |
15 #include "base/strings/string_number_conversions.h" | 20 |
16 #include "base/strings/stringprintf.h" | 21 class LocationConvertableToTraceFormat |
22 : public base::debug::ConvertableToTraceFormat { | |
23 public: | |
24 LocationConvertableToTraceFormat(const char* function_name, | |
25 const char* file_name) | |
26 : function_name_(function_name), | |
27 file_name_(file_name) { | |
28 } | |
29 | |
30 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { | |
jar (doing other things)
2013/07/08 17:08:33
When/how often does this get called? Is it only at
Xianzhu
2013/07/08 18:51:39
It's only called at display (or save) time, once p
| |
31 out->append("\""); | |
32 base::JsonDoubleQuote(function_name_, false, out); | |
33 out->append("@"); | |
34 base::JsonDoubleQuote(file_name_, false, out); | |
35 out->append("\""); | |
36 } | |
37 | |
38 private: | |
39 const char* function_name_; | |
40 const char* file_name_; | |
41 }; | |
42 | |
43 } // namespace | |
17 | 44 |
18 namespace tracked_objects { | 45 namespace tracked_objects { |
19 | 46 |
20 Location::Location(const char* function_name, | 47 Location::Location(const char* function_name, |
21 const char* file_name, | 48 const char* file_name, |
22 int line_number, | 49 int line_number, |
23 const void* program_counter) | 50 const void* program_counter) |
24 : function_name_(function_name), | 51 : function_name_(function_name), |
25 file_name_(file_name), | 52 file_name_(file_name), |
26 line_number_(line_number), | 53 line_number_(line_number), |
27 program_counter_(program_counter) { | 54 program_counter_(program_counter) { |
28 } | 55 } |
29 | 56 |
30 Location::Location() | 57 Location::Location() |
31 : function_name_("Unknown"), | 58 : function_name_("Unknown"), |
32 file_name_("Unknown"), | 59 file_name_("Unknown"), |
33 line_number_(-1), | 60 line_number_(-1), |
34 program_counter_(NULL) { | 61 program_counter_(NULL) { |
35 } | 62 } |
36 | 63 |
37 std::string Location::ToString() const { | 64 std::string Location::ToString() const { |
38 return std::string(function_name_) + "@" + file_name_ + ":" + | 65 return std::string(function_name_) + "@" + file_name_ + ":" + |
39 base::IntToString(line_number_); | 66 base::IntToString(line_number_); |
40 } | 67 } |
41 | 68 |
69 scoped_ptr<base::debug::ConvertableToTraceFormat> | |
70 Location::ToTraceFormat() const { | |
71 return scoped_ptr<base::debug::ConvertableToTraceFormat>( | |
72 new LocationConvertableToTraceFormat(function_name_, file_name_)); | |
73 } | |
74 | |
42 void Location::Write(bool display_filename, bool display_function_name, | 75 void Location::Write(bool display_filename, bool display_function_name, |
43 std::string* output) const { | 76 std::string* output) const { |
44 base::StringAppendF(output, "%s[%d] ", | 77 base::StringAppendF(output, "%s[%d] ", |
45 display_filename ? file_name_ : "line", | 78 display_filename ? file_name_ : "line", |
46 line_number_); | 79 line_number_); |
47 | 80 |
48 if (display_function_name) { | 81 if (display_function_name) { |
49 WriteFunctionName(output); | 82 WriteFunctionName(output); |
50 output->push_back(' '); | 83 output->push_back(' '); |
51 } | 84 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 #if defined(COMPILER_MSVC) | 126 #if defined(COMPILER_MSVC) |
94 return _ReturnAddress(); | 127 return _ReturnAddress(); |
95 #elif defined(COMPILER_GCC) | 128 #elif defined(COMPILER_GCC) |
96 return __builtin_extract_return_addr(__builtin_return_address(0)); | 129 return __builtin_extract_return_addr(__builtin_return_address(0)); |
97 #endif // COMPILER_GCC | 130 #endif // COMPILER_GCC |
98 | 131 |
99 return NULL; | 132 return NULL; |
100 } | 133 } |
101 | 134 |
102 } // namespace tracked_objects | 135 } // namespace tracked_objects |
OLD | NEW |