|
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.h: Breakpad logging |
|
31 // |
|
32 // Breakpad itself uses Breakpad logging with statements of the form: |
|
33 // BPLOG(severity) << "message"; |
|
34 // severity may be INFO, ERROR, or other values defined in this file. |
|
35 // |
|
36 // BPLOG is an overridable macro so that users can customize Breakpad's |
|
37 // logging. Left at the default, logging messages are sent to stderr along |
|
38 // with a timestamp and the source code location that produced a message. |
|
39 // The streams may be changed by redefining BPLOG_*_STREAM, the logging |
|
40 // behavior may be changed by redefining BPLOG_*, and the entire logging |
|
41 // system may be overridden by redefining BPLOG(severity). These |
|
42 // redefinitions may be passed to the preprocessor as a command-line flag |
|
43 // (-D). |
|
44 // |
|
45 // If an additional header is required to override Breakpad logging, it can |
|
46 // be specified by the BP_LOGGING_INCLUDE macro. If defined, this header |
|
47 // will #include the header specified by that macro. |
|
48 // |
|
49 // If any initialization is needed before logging, it can be performed by |
|
50 // a function called through the BPLOG_INIT macro. Each main function of |
|
51 // an executable program in the Breakpad processor library calls |
|
52 // BPLOG_INIT(&argc, &argv); before any logging can be performed; define |
|
53 // BPLOG_INIT appropriately if initialization is required. |
|
54 // |
|
55 // Author: Mark Mentovai |
|
56 |
|
57 #ifndef PROCESSOR_LOGGING_H__ |
|
58 #define PROCESSOR_LOGGING_H__ |
|
59 |
|
60 #include <iostream> |
|
61 #include <sstream> |
|
62 #include <string> |
|
63 |
|
64 #include "common/using_std_string.h" |
|
65 #include "google_breakpad/common/breakpad_types.h" |
|
66 |
|
67 #ifdef BP_LOGGING_INCLUDE |
|
68 #include BP_LOGGING_INCLUDE |
|
69 #endif // BP_LOGGING_INCLUDE |
|
70 |
|
71 #ifndef THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_ |
|
72 namespace base_logging { |
|
73 |
|
74 // The open-source copy of logging.h has diverged from Google's internal copy |
|
75 // (temporarily, at least). To support the transition to structured logging |
|
76 // a definition for base_logging::LogMessage is needed, which is a ostream- |
|
77 // like object for streaming arguments to construct a log message. |
|
78 typedef std::ostream LogMessage; |
|
79 |
|
80 } // namespace base_logging |
|
81 #endif // THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_ |
|
82 |
|
83 namespace google_breakpad { |
|
84 |
|
85 // These are defined in Microsoft headers. |
|
86 #ifdef SEVERITY_ERROR |
|
87 #undef SEVERITY_ERROR |
|
88 #endif |
|
89 |
|
90 #ifdef ERROR |
|
91 #undef ERROR |
|
92 #endif |
|
93 |
|
94 class LogStream { |
|
95 public: |
|
96 enum Severity { |
|
97 SEVERITY_INFO, |
|
98 SEVERITY_ERROR |
|
99 }; |
|
100 |
|
101 // Begin logging a message to the stream identified by |stream|, at the |
|
102 // indicated severity. The file and line parameters should be set so as to |
|
103 // identify the line of source code that is producing a message. |
|
104 LogStream(std::ostream &stream, Severity severity, |
|
105 const char *file, int line); |
|
106 |
|
107 // Finish logging by printing a newline and flushing the output stream. |
|
108 ~LogStream(); |
|
109 |
|
110 // Accumulate text in the str_. It will be emitted to stream_ when |
|
111 // the object is destructed. |
|
112 template<typename T> std::ostream& operator<<(const T &t) { |
|
113 return str_ << t; |
|
114 } |
|
115 |
|
116 private: |
|
117 std::ostream &stream_; |
|
118 std::ostringstream str_; |
|
119 |
|
120 // Disallow copy constructor and assignment operator |
|
121 explicit LogStream(const LogStream &that); |
|
122 void operator=(const LogStream &that); |
|
123 }; |
|
124 |
|
125 // This class is used to explicitly ignore values in the conditional logging |
|
126 // macros. This avoids compiler warnings like "value computed is not used" |
|
127 // and "statement has no effect". |
|
128 class LogMessageVoidify { |
|
129 public: |
|
130 LogMessageVoidify() {} |
|
131 |
|
132 // This has to be an operator with a precedence lower than << but higher |
|
133 // than ?: |
|
134 void operator&(base_logging::LogMessage &) {} |
|
135 }; |
|
136 |
|
137 // Returns number formatted as a hexadecimal string, such as "0x7b". |
|
138 string HexString(uint32_t number); |
|
139 string HexString(uint64_t number); |
|
140 string HexString(int number); |
|
141 |
|
142 // Returns the error code as set in the global errno variable, and sets |
|
143 // error_string, a required argument, to a string describing that error |
|
144 // code. |
|
145 int ErrnoString(string *error_string); |
|
146 |
|
147 } // namespace google_breakpad |
|
148 |
|
149 // Useful for doing exponential backoff of error reporting |
|
150 bool is_power_of_2(uint64_t); |
|
151 |
|
152 #ifndef BPLOG_INIT |
|
153 #define BPLOG_INIT(pargc, pargv) |
|
154 #endif // BPLOG_INIT |
|
155 |
|
156 #ifndef BPLOG |
|
157 #define BPLOG(severity) BPLOG_ ## severity |
|
158 #endif // BPLOG |
|
159 |
|
160 #ifndef BPLOG_INFO |
|
161 #ifndef BPLOG_INFO_STREAM |
|
162 #define BPLOG_INFO_STREAM std::clog |
|
163 #endif // BPLOG_INFO_STREAM |
|
164 #define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \ |
|
165 google_breakpad::LogStream::SEVERITY_INFO, \ |
|
166 __FILE__, __LINE__) |
|
167 #endif // BPLOG_INFO |
|
168 |
|
169 #ifndef BPLOG_ERROR |
|
170 #ifndef BPLOG_ERROR_STREAM |
|
171 #define BPLOG_ERROR_STREAM std::cerr |
|
172 #endif // BPLOG_ERROR_STREAM |
|
173 #define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \ |
|
174 google_breakpad::LogStream::SEVERITY_ERROR, \ |
|
175 __FILE__, __LINE__) |
|
176 #endif // BPLOG_ERROR |
|
177 |
|
178 #define BPLOG_IF(severity, condition) \ |
|
179 !(condition) ? (void) 0 : \ |
|
180 google_breakpad::LogMessageVoidify() & BPLOG(severity) |
|
181 |
|
182 #endif // PROCESSOR_LOGGING_H__ |