- add sources.
[platform/framework/web/crosswalk.git] / src / base / location.cc
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.
4
5 #include "build/build_config.h"
6
7 #if defined(COMPILER_MSVC)
8 // MSDN says to #include <intrin.h>, but that breaks the VS2005 build.
9 extern "C" {
10   void* _ReturnAddress();
11 }
12 #endif
13
14 #include "base/location.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/stringprintf.h"
17
18 namespace tracked_objects {
19
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) {
28 }
29
30 Location::Location()
31     : function_name_("Unknown"),
32       file_name_("Unknown"),
33       line_number_(-1),
34       program_counter_(NULL) {
35 }
36
37 std::string Location::ToString() const {
38   return std::string(function_name_) + "@" + file_name_ + ":" +
39       base::IntToString(line_number_);
40 }
41
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_);
47
48   if (display_function_name) {
49     WriteFunctionName(output);
50     output->push_back(' ');
51   }
52 }
53
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;
62
63       case '>':
64         output->append("&gt;");
65         break;
66
67       default:
68         output->push_back(*p);
69         break;
70     }
71   }
72 }
73
74 //------------------------------------------------------------------------------
75 LocationSnapshot::LocationSnapshot() : line_number(-1) {
76 }
77
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()) {
83 }
84
85 LocationSnapshot::~LocationSnapshot() {
86 }
87
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)
96   return __builtin_extract_return_addr(__builtin_return_address(0));
97 #endif  // COMPILER_GCC
98
99   return NULL;
100 }
101
102 }  // namespace tracked_objects