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
70namespace google_breakpad {
71
72// These are defined in Microsoft headers.
73#ifdef SEVERITY_ERROR
74#undef SEVERITY_ERROR
75#endif
76
77#ifdef ERROR
78#undef ERROR
79#endif
80
81class LogStream {
82 public:
83 enum Severity {
84 SEVERITY_INFO,
85 SEVERITY_ERROR,
86 SEVERITY_CRITICAL
87 };
88
89 // Begin logging a message to the stream identified by |stream|, at the
90 // indicated severity. The file and line parameters should be set so as to
91 // identify the line of source code that is producing a message.
92 LogStream(std::ostream& stream, Severity severity,
93 const char* file, int line);
94
95 // Finish logging by printing a newline and flushing the output stream.
96 ~LogStream();
97
98 template<typename T> std::ostream& operator<<(const T& t) {
99 return stream_ << t;
100 }
101
102 private:
103 std::ostream& stream_;
104
105 // Disallow copy constructor and assignment operator
106 explicit LogStream(const LogStream& that);
107 void operator=(const LogStream& that);
108};
109
110// This class is used to explicitly ignore values in the conditional logging
111// macros. This avoids compiler warnings like "value computed is not used"
112// and "statement has no effect".
113class LogMessageVoidify {
114 public:
115 LogMessageVoidify() {}
116
117 // This has to be an operator with a precedence lower than << but higher
118 // than ?:
119 void operator&(std::ostream&) {}
120};
121
122// Returns number formatted as a hexadecimal string, such as "0x7b".
123string HexString(uint32_t number);
124string HexString(uint64_t number);
125string HexString(int number);
126
127// Returns the error code as set in the global errno variable, and sets
128// error_string, a required argument, to a string describing that error
129// code.
130int ErrnoString(string* error_string);
131
132} // namespace google_breakpad
133
134#ifndef BPLOG_INIT
135#define BPLOG_INIT(pargc, pargv)
136#endif // BPLOG_INIT
137
138#ifndef BPLOG_LAZY_STREAM
139#define BPLOG_LAZY_STREAM(stream, condition) \
140 !(condition) ? (void) 0 : \
141 google_breakpad::LogMessageVoidify() & (BPLOG_ ## stream)
142#endif
143
144#ifndef BPLOG_MINIMUM_SEVERITY
145#define BPLOG_MINIMUM_SEVERITY SEVERITY_INFO
146#endif
147
148#define BPLOG_LOG_IS_ON(severity) \
149 ((google_breakpad::LogStream::SEVERITY_ ## severity) >= \
150 (google_breakpad::LogStream::BPLOG_MINIMUM_SEVERITY))
151
152#ifndef BPLOG
153#define BPLOG(severity) BPLOG_LAZY_STREAM(severity, BPLOG_LOG_IS_ON(severity))
154#endif // BPLOG
155
156#ifndef BPLOG_INFO
157#ifndef BPLOG_INFO_STREAM
158#define BPLOG_INFO_STREAM std::clog
159#endif // BPLOG_INFO_STREAM
160#define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
161 google_breakpad::LogStream::SEVERITY_INFO, \
162 __FILE__, __LINE__)
163#endif // BPLOG_INFO
164
165#ifndef BPLOG_ERROR
166#ifndef BPLOG_ERROR_STREAM
167#define BPLOG_ERROR_STREAM std::cerr
168#endif // BPLOG_ERROR_STREAM
169#define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
170 google_breakpad::LogStream::SEVERITY_ERROR, \
171 __FILE__, __LINE__)
172#endif // BPLOG_ERROR
173
174#ifndef BPLOG_CRITICAL
175#ifndef BPLOG_CRITICAL_STREAM
176#define BPLOG_CRITICAL_STREAM std::cerr
177#endif // BPLOG_CRITICAL_STREAM
178#define BPLOG_CRITICAL google_breakpad::LogStream(BPLOG_CRITICAL_STREAM, \
179 google_breakpad::LogStream::SEVERITY_CRITICAL, \
180 __FILE__, __LINE__)
181#endif // BPLOG_CRITICAL
182
183#ifndef BPLOG_IF
184#define BPLOG_IF(severity, condition) \
185 BPLOG_LAZY_STREAM(severity, ((condition) && BPLOG_LOG_IS_ON(severity)))
186#endif // BPLOG_IF
187
188#endif // PROCESSOR_LOGGING_H__
189