Add creis, davidben, and mmenke to content/browser/loader/OWNERs.
[chromium-blink-merge.git] / base / location.cc
blobd87ed3581fb5de9499e731fcd7ed0183dd946447
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.
5 #include "build/build_config.h"
7 #if defined(COMPILER_MSVC)
8 // MSDN says to #include <intrin.h>, but that breaks the VS2005 build.
9 extern "C" {
10 void* _ReturnAddress();
12 #endif
14 #include "base/location.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/stringprintf.h"
18 namespace tracked_objects {
20 Location::Location(const char* function_name,
21 const char* file_name,
22 int line_number,
23 const void* program_counter)
24 : function_name_(function_name),
25 file_name_(file_name),
26 line_number_(line_number),
27 program_counter_(program_counter) {
30 Location::Location()
31 : function_name_("Unknown"),
32 file_name_("Unknown"),
33 line_number_(-1),
34 program_counter_(NULL) {
37 std::string Location::ToString() const {
38 return std::string(function_name_) + "@" + file_name_ + ":" +
39 base::IntToString(line_number_);
42 void Location::Write(bool display_filename, bool display_function_name,
43 std::string* output) const {
44 base::StringAppendF(output, "%s[%d] ",
45 display_filename ? file_name_ : "line",
46 line_number_);
48 if (display_function_name) {
49 WriteFunctionName(output);
50 output->push_back(' ');
54 void Location::WriteFunctionName(std::string* output) const {
55 // Translate "<" to "&lt;" for HTML safety.
56 // TODO(jar): Support ASCII or html for logging in ASCII.
57 for (const char *p = function_name_; *p; p++) {
58 switch (*p) {
59 case '<':
60 output->append("&lt;");
61 break;
63 case '>':
64 output->append("&gt;");
65 break;
67 default:
68 output->push_back(*p);
69 break;
74 //------------------------------------------------------------------------------
75 LocationSnapshot::LocationSnapshot() : line_number(-1) {
78 LocationSnapshot::LocationSnapshot(
79 const tracked_objects::Location& location)
80 : file_name(location.file_name()),
81 function_name(location.function_name()),
82 line_number(location.line_number()) {
85 LocationSnapshot::~LocationSnapshot() {
88 //------------------------------------------------------------------------------
89 #if defined(COMPILER_MSVC)
90 __declspec(noinline)
91 #endif
92 BASE_EXPORT const void* GetProgramCounter() {
93 #if defined(COMPILER_MSVC)
94 return _ReturnAddress();
95 #elif defined(COMPILER_GCC) && !defined(OS_NACL)
96 return __builtin_extract_return_addr(__builtin_return_address(0));
97 #endif // COMPILER_GCC
98 return NULL;
101 } // namespace tracked_objects