blob: 4925e67f272801d86b28742123ff6b3596fb934c [file] [log] [blame]
mmentovaiaf3c43f2007-05-17 18:34:37 +00001// 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//
mmentovai32b802d2007-05-25 18:04:17 +000049// 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//
mmentovaiaf3c43f2007-05-17 18:34:37 +000055// Author: Mark Mentovai
56
57#ifndef PROCESSOR_LOGGING_H__
58#define PROCESSOR_LOGGING_H__
59
60#include <iostream>
61#include <string>
62
ivan.penkov@gmail.com6de969a2012-06-28 22:46:01 +000063#include "common/using_std_string.h"
mmentovaiaf3c43f2007-05-17 18:34:37 +000064#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
ted.mielczarekc77fc8a2011-01-11 20:27:29 +000072// 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
mmentovaiaf3c43f2007-05-17 18:34:37 +000081class LogStream {
82 public:
83 enum Severity {
84 SEVERITY_INFO,
mmentovai2e0e2232007-05-31 19:44:52 +000085 SEVERITY_ERROR
mmentovaiaf3c43f2007-05-17 18:34:37 +000086 };
87
88 // Begin logging a message to the stream identified by |stream|, at the
89 // indicated severity. The file and line parameters should be set so as to
90 // identify the line of source code that is producing a message.
91 LogStream(std::ostream &stream, Severity severity,
92 const char *file, int line);
93
94 // Finish logging by printing a newline and flushing the output stream.
95 ~LogStream();
96
97 template<typename T> std::ostream& operator<<(const T &t) {
98 return stream_ << t;
99 }
100
101 private:
102 std::ostream &stream_;
103
104 // Disallow copy constructor and assignment operator
105 explicit LogStream(const LogStream &that);
106 void operator=(const LogStream &that);
107};
108
109// This class is used to explicitly ignore values in the conditional logging
110// macros. This avoids compiler warnings like "value computed is not used"
111// and "statement has no effect".
112class LogMessageVoidify {
113 public:
114 LogMessageVoidify() {}
115
116 // This has to be an operator with a precedence lower than << but higher
117 // than ?:
118 void operator&(std::ostream &) {}
119};
120
121// Returns number formatted as a hexadecimal string, such as "0x7b".
ted.mielczarek@gmail.comaeffe102013-03-06 14:04:42 +0000122string HexString(uint32_t number);
123string HexString(uint64_t number);
ivan.penkov@gmail.com6de969a2012-06-28 22:46:01 +0000124string HexString(int number);
mmentovaiaf3c43f2007-05-17 18:34:37 +0000125
126// Returns the error code as set in the global errno variable, and sets
127// error_string, a required argument, to a string describing that error
128// code.
ivan.penkov@gmail.com6de969a2012-06-28 22:46:01 +0000129int ErrnoString(string *error_string);
mmentovaiaf3c43f2007-05-17 18:34:37 +0000130
131} // namespace google_breakpad
132
mmentovai32b802d2007-05-25 18:04:17 +0000133#ifndef BPLOG_INIT
134#define BPLOG_INIT(pargc, pargv)
135#endif // BPLOG_INIT
136
mmentovaiaf3c43f2007-05-17 18:34:37 +0000137#ifndef BPLOG
138#define BPLOG(severity) BPLOG_ ## severity
139#endif // BPLOG
140
141#ifndef BPLOG_INFO
142#ifndef BPLOG_INFO_STREAM
143#define BPLOG_INFO_STREAM std::clog
144#endif // BPLOG_INFO_STREAM
mmentovai32b802d2007-05-25 18:04:17 +0000145#define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
146 google_breakpad::LogStream::SEVERITY_INFO, \
147 __FILE__, __LINE__)
mmentovaiaf3c43f2007-05-17 18:34:37 +0000148#endif // BPLOG_INFO
149
150#ifndef BPLOG_ERROR
151#ifndef BPLOG_ERROR_STREAM
152#define BPLOG_ERROR_STREAM std::cerr
153#endif // BPLOG_ERROR_STREAM
mmentovai32b802d2007-05-25 18:04:17 +0000154#define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
155 google_breakpad::LogStream::SEVERITY_ERROR, \
156 __FILE__, __LINE__)
mmentovaiaf3c43f2007-05-17 18:34:37 +0000157#endif // BPLOG_ERROR
158
159#define BPLOG_IF(severity, condition) \
mmentovai32b802d2007-05-25 18:04:17 +0000160 !(condition) ? (void) 0 : \
161 google_breakpad::LogMessageVoidify() & BPLOG(severity)
mmentovaiaf3c43f2007-05-17 18:34:37 +0000162
163#endif // PROCESSOR_LOGGING_H__