OLD | NEW |
(Empty) | |
| 1 // 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 #include "base/location.h" |
| 6 #include "base/stringprintf.h" |
| 7 |
| 8 namespace tracked_objects { |
| 9 |
| 10 Location::Location(const char* function_name, |
| 11 const char* file_name, |
| 12 int line_number, |
| 13 const void* program_counter) |
| 14 : function_name_(function_name), |
| 15 file_name_(file_name), |
| 16 line_number_(line_number), |
| 17 program_counter_(program_counter) { |
| 18 } |
| 19 |
| 20 Location::Location() |
| 21 : function_name_("Unknown"), |
| 22 file_name_("Unknown"), |
| 23 line_number_(-1), |
| 24 program_counter_(NULL) { |
| 25 } |
| 26 |
| 27 void Location::Write(bool display_filename, bool display_function_name, |
| 28 std::string* output) const { |
| 29 base::StringAppendF(output, "%s[%d] ", |
| 30 display_filename ? file_name_ : "line", |
| 31 line_number_); |
| 32 |
| 33 if (display_function_name) { |
| 34 WriteFunctionName(output); |
| 35 output->push_back(' '); |
| 36 } |
| 37 } |
| 38 |
| 39 void Location::WriteFunctionName(std::string* output) const { |
| 40 // Translate "<" to "<" for HTML safety. |
| 41 // TODO(jar): Support ASCII or html for logging in ASCII. |
| 42 for (const char *p = function_name_; *p; p++) { |
| 43 switch (*p) { |
| 44 case '<': |
| 45 output->append("<"); |
| 46 break; |
| 47 |
| 48 case '>': |
| 49 output->append(">"); |
| 50 break; |
| 51 |
| 52 default: |
| 53 output->push_back(*p); |
| 54 break; |
| 55 } |
| 56 } |
| 57 } |
| 58 |
| 59 #if defined(COMPILER_MSVC) |
| 60 __declspec(noinline) |
| 61 #endif |
| 62 BASE_EXPORT const void* GetProgramCounter() { |
| 63 #if defined(COMPILER_MSVC) |
| 64 return _ReturnAddress(); |
| 65 #elif defined(COMPILER_GCC) |
| 66 return __builtin_extract_return_addr(__builtin_return_address(0)); |
| 67 #endif // COMPILER_GCC |
| 68 |
| 69 return NULL; |
| 70 } |
| 71 |
| 72 } // namespace tracked_objects |
OLD | NEW |