1 // Copyright (c) 2011 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.
5 #ifndef BASE_LOCATION_H_
6 #define BASE_LOCATION_H_
10 #include "base/base_export.h"
11 #include "base/values.h"
13 namespace tracked_objects
{
15 // Location provides basic info where of an object was constructed, or was
16 // significantly brought to life.
17 class BASE_EXPORT Location
{
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
,
25 const void* program_counter
);
27 // Provide a default constructor for easy of debugging.
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
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_
;
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_
; }
49 std::string
ToString() const;
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;
58 // Write function_name_ in HTML with '<' and '>' properly encoded.
59 void WriteFunctionName(std::string
* output
) const;
61 // Construct a Value* representation. The caller assumes ownership of the
62 // memory in the returned instance.
63 base::DictionaryValue
* ToValue() const;
66 const char* function_name_
;
67 const char* file_name_
;
69 const void* program_counter_
;
72 BASE_EXPORT
const void* GetProgramCounter();
74 // Define a macro to record the current source location.
75 #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__)
77 #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \
78 ::tracked_objects::Location(function_name, \
81 ::tracked_objects::GetProgramCounter())
83 } // namespace tracked_objects
85 #endif // BASE_LOCATION_H_