toolkit/crashreporter/google-breakpad/src/common/logging.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2007, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 // logging.h: Breakpad logging
michael@0 31 //
michael@0 32 // Breakpad itself uses Breakpad logging with statements of the form:
michael@0 33 // BPLOG(severity) << "message";
michael@0 34 // severity may be INFO, ERROR, or other values defined in this file.
michael@0 35 //
michael@0 36 // BPLOG is an overridable macro so that users can customize Breakpad's
michael@0 37 // logging. Left at the default, logging messages are sent to stderr along
michael@0 38 // with a timestamp and the source code location that produced a message.
michael@0 39 // The streams may be changed by redefining BPLOG_*_STREAM, the logging
michael@0 40 // behavior may be changed by redefining BPLOG_*, and the entire logging
michael@0 41 // system may be overridden by redefining BPLOG(severity). These
michael@0 42 // redefinitions may be passed to the preprocessor as a command-line flag
michael@0 43 // (-D).
michael@0 44 //
michael@0 45 // If an additional header is required to override Breakpad logging, it can
michael@0 46 // be specified by the BP_LOGGING_INCLUDE macro. If defined, this header
michael@0 47 // will #include the header specified by that macro.
michael@0 48 //
michael@0 49 // If any initialization is needed before logging, it can be performed by
michael@0 50 // a function called through the BPLOG_INIT macro. Each main function of
michael@0 51 // an executable program in the Breakpad processor library calls
michael@0 52 // BPLOG_INIT(&argc, &argv); before any logging can be performed; define
michael@0 53 // BPLOG_INIT appropriately if initialization is required.
michael@0 54 //
michael@0 55 // Author: Mark Mentovai
michael@0 56
michael@0 57 #ifndef PROCESSOR_LOGGING_H__
michael@0 58 #define PROCESSOR_LOGGING_H__
michael@0 59
michael@0 60 #include <iostream>
michael@0 61 #include <sstream>
michael@0 62 #include <string>
michael@0 63
michael@0 64 #include "common/using_std_string.h"
michael@0 65 #include "google_breakpad/common/breakpad_types.h"
michael@0 66
michael@0 67 #ifdef BP_LOGGING_INCLUDE
michael@0 68 #include BP_LOGGING_INCLUDE
michael@0 69 #endif // BP_LOGGING_INCLUDE
michael@0 70
michael@0 71 #ifndef THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
michael@0 72 namespace base_logging {
michael@0 73
michael@0 74 // The open-source copy of logging.h has diverged from Google's internal copy
michael@0 75 // (temporarily, at least). To support the transition to structured logging
michael@0 76 // a definition for base_logging::LogMessage is needed, which is a ostream-
michael@0 77 // like object for streaming arguments to construct a log message.
michael@0 78 typedef std::ostream LogMessage;
michael@0 79
michael@0 80 } // namespace base_logging
michael@0 81 #endif // THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
michael@0 82
michael@0 83 namespace google_breakpad {
michael@0 84
michael@0 85 // These are defined in Microsoft headers.
michael@0 86 #ifdef SEVERITY_ERROR
michael@0 87 #undef SEVERITY_ERROR
michael@0 88 #endif
michael@0 89
michael@0 90 #ifdef ERROR
michael@0 91 #undef ERROR
michael@0 92 #endif
michael@0 93
michael@0 94 class LogStream {
michael@0 95 public:
michael@0 96 enum Severity {
michael@0 97 SEVERITY_INFO,
michael@0 98 SEVERITY_ERROR
michael@0 99 };
michael@0 100
michael@0 101 // Begin logging a message to the stream identified by |stream|, at the
michael@0 102 // indicated severity. The file and line parameters should be set so as to
michael@0 103 // identify the line of source code that is producing a message.
michael@0 104 LogStream(std::ostream &stream, Severity severity,
michael@0 105 const char *file, int line);
michael@0 106
michael@0 107 // Finish logging by printing a newline and flushing the output stream.
michael@0 108 ~LogStream();
michael@0 109
michael@0 110 // Accumulate text in the str_. It will be emitted to stream_ when
michael@0 111 // the object is destructed.
michael@0 112 template<typename T> std::ostream& operator<<(const T &t) {
michael@0 113 return str_ << t;
michael@0 114 }
michael@0 115
michael@0 116 private:
michael@0 117 std::ostream &stream_;
michael@0 118 std::ostringstream str_;
michael@0 119
michael@0 120 // Disallow copy constructor and assignment operator
michael@0 121 explicit LogStream(const LogStream &that);
michael@0 122 void operator=(const LogStream &that);
michael@0 123 };
michael@0 124
michael@0 125 // This class is used to explicitly ignore values in the conditional logging
michael@0 126 // macros. This avoids compiler warnings like "value computed is not used"
michael@0 127 // and "statement has no effect".
michael@0 128 class LogMessageVoidify {
michael@0 129 public:
michael@0 130 LogMessageVoidify() {}
michael@0 131
michael@0 132 // This has to be an operator with a precedence lower than << but higher
michael@0 133 // than ?:
michael@0 134 void operator&(base_logging::LogMessage &) {}
michael@0 135 };
michael@0 136
michael@0 137 // Returns number formatted as a hexadecimal string, such as "0x7b".
michael@0 138 string HexString(uint32_t number);
michael@0 139 string HexString(uint64_t number);
michael@0 140 string HexString(int number);
michael@0 141
michael@0 142 // Returns the error code as set in the global errno variable, and sets
michael@0 143 // error_string, a required argument, to a string describing that error
michael@0 144 // code.
michael@0 145 int ErrnoString(string *error_string);
michael@0 146
michael@0 147 } // namespace google_breakpad
michael@0 148
michael@0 149 // Useful for doing exponential backoff of error reporting
michael@0 150 bool is_power_of_2(uint64_t);
michael@0 151
michael@0 152 #ifndef BPLOG_INIT
michael@0 153 #define BPLOG_INIT(pargc, pargv)
michael@0 154 #endif // BPLOG_INIT
michael@0 155
michael@0 156 #ifndef BPLOG
michael@0 157 #define BPLOG(severity) BPLOG_ ## severity
michael@0 158 #endif // BPLOG
michael@0 159
michael@0 160 #ifndef BPLOG_INFO
michael@0 161 #ifndef BPLOG_INFO_STREAM
michael@0 162 #define BPLOG_INFO_STREAM std::clog
michael@0 163 #endif // BPLOG_INFO_STREAM
michael@0 164 #define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
michael@0 165 google_breakpad::LogStream::SEVERITY_INFO, \
michael@0 166 __FILE__, __LINE__)
michael@0 167 #endif // BPLOG_INFO
michael@0 168
michael@0 169 #ifndef BPLOG_ERROR
michael@0 170 #ifndef BPLOG_ERROR_STREAM
michael@0 171 #define BPLOG_ERROR_STREAM std::cerr
michael@0 172 #endif // BPLOG_ERROR_STREAM
michael@0 173 #define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
michael@0 174 google_breakpad::LogStream::SEVERITY_ERROR, \
michael@0 175 __FILE__, __LINE__)
michael@0 176 #endif // BPLOG_ERROR
michael@0 177
michael@0 178 #define BPLOG_IF(severity, condition) \
michael@0 179 !(condition) ? (void) 0 : \
michael@0 180 google_breakpad::LogMessageVoidify() & BPLOG(severity)
michael@0 181
michael@0 182 #endif // PROCESSOR_LOGGING_H__

mercurial