michael@0: // Copyright (c) 2007, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // logging.h: Breakpad logging michael@0: // michael@0: // Breakpad itself uses Breakpad logging with statements of the form: michael@0: // BPLOG(severity) << "message"; michael@0: // severity may be INFO, ERROR, or other values defined in this file. michael@0: // michael@0: // BPLOG is an overridable macro so that users can customize Breakpad's michael@0: // logging. Left at the default, logging messages are sent to stderr along michael@0: // with a timestamp and the source code location that produced a message. michael@0: // The streams may be changed by redefining BPLOG_*_STREAM, the logging michael@0: // behavior may be changed by redefining BPLOG_*, and the entire logging michael@0: // system may be overridden by redefining BPLOG(severity). These michael@0: // redefinitions may be passed to the preprocessor as a command-line flag michael@0: // (-D). michael@0: // michael@0: // If an additional header is required to override Breakpad logging, it can michael@0: // be specified by the BP_LOGGING_INCLUDE macro. If defined, this header michael@0: // will #include the header specified by that macro. michael@0: // michael@0: // If any initialization is needed before logging, it can be performed by michael@0: // a function called through the BPLOG_INIT macro. Each main function of michael@0: // an executable program in the Breakpad processor library calls michael@0: // BPLOG_INIT(&argc, &argv); before any logging can be performed; define michael@0: // BPLOG_INIT appropriately if initialization is required. michael@0: // michael@0: // Author: Mark Mentovai michael@0: michael@0: #ifndef PROCESSOR_LOGGING_H__ michael@0: #define PROCESSOR_LOGGING_H__ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "common/using_std_string.h" michael@0: #include "google_breakpad/common/breakpad_types.h" michael@0: michael@0: #ifdef BP_LOGGING_INCLUDE michael@0: #include BP_LOGGING_INCLUDE michael@0: #endif // BP_LOGGING_INCLUDE michael@0: michael@0: #ifndef THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_ michael@0: namespace base_logging { michael@0: michael@0: // The open-source copy of logging.h has diverged from Google's internal copy michael@0: // (temporarily, at least). To support the transition to structured logging michael@0: // a definition for base_logging::LogMessage is needed, which is a ostream- michael@0: // like object for streaming arguments to construct a log message. michael@0: typedef std::ostream LogMessage; michael@0: michael@0: } // namespace base_logging michael@0: #endif // THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_ michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // These are defined in Microsoft headers. michael@0: #ifdef SEVERITY_ERROR michael@0: #undef SEVERITY_ERROR michael@0: #endif michael@0: michael@0: #ifdef ERROR michael@0: #undef ERROR michael@0: #endif michael@0: michael@0: class LogStream { michael@0: public: michael@0: enum Severity { michael@0: SEVERITY_INFO, michael@0: SEVERITY_ERROR michael@0: }; michael@0: michael@0: // Begin logging a message to the stream identified by |stream|, at the michael@0: // indicated severity. The file and line parameters should be set so as to michael@0: // identify the line of source code that is producing a message. michael@0: LogStream(std::ostream &stream, Severity severity, michael@0: const char *file, int line); michael@0: michael@0: // Finish logging by printing a newline and flushing the output stream. michael@0: ~LogStream(); michael@0: michael@0: // Accumulate text in the str_. It will be emitted to stream_ when michael@0: // the object is destructed. michael@0: template std::ostream& operator<<(const T &t) { michael@0: return str_ << t; michael@0: } michael@0: michael@0: private: michael@0: std::ostream &stream_; michael@0: std::ostringstream str_; michael@0: michael@0: // Disallow copy constructor and assignment operator michael@0: explicit LogStream(const LogStream &that); michael@0: void operator=(const LogStream &that); michael@0: }; michael@0: michael@0: // This class is used to explicitly ignore values in the conditional logging michael@0: // macros. This avoids compiler warnings like "value computed is not used" michael@0: // and "statement has no effect". michael@0: class LogMessageVoidify { michael@0: public: michael@0: LogMessageVoidify() {} michael@0: michael@0: // This has to be an operator with a precedence lower than << but higher michael@0: // than ?: michael@0: void operator&(base_logging::LogMessage &) {} michael@0: }; michael@0: michael@0: // Returns number formatted as a hexadecimal string, such as "0x7b". michael@0: string HexString(uint32_t number); michael@0: string HexString(uint64_t number); michael@0: string HexString(int number); michael@0: michael@0: // Returns the error code as set in the global errno variable, and sets michael@0: // error_string, a required argument, to a string describing that error michael@0: // code. michael@0: int ErrnoString(string *error_string); michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: // Useful for doing exponential backoff of error reporting michael@0: bool is_power_of_2(uint64_t); michael@0: michael@0: #ifndef BPLOG_INIT michael@0: #define BPLOG_INIT(pargc, pargv) michael@0: #endif // BPLOG_INIT michael@0: michael@0: #ifndef BPLOG michael@0: #define BPLOG(severity) BPLOG_ ## severity michael@0: #endif // BPLOG michael@0: michael@0: #ifndef BPLOG_INFO michael@0: #ifndef BPLOG_INFO_STREAM michael@0: #define BPLOG_INFO_STREAM std::clog michael@0: #endif // BPLOG_INFO_STREAM michael@0: #define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \ michael@0: google_breakpad::LogStream::SEVERITY_INFO, \ michael@0: __FILE__, __LINE__) michael@0: #endif // BPLOG_INFO michael@0: michael@0: #ifndef BPLOG_ERROR michael@0: #ifndef BPLOG_ERROR_STREAM michael@0: #define BPLOG_ERROR_STREAM std::cerr michael@0: #endif // BPLOG_ERROR_STREAM michael@0: #define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \ michael@0: google_breakpad::LogStream::SEVERITY_ERROR, \ michael@0: __FILE__, __LINE__) michael@0: #endif // BPLOG_ERROR michael@0: michael@0: #define BPLOG_IF(severity, condition) \ michael@0: !(condition) ? (void) 0 : \ michael@0: google_breakpad::LogMessageVoidify() & BPLOG(severity) michael@0: michael@0: #endif // PROCESSOR_LOGGING_H__