blob: fc9d3603251a27f526f599e564714ec3bb5befbc [file] [log] [blame]
isherman@chromium.orga9490ac2012-03-17 06:43:41 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +09002// 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.orga9490ac2012-03-17 06:43:41 +090011
12namespace base {
13class DictionaryValue;
14}
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090015
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090016namespace tracked_objects {
17
18// Location provides basic info where of an object was constructed, or was
19// significantly brought to life.
20class 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.org666ef9c2011-10-25 03:55:16 +090054 // 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.org8e2e3002011-09-22 03:05:41 +090058 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.org666ef9c2011-10-25 03:55:16 +090064 // Construct a Value* representation. The caller assumes ownership of the
65 // memory in the returned instance.
66 base::DictionaryValue* ToValue() const;
67
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090068 private:
jbates@chromium.orgb84db522011-10-04 02:51:25 +090069 const char* function_name_;
70 const char* file_name_;
71 int line_number_;
72 const void* program_counter_;
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090073};
74
75BASE_EXPORT const void* GetProgramCounter();
76
77// Define a macro to record the current source location.
jar@chromium.org27cf2972011-11-09 02:09:21 +090078#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.org8e2e3002011-09-22 03:05:41 +090085
86} // namespace tracked_objects
87
88#endif // BASE_LOCATION_H_