|
1 // Copyright (c) 2007, Google Inc. |
|
2 // All rights reserved. |
|
3 // |
|
4 // Redistribution and use in source and binary forms, with or without |
|
5 // modification, are permitted provided that the following conditions are |
|
6 // met: |
|
7 // |
|
8 // * Redistributions of source code must retain the above copyright |
|
9 // notice, this list of conditions and the following disclaimer. |
|
10 // * Redistributions in binary form must reproduce the above |
|
11 // copyright notice, this list of conditions and the following disclaimer |
|
12 // in the documentation and/or other materials provided with the |
|
13 // distribution. |
|
14 // * Neither the name of Google Inc. nor the names of its |
|
15 // contributors may be used to endorse or promote products derived from |
|
16 // this software without specific prior written permission. |
|
17 // |
|
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
|
30 // logging.cc: Breakpad logging |
|
31 // |
|
32 // See logging.h for documentation. |
|
33 // |
|
34 // Author: Mark Mentovai |
|
35 |
|
36 #include <assert.h> |
|
37 #include <errno.h> |
|
38 #include <stdio.h> |
|
39 #include <string.h> |
|
40 #include <time.h> |
|
41 |
|
42 #include <string> |
|
43 |
|
44 #include "common/using_std_string.h" |
|
45 #include "common/logging.h" |
|
46 #include "common/pathname_stripper.h" |
|
47 |
|
48 #ifdef _WIN32 |
|
49 #define snprintf _snprintf |
|
50 #endif |
|
51 |
|
52 #ifdef __ANDROID__ |
|
53 # include <android/log.h> |
|
54 #endif |
|
55 |
|
56 namespace google_breakpad { |
|
57 |
|
58 LogStream::LogStream(std::ostream &stream, Severity severity, |
|
59 const char *file, int line) |
|
60 : stream_(stream) { |
|
61 time_t clock; |
|
62 time(&clock); |
|
63 struct tm tm_struct; |
|
64 #ifdef _WIN32 |
|
65 localtime_s(&tm_struct, &clock); |
|
66 #else |
|
67 localtime_r(&clock, &tm_struct); |
|
68 #endif |
|
69 char time_string[20]; |
|
70 strftime(time_string, sizeof(time_string), "%Y-%m-%d %H:%M:%S", &tm_struct); |
|
71 |
|
72 const char *severity_string = "UNKNOWN_SEVERITY"; |
|
73 switch (severity) { |
|
74 case SEVERITY_INFO: |
|
75 severity_string = "INFO"; |
|
76 break; |
|
77 case SEVERITY_ERROR: |
|
78 severity_string = "ERROR"; |
|
79 break; |
|
80 } |
|
81 |
|
82 str_ << time_string << ": " << PathnameStripper::File(file) << ":" << |
|
83 line << ": " << severity_string << ": "; |
|
84 } |
|
85 |
|
86 LogStream::~LogStream() { |
|
87 #ifdef __ANDROID__ |
|
88 __android_log_print(ANDROID_LOG_ERROR, |
|
89 "Profiler", "%s", str_.str().c_str()); |
|
90 #else |
|
91 stream_ << str_.str(); |
|
92 stream_ << std::endl; |
|
93 #endif |
|
94 } |
|
95 |
|
96 string HexString(uint32_t number) { |
|
97 char buffer[11]; |
|
98 snprintf(buffer, sizeof(buffer), "0x%x", number); |
|
99 return string(buffer); |
|
100 } |
|
101 |
|
102 string HexString(uint64_t number) { |
|
103 char buffer[19]; |
|
104 snprintf(buffer, sizeof(buffer), "0x%" PRIx64, number); |
|
105 return string(buffer); |
|
106 } |
|
107 |
|
108 string HexString(int number) { |
|
109 char buffer[19]; |
|
110 snprintf(buffer, sizeof(buffer), "0x%x", number); |
|
111 return string(buffer); |
|
112 } |
|
113 |
|
114 int ErrnoString(string *error_string) { |
|
115 assert(error_string); |
|
116 |
|
117 // strerror isn't necessarily thread-safe. strerror_r would be preferrable, |
|
118 // but GNU libc uses a nonstandard strerror_r by default, which returns a |
|
119 // char* (rather than an int success indicator) and doesn't necessarily |
|
120 // use the supplied buffer. |
|
121 error_string->assign(strerror(errno)); |
|
122 return errno; |
|
123 } |
|
124 |
|
125 } // namespace google_breakpad |
|
126 |
|
127 bool is_power_of_2(uint64_t x_in) |
|
128 { |
|
129 uint64_t x = x_in; |
|
130 x = x | (x >> 1); |
|
131 x = x | (x >> 2); |
|
132 x = x | (x >> 4); |
|
133 x = x | (x >> 8); |
|
134 x = x | (x >> 16); |
|
135 x = x | (x >> 32); |
|
136 x = x - (x >> 1); |
|
137 // x has now been rounded down to the nearest power of 2 <= x_in. |
|
138 return x == x_in; |
|
139 } |