|
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 |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #ifndef BASE_LOCATION_H_ |
|
6 #define BASE_LOCATION_H_ |
|
7 |
|
8 #include <string> |
|
9 |
|
10 #include "base/base_export.h" |
|
11 #include "base/basictypes.h" |
|
12 |
|
13 namespace tracked_objects { |
|
14 |
|
15 // Location provides basic info where of an object was constructed, or was |
|
16 // significantly brought to life. |
|
17 class BASE_EXPORT Location { |
|
18 public: |
|
19 // Constructor should be called with a long-lived char*, such as __FILE__. |
|
20 // It assumes the provided value will persist as a global constant, and it |
|
21 // will not make a copy of it. |
|
22 Location(const char* function_name, |
|
23 const char* file_name, |
|
24 int line_number, |
|
25 const void* program_counter); |
|
26 |
|
27 // Provide a default constructor for easy of debugging. |
|
28 Location(); |
|
29 |
|
30 // Comparison operator for insertion into a std::map<> hash tables. |
|
31 // All we need is *some* (any) hashing distinction. Strings should already |
|
32 // be unique, so we don't bother with strcmp or such. |
|
33 // Use line number as the primary key (because it is fast, and usually gets us |
|
34 // a difference), and then pointers as secondary keys (just to get some |
|
35 // distinctions). |
|
36 bool operator < (const Location& other) const { |
|
37 if (line_number_ != other.line_number_) |
|
38 return line_number_ < other.line_number_; |
|
39 if (file_name_ != other.file_name_) |
|
40 return file_name_ < other.file_name_; |
|
41 return function_name_ < other.function_name_; |
|
42 } |
|
43 |
|
44 const char* function_name() const { return function_name_; } |
|
45 const char* file_name() const { return file_name_; } |
|
46 int line_number() const { return line_number_; } |
|
47 const void* program_counter() const { return program_counter_; } |
|
48 |
|
49 std::string ToString() const; |
|
50 |
|
51 // Translate the some of the state in this instance into a human readable |
|
52 // string with HTML characters in the function names escaped, and append that |
|
53 // string to |output|. Inclusion of the file_name_ and function_name_ are |
|
54 // optional, and controlled by the boolean arguments. |
|
55 void Write(bool display_filename, bool display_function_name, |
|
56 std::string* output) const; |
|
57 |
|
58 // Write function_name_ in HTML with '<' and '>' properly encoded. |
|
59 void WriteFunctionName(std::string* output) const; |
|
60 |
|
61 private: |
|
62 const char* function_name_; |
|
63 const char* file_name_; |
|
64 int line_number_; |
|
65 const void* program_counter_; |
|
66 }; |
|
67 |
|
68 // A "snapshotted" representation of the Location class that can safely be |
|
69 // passed across process boundaries. |
|
70 struct BASE_EXPORT LocationSnapshot { |
|
71 // The default constructor is exposed to support the IPC serialization macros. |
|
72 LocationSnapshot(); |
|
73 explicit LocationSnapshot(const tracked_objects::Location& location); |
|
74 ~LocationSnapshot(); |
|
75 |
|
76 std::string file_name; |
|
77 std::string function_name; |
|
78 int line_number; |
|
79 }; |
|
80 |
|
81 BASE_EXPORT const void* GetProgramCounter(); |
|
82 |
|
83 // Define a macro to record the current source location. |
|
84 #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__) |
|
85 |
|
86 #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ |
|
87 ::tracked_objects::Location(function_name, \ |
|
88 __FILE__, \ |
|
89 __LINE__, \ |
|
90 ::tracked_objects::GetProgramCounter()) |
|
91 |
|
92 } // namespace tracked_objects |
|
93 |
|
94 #endif // BASE_LOCATION_H_ |