blob: 4a382648e9b370de9baeb137a7933db923a8ac70 [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// Copyright (c) 2012 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 <cassert>
9#include <string>
10
11#include "base/base_export.h"
12#include "base/basictypes.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070013
14namespace tracked_objects {
15
16// Location provides basic info where of an object was constructed, or was
17// significantly brought to life.
Vitaly Buka02fe5932015-08-23 12:23:08 -070018class BASE_EXPORT Location {
Vitaly Bukacbed2062015-08-17 12:54:05 -070019 public:
20 // Constructor should be called with a long-lived char*, such as __FILE__.
21 // It assumes the provided value will persist as a global constant, and it
22 // will not make a copy of it.
23 Location(const char* function_name,
24 const char* file_name,
25 int line_number,
26 const void* program_counter);
27
28 // Provide a default constructor for easy of debugging.
29 Location();
30
31 // Copy constructor.
32 Location(const Location& other);
33
34 // Comparator for hash map insertion.
35 // No need to use |function_name_| since the other two fields uniquely
36 // identify this location.
37 bool operator==(const Location& other) const {
38 return line_number_ == other.line_number_ &&
39 file_name_ == other.file_name_;
40 }
41
42 const char* function_name() const { return function_name_; }
43 const char* file_name() const { return file_name_; }
44 int line_number() const { return line_number_; }
45 const void* program_counter() const { return program_counter_; }
46
47 std::string ToString() const;
48
Vitaly Bukacbed2062015-08-17 12:54:05 -070049 // Translate the some of the state in this instance into a human readable
50 // string with HTML characters in the function names escaped, and append that
51 // string to |output|. Inclusion of the file_name_ and function_name_ are
52 // optional, and controlled by the boolean arguments.
53 void Write(bool display_filename, bool display_function_name,
54 std::string* output) const;
55
56 // Write function_name_ in HTML with '<' and '>' properly encoded.
57 void WriteFunctionName(std::string* output) const;
58
59 private:
60 const char* function_name_;
61 const char* file_name_;
62 int line_number_;
63 const void* program_counter_;
64};
65
66// A "snapshotted" representation of the Location class that can safely be
67// passed across process boundaries.
68struct BASE_EXPORT LocationSnapshot {
69 // The default constructor is exposed to support the IPC serialization macros.
70 LocationSnapshot();
71 explicit LocationSnapshot(const tracked_objects::Location& location);
72 ~LocationSnapshot();
73
74 std::string file_name;
75 std::string function_name;
76 int line_number;
77};
78
Vitaly Buka02fe5932015-08-23 12:23:08 -070079BASE_EXPORT const void* GetProgramCounter();
Vitaly Bukacbed2062015-08-17 12:54:05 -070080
81// Define a macro to record the current source location.
82#define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__)
83
84#define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \
85 ::tracked_objects::Location(function_name, \
86 __FILE__, \
87 __LINE__, \
88 ::tracked_objects::GetProgramCounter())
89
90} // namespace tracked_objects
91
92#endif // BASE_LOCATION_H_