Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "build/build_config.h" |
michael@0 | 6 | |
michael@0 | 7 | #if defined(COMPILER_MSVC) |
michael@0 | 8 | // MSDN says to #include <intrin.h>, but that breaks the VS2005 build. |
michael@0 | 9 | extern "C" { |
michael@0 | 10 | void* _ReturnAddress(); |
michael@0 | 11 | } |
michael@0 | 12 | #endif |
michael@0 | 13 | |
michael@0 | 14 | #include "base/location.h" |
michael@0 | 15 | #include "base/strings/string_number_conversions.h" |
michael@0 | 16 | #include "base/strings/stringprintf.h" |
michael@0 | 17 | |
michael@0 | 18 | namespace tracked_objects { |
michael@0 | 19 | |
michael@0 | 20 | Location::Location(const char* function_name, |
michael@0 | 21 | const char* file_name, |
michael@0 | 22 | int line_number, |
michael@0 | 23 | const void* program_counter) |
michael@0 | 24 | : function_name_(function_name), |
michael@0 | 25 | file_name_(file_name), |
michael@0 | 26 | line_number_(line_number), |
michael@0 | 27 | program_counter_(program_counter) { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | Location::Location() |
michael@0 | 31 | : function_name_("Unknown"), |
michael@0 | 32 | file_name_("Unknown"), |
michael@0 | 33 | line_number_(-1), |
michael@0 | 34 | program_counter_(NULL) { |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | std::string Location::ToString() const { |
michael@0 | 38 | return std::string(function_name_) + "@" + file_name_ + ":" + |
michael@0 | 39 | base::IntToString(line_number_); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | void Location::Write(bool display_filename, bool display_function_name, |
michael@0 | 43 | std::string* output) const { |
michael@0 | 44 | base::StringAppendF(output, "%s[%d] ", |
michael@0 | 45 | display_filename ? file_name_ : "line", |
michael@0 | 46 | line_number_); |
michael@0 | 47 | |
michael@0 | 48 | if (display_function_name) { |
michael@0 | 49 | WriteFunctionName(output); |
michael@0 | 50 | output->push_back(' '); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | void Location::WriteFunctionName(std::string* output) const { |
michael@0 | 55 | // Translate "<" to "<" for HTML safety. |
michael@0 | 56 | // TODO(jar): Support ASCII or html for logging in ASCII. |
michael@0 | 57 | for (const char *p = function_name_; *p; p++) { |
michael@0 | 58 | switch (*p) { |
michael@0 | 59 | case '<': |
michael@0 | 60 | output->append("<"); |
michael@0 | 61 | break; |
michael@0 | 62 | |
michael@0 | 63 | case '>': |
michael@0 | 64 | output->append(">"); |
michael@0 | 65 | break; |
michael@0 | 66 | |
michael@0 | 67 | default: |
michael@0 | 68 | output->push_back(*p); |
michael@0 | 69 | break; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | //------------------------------------------------------------------------------ |
michael@0 | 75 | LocationSnapshot::LocationSnapshot() : line_number(-1) { |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | LocationSnapshot::LocationSnapshot( |
michael@0 | 79 | const tracked_objects::Location& location) |
michael@0 | 80 | : file_name(location.file_name()), |
michael@0 | 81 | function_name(location.function_name()), |
michael@0 | 82 | line_number(location.line_number()) { |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | LocationSnapshot::~LocationSnapshot() { |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | //------------------------------------------------------------------------------ |
michael@0 | 89 | #if defined(COMPILER_MSVC) |
michael@0 | 90 | __declspec(noinline) |
michael@0 | 91 | #endif |
michael@0 | 92 | BASE_EXPORT const void* GetProgramCounter() { |
michael@0 | 93 | #if defined(COMPILER_MSVC) |
michael@0 | 94 | return _ReturnAddress(); |
michael@0 | 95 | #elif defined(COMPILER_GCC) |
michael@0 | 96 | return __builtin_extract_return_addr(__builtin_return_address(0)); |
michael@0 | 97 | #endif // COMPILER_GCC |
michael@0 | 98 | |
michael@0 | 99 | return NULL; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | } // namespace tracked_objects |