blob: fab8f4f54738ed792be68eed9829612bec085e75 [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"
11
12#ifndef NDEBUG
13#ifndef TRACK_ALL_TASK_OBJECTS
14#define TRACK_ALL_TASK_OBJECTS
15#endif // TRACK_ALL_TASK_OBJECTS
16#endif // NDEBUG
17
18namespace tracked_objects {
19
20// Location provides basic info where of an object was constructed, or was
21// significantly brought to life.
22class BASE_EXPORT Location {
23 public:
24 // Constructor should be called with a long-lived char*, such as __FILE__.
25 // It assumes the provided value will persist as a global constant, and it
26 // will not make a copy of it.
27 Location(const char* function_name,
28 const char* file_name,
29 int line_number,
30 const void* program_counter);
31
32 // Provide a default constructor for easy of debugging.
33 Location();
34
35 // Comparison operator for insertion into a std::map<> hash tables.
36 // All we need is *some* (any) hashing distinction. Strings should already
37 // be unique, so we don't bother with strcmp or such.
38 // Use line number as the primary key (because it is fast, and usually gets us
39 // a difference), and then pointers as secondary keys (just to get some
40 // distinctions).
41 bool operator < (const Location& other) const {
42 if (line_number_ != other.line_number_)
43 return line_number_ < other.line_number_;
44 if (file_name_ != other.file_name_)
45 return file_name_ < other.file_name_;
46 return function_name_ < other.function_name_;
47 }
48
49 const char* function_name() const { return function_name_; }
50 const char* file_name() const { return file_name_; }
51 int line_number() const { return line_number_; }
52 const void* program_counter() const { return program_counter_; }
53
54 std::string ToString() const;
55
56 void Write(bool display_filename, bool display_function_name,
57 std::string* output) const;
58
59 // Write function_name_ in HTML with '<' and '>' properly encoded.
60 void WriteFunctionName(std::string* output) const;
61
62 private:
63 const char* const function_name_;
64 const char* const file_name_;
65 const int line_number_;
66 const void* const program_counter_;
67};
68
69BASE_EXPORT const void* GetProgramCounter();
70
71// Define a macro to record the current source location.
72#define FROM_HERE tracked_objects::Location( \
73 __FUNCTION__, \
74 __FILE__, \
75 __LINE__, \
76 tracked_objects::GetProgramCounter()) \
77
78} // namespace tracked_objects
79
80#endif // BASE_LOCATION_H_