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