blob: 523bfaf189c43907342bb6cef037ae0a7a31f005 [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
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090013namespace tracked_objects {
14
15// Location provides basic info where of an object was constructed, or was
16// significantly brought to life.
17class 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
jar@chromium.org666ef9c2011-10-25 03:55:16 +090051 // 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.
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090055 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
jar@chromium.org666ef9c2011-10-25 03:55:16 +090061 // Construct a Value* representation. The caller assumes ownership of the
62 // memory in the returned instance.
63 base::DictionaryValue* ToValue() const;
64
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090065 private:
jbates@chromium.orgb84db522011-10-04 02:51:25 +090066 const char* function_name_;
67 const char* file_name_;
68 int line_number_;
69 const void* program_counter_;
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090070};
71
72BASE_EXPORT const void* GetProgramCounter();
73
74// Define a macro to record the current source location.
joi@chromium.org523ca432011-11-08 19:46:48 +090075#define FROM_HERE tracked_objects::Location( \
76 __FUNCTION__, \
77 __FILE__, \
78 __LINE__, \
79 tracked_objects::GetProgramCounter()) \
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +090080
81} // namespace tracked_objects
82
83#endif // BASE_LOCATION_H_