• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
62 
63 #include "common/using_std_string.h"
64 #include "google_breakpad/common/breakpad_types.h"
65 
66 #ifdef BP_LOGGING_INCLUDE
67 #include BP_LOGGING_INCLUDE
68 #endif  // BP_LOGGING_INCLUDE
69 
70 #ifndef THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
71 namespace base_logging {
72 
73 // The open-source copy of logging.h has diverged from Google's internal copy
74 // (temporarily, at least).  To support the transition to structured logging
75 // a definition for base_logging::LogMessage is needed, which is a ostream-
76 // like object for streaming arguments to construct a log message.
77 typedef std::ostream LogMessage;
78 
79 }  // namespace base_logging
80 #endif  // THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
81 
82 namespace google_breakpad {
83 
84 // These are defined in Microsoft headers.
85 #ifdef SEVERITY_ERROR
86 #undef SEVERITY_ERROR
87 #endif
88 
89 #ifdef ERROR
90 #undef ERROR
91 #endif
92 
93 class LogStream {
94  public:
95   enum Severity {
96     SEVERITY_INFO,
97     SEVERITY_ERROR,
98     SEVERITY_CRITICAL
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   template<typename T> std::ostream& operator<<(const T &t) {
111     return stream_ << t;
112   }
113 
114  private:
115   std::ostream &stream_;
116 
117   // Disallow copy constructor and assignment operator
118   explicit LogStream(const LogStream &that);
119   void operator=(const LogStream &that);
120 };
121 
122 // This class is used to explicitly ignore values in the conditional logging
123 // macros.  This avoids compiler warnings like "value computed is not used"
124 // and "statement has no effect".
125 class LogMessageVoidify {
126  public:
LogMessageVoidify()127   LogMessageVoidify() {}
128 
129   // This has to be an operator with a precedence lower than << but higher
130   // than ?:
131   void operator&(base_logging::LogMessage &) {}
132 };
133 
134 // Returns number formatted as a hexadecimal string, such as "0x7b".
135 string HexString(uint32_t number);
136 string HexString(uint64_t number);
137 string HexString(int number);
138 
139 // Returns the error code as set in the global errno variable, and sets
140 // error_string, a required argument, to a string describing that error
141 // code.
142 int ErrnoString(string *error_string);
143 
144 }  // namespace google_breakpad
145 
146 #ifndef BPLOG_INIT
147 #define BPLOG_INIT(pargc, pargv)
148 #endif  // BPLOG_INIT
149 
150 #ifndef BPLOG_LAZY_STREAM
151 #define BPLOG_LAZY_STREAM(stream, condition) \
152     !(condition) ? (void) 0 : \
153                    google_breakpad::LogMessageVoidify() & (BPLOG_ ## stream)
154 #endif
155 
156 #ifndef BPLOG_MINIMUM_SEVERITY
157 #define BPLOG_MINIMUM_SEVERITY SEVERITY_INFO
158 #endif
159 
160 #define BPLOG_LOG_IS_ON(severity) \
161     ((google_breakpad::LogStream::SEVERITY_ ## severity) >= \
162      (google_breakpad::LogStream::BPLOG_MINIMUM_SEVERITY))
163 
164 #ifndef BPLOG
165 #define BPLOG(severity) BPLOG_LAZY_STREAM(severity, BPLOG_LOG_IS_ON(severity))
166 #endif  // BPLOG
167 
168 #ifndef BPLOG_INFO
169 #ifndef BPLOG_INFO_STREAM
170 #define BPLOG_INFO_STREAM std::clog
171 #endif  // BPLOG_INFO_STREAM
172 #define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
173                        google_breakpad::LogStream::SEVERITY_INFO, \
174                        __FILE__, __LINE__)
175 #endif  // BPLOG_INFO
176 
177 #ifndef BPLOG_ERROR
178 #ifndef BPLOG_ERROR_STREAM
179 #define BPLOG_ERROR_STREAM std::cerr
180 #endif  // BPLOG_ERROR_STREAM
181 #define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
182                         google_breakpad::LogStream::SEVERITY_ERROR, \
183                         __FILE__, __LINE__)
184 #endif  // BPLOG_ERROR
185 
186 #ifndef BPLOG_CRITICAL
187 #ifndef BPLOG_CRITICAL_STREAM
188 #define BPLOG_CRITICAL_STREAM std::cerr
189 #endif  // BPLOG_CRITICAL_STREAM
190 #define BPLOG_CRITICAL google_breakpad::LogStream(BPLOG_CRITICAL_STREAM, \
191                         google_breakpad::LogStream::SEVERITY_CRITICAL, \
192                         __FILE__, __LINE__)
193 #endif  // BPLOG_CRITICAL
194 
195 #ifndef BPLOG_IF
196 #define BPLOG_IF(severity, condition) \
197     BPLOG_LAZY_STREAM(severity, ((condition) && BPLOG_LOG_IS_ON(severity)))
198 #endif  // BPLOG_IF
199 
200 #endif  // PROCESSOR_LOGGING_H__
201