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