isherman@chromium.org | a9490ac | 2012-03-17 06:43:41 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 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" |
isherman@chromium.org | a9490ac | 2012-03-17 06:43:41 +0900 | [diff] [blame] | 11 | |
| 12 | namespace base { |
| 13 | class DictionaryValue; |
| 14 | } |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 15 | |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 16 | namespace tracked_objects { |
| 17 | |
| 18 | // Location provides basic info where of an object was constructed, or was |
| 19 | // significantly brought to life. |
| 20 | class BASE_EXPORT Location { |
| 21 | public: |
| 22 | // Constructor should be called with a long-lived char*, such as __FILE__. |
| 23 | // It assumes the provided value will persist as a global constant, and it |
| 24 | // will not make a copy of it. |
| 25 | Location(const char* function_name, |
| 26 | const char* file_name, |
| 27 | int line_number, |
| 28 | const void* program_counter); |
| 29 | |
| 30 | // Provide a default constructor for easy of debugging. |
| 31 | Location(); |
| 32 | |
| 33 | // Comparison operator for insertion into a std::map<> hash tables. |
| 34 | // All we need is *some* (any) hashing distinction. Strings should already |
| 35 | // be unique, so we don't bother with strcmp or such. |
| 36 | // Use line number as the primary key (because it is fast, and usually gets us |
| 37 | // a difference), and then pointers as secondary keys (just to get some |
| 38 | // distinctions). |
| 39 | bool operator < (const Location& other) const { |
| 40 | if (line_number_ != other.line_number_) |
| 41 | return line_number_ < other.line_number_; |
| 42 | if (file_name_ != other.file_name_) |
| 43 | return file_name_ < other.file_name_; |
| 44 | return function_name_ < other.function_name_; |
| 45 | } |
| 46 | |
| 47 | const char* function_name() const { return function_name_; } |
| 48 | const char* file_name() const { return file_name_; } |
| 49 | int line_number() const { return line_number_; } |
| 50 | const void* program_counter() const { return program_counter_; } |
| 51 | |
| 52 | std::string ToString() const; |
| 53 | |
jar@chromium.org | 666ef9c | 2011-10-25 03:55:16 +0900 | [diff] [blame] | 54 | // Translate the some of the state in this instance into a human readable |
| 55 | // string with HTML characters in the function names escaped, and append that |
| 56 | // string to |output|. Inclusion of the file_name_ and function_name_ are |
| 57 | // optional, and controlled by the boolean arguments. |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 58 | void Write(bool display_filename, bool display_function_name, |
| 59 | std::string* output) const; |
| 60 | |
| 61 | // Write function_name_ in HTML with '<' and '>' properly encoded. |
| 62 | void WriteFunctionName(std::string* output) const; |
| 63 | |
jar@chromium.org | 666ef9c | 2011-10-25 03:55:16 +0900 | [diff] [blame] | 64 | // Construct a Value* representation. The caller assumes ownership of the |
| 65 | // memory in the returned instance. |
| 66 | base::DictionaryValue* ToValue() const; |
| 67 | |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 68 | private: |
jbates@chromium.org | b84db52 | 2011-10-04 02:51:25 +0900 | [diff] [blame] | 69 | const char* function_name_; |
| 70 | const char* file_name_; |
| 71 | int line_number_; |
| 72 | const void* program_counter_; |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | BASE_EXPORT const void* GetProgramCounter(); |
| 76 | |
| 77 | // Define a macro to record the current source location. |
jar@chromium.org | 27cf297 | 2011-11-09 02:09:21 +0900 | [diff] [blame] | 78 | #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__) |
| 79 | |
| 80 | #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ |
| 81 | ::tracked_objects::Location(function_name, \ |
| 82 | __FILE__, \ |
| 83 | __LINE__, \ |
| 84 | ::tracked_objects::GetProgramCounter()) |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 85 | |
| 86 | } // namespace tracked_objects |
| 87 | |
| 88 | #endif // BASE_LOCATION_H_ |