michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_LOCATION_H_ michael@0: #define BASE_LOCATION_H_ michael@0: michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: michael@0: namespace tracked_objects { michael@0: michael@0: // Location provides basic info where of an object was constructed, or was michael@0: // significantly brought to life. michael@0: class BASE_EXPORT Location { michael@0: public: michael@0: // Constructor should be called with a long-lived char*, such as __FILE__. michael@0: // It assumes the provided value will persist as a global constant, and it michael@0: // will not make a copy of it. michael@0: Location(const char* function_name, michael@0: const char* file_name, michael@0: int line_number, michael@0: const void* program_counter); michael@0: michael@0: // Provide a default constructor for easy of debugging. michael@0: Location(); michael@0: michael@0: // Comparison operator for insertion into a std::map<> hash tables. michael@0: // All we need is *some* (any) hashing distinction. Strings should already michael@0: // be unique, so we don't bother with strcmp or such. michael@0: // Use line number as the primary key (because it is fast, and usually gets us michael@0: // a difference), and then pointers as secondary keys (just to get some michael@0: // distinctions). michael@0: bool operator < (const Location& other) const { michael@0: if (line_number_ != other.line_number_) michael@0: return line_number_ < other.line_number_; michael@0: if (file_name_ != other.file_name_) michael@0: return file_name_ < other.file_name_; michael@0: return function_name_ < other.function_name_; michael@0: } michael@0: michael@0: const char* function_name() const { return function_name_; } michael@0: const char* file_name() const { return file_name_; } michael@0: int line_number() const { return line_number_; } michael@0: const void* program_counter() const { return program_counter_; } michael@0: michael@0: std::string ToString() const; michael@0: michael@0: // Translate the some of the state in this instance into a human readable michael@0: // string with HTML characters in the function names escaped, and append that michael@0: // string to |output|. Inclusion of the file_name_ and function_name_ are michael@0: // optional, and controlled by the boolean arguments. michael@0: void Write(bool display_filename, bool display_function_name, michael@0: std::string* output) const; michael@0: michael@0: // Write function_name_ in HTML with '<' and '>' properly encoded. michael@0: void WriteFunctionName(std::string* output) const; michael@0: michael@0: private: michael@0: const char* function_name_; michael@0: const char* file_name_; michael@0: int line_number_; michael@0: const void* program_counter_; michael@0: }; michael@0: michael@0: // A "snapshotted" representation of the Location class that can safely be michael@0: // passed across process boundaries. michael@0: struct BASE_EXPORT LocationSnapshot { michael@0: // The default constructor is exposed to support the IPC serialization macros. michael@0: LocationSnapshot(); michael@0: explicit LocationSnapshot(const tracked_objects::Location& location); michael@0: ~LocationSnapshot(); michael@0: michael@0: std::string file_name; michael@0: std::string function_name; michael@0: int line_number; michael@0: }; michael@0: michael@0: BASE_EXPORT const void* GetProgramCounter(); michael@0: michael@0: // Define a macro to record the current source location. michael@0: #define FROM_HERE FROM_HERE_WITH_EXPLICIT_FUNCTION(__FUNCTION__) michael@0: michael@0: #define FROM_HERE_WITH_EXPLICIT_FUNCTION(function_name) \ michael@0: ::tracked_objects::Location(function_name, \ michael@0: __FILE__, \ michael@0: __LINE__, \ michael@0: ::tracked_objects::GetProgramCounter()) michael@0: michael@0: } // namespace tracked_objects michael@0: michael@0: #endif // BASE_LOCATION_H_