Home | History | Annotate | Download | only in base
      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 #include "base/hash.h"
     15 
     16 namespace tracked_objects {
     17 
     18 // Location provides basic info where of an object was constructed, or was
     19 // significantly brought to life.
     20 class 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   // Copy constructor.
     34   Location(const Location& other);
     35 
     36   // Comparator for hash map insertion.
     37   // No need to use |function_name_| since the other two fields uniquely
     38   // identify this location.
     39   bool operator==(const Location& other) const {
     40     return line_number_ == other.line_number_ &&
     41            file_name_ == other.file_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 
     51   // Hash operator for hash maps.
     52   struct Hash {
     53     size_t operator()(const Location& location) const {
     54       // Compute the hash value using file name pointer and line number.
     55       // No need to use |function_name_| since the other two fields uniquely
     56       // identify this location.
     57 
     58       // The file name will always be uniquely identified by its pointer since
     59       // it comes from __FILE__, so no need to check the contents of the string.
     60       // See the definition of FROM_HERE in location.h, and how it is used
     61       // elsewhere.
     62       return base::HashInts(reinterpret_cast<uintptr_t>(location.file_name()),
     63                             location.line_number());
     64     }
     65   };
     66 
     67   // Translate the some of the state in this instance into a human readable
     68   // string with HTML characters in the function names escaped, and append that
     69   // string to |output|.  Inclusion of the file_name_ and function_name_ are
     70   // optional, and controlled by the boolean arguments.
     71   void Write(bool display_filename, bool display_function_name,
     72              std::string* output) const;
     73 
     74   // Write function_name_ in HTML with '<' and '>' properly encoded.
     75   void WriteFunctionName(std::string* output) const;
     76 
     77  private:
     78   const char* function_name_;
     79   const char* file_name_;
     80   int line_number_;
     81   const void* program_counter_;
     82 };
     83 
     84 // A "snapshotted" representation of the Location class that can safely be
     85 // passed across process boundaries.
     86 struct BASE_EXPORT LocationSnapshot {
     87   // The default constructor is exposed to support the IPC serialization macros.
     88   LocationSnapshot();
     89   explicit LocationSnapshot(const tracked_objects::Location& location);
     90   ~LocationSnapshot();
     91 
     92   std::string file_name;
     93   std::string function_name;
     94   int line_number;
     95 };
     96 
     97 BASE_EXPORT const void* GetProgramCounter();
     98 
     99 // Define a macro to record the current source location.
    100 #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__func__)
    101 
    102 #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name)                        \
    103     ::tracked_objects::Location(function_name,                                 \
    104                                 __FILE__,                                      \
    105                                 __LINE__,                                      \
    106                                 ::tracked_objects::GetProgramCounter())
    107 
    108 }  // namespace tracked_objects
    109 
    110 #endif  // BASE_LOCATION_H_
    111