Planeshift

logging.h

Go to the documentation of this file.
00001 // Copyright (c) 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // logging.h: Breakpad logging
00031 //
00032 // Breakpad itself uses Breakpad logging with statements of the form:
00033 //   BPLOG(severity) << "message";
00034 // severity may be INFO, ERROR, or other values defined in this file.
00035 //
00036 // BPLOG is an overridable macro so that users can customize Breakpad's
00037 // logging.  Left at the default, logging messages are sent to stderr along
00038 // with a timestamp and the source code location that produced a message.
00039 // The streams may be changed by redefining BPLOG_*_STREAM, the logging
00040 // behavior may be changed by redefining BPLOG_*, and the entire logging
00041 // system may be overridden by redefining BPLOG(severity).  These
00042 // redefinitions may be passed to the preprocessor as a command-line flag
00043 // (-D).
00044 //
00045 // If an additional header is required to override Breakpad logging, it can
00046 // be specified by the BP_LOGGING_INCLUDE macro.  If defined, this header
00047 // will #include the header specified by that macro.
00048 //
00049 // If any initialization is needed before logging, it can be performed by
00050 // a function called through the BPLOG_INIT macro.  Each main function of
00051 // an executable program in the Breakpad processor library calls
00052 // BPLOG_INIT(&argc, &argv); before any logging can be performed; define
00053 // BPLOG_INIT appropriately if initialization is required.
00054 //
00055 // Author: Mark Mentovai
00056 
00057 #ifndef PROCESSOR_LOGGING_H__
00058 #define PROCESSOR_LOGGING_H__
00059 
00060 #include <iostream>
00061 #include <string>
00062 
00063 #include "common/using_std_string.h"
00064 #include "google_breakpad/common/breakpad_types.h"
00065 
00066 #ifdef BP_LOGGING_INCLUDE
00067 #include BP_LOGGING_INCLUDE
00068 #endif  // BP_LOGGING_INCLUDE
00069 
00070 #ifndef THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
00071 namespace base_logging {
00072 
00073 // The open-source copy of logging.h has diverged from Google's internal copy
00074 // (temporarily, at least).  To support the transition to structured logging
00075 // a definition for base_logging::LogMessage is needed, which is a ostream-
00076 // like object for streaming arguments to construct a log message.
00077 typedef std::ostream LogMessage;
00078 
00079 }  // namespace base_logging
00080 #endif  // THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
00081 
00082 namespace google_breakpad {
00083 
00084 // These are defined in Microsoft headers.
00085 #ifdef SEVERITY_ERROR
00086 #undef SEVERITY_ERROR
00087 #endif
00088 
00089 #ifdef ERROR
00090 #undef ERROR
00091 #endif
00092 
00093 class LogStream {
00094  public:
00095   enum Severity {
00096     SEVERITY_INFO,
00097     SEVERITY_ERROR
00098   };
00099 
00100   // Begin logging a message to the stream identified by |stream|, at the
00101   // indicated severity.  The file and line parameters should be set so as to
00102   // identify the line of source code that is producing a message.
00103   LogStream(std::ostream &stream, Severity severity,
00104             const char *file, int line);
00105 
00106   // Finish logging by printing a newline and flushing the output stream.
00107   ~LogStream();
00108 
00109   template<typename T> std::ostream& operator<<(const T &t) {
00110     return stream_ << t;
00111   }
00112 
00113  private:
00114   std::ostream &stream_;
00115 
00116   // Disallow copy constructor and assignment operator
00117   explicit LogStream(const LogStream &that);
00118   void operator=(const LogStream &that);
00119 };
00120 
00121 // This class is used to explicitly ignore values in the conditional logging
00122 // macros.  This avoids compiler warnings like "value computed is not used"
00123 // and "statement has no effect".
00124 class LogMessageVoidify {
00125  public:
00126   LogMessageVoidify() {}
00127 
00128   // This has to be an operator with a precedence lower than << but higher
00129   // than ?:
00130   void operator&(base_logging::LogMessage &) {}
00131 };
00132 
00133 // Returns number formatted as a hexadecimal string, such as "0x7b".
00134 string HexString(uint32_t number);
00135 string HexString(uint64_t number);
00136 string HexString(int number);
00137 
00138 // Returns the error code as set in the global errno variable, and sets
00139 // error_string, a required argument, to a string describing that error
00140 // code.
00141 int ErrnoString(string *error_string);
00142 
00143 }  // namespace google_breakpad
00144 
00145 #ifndef BPLOG_INIT
00146 #define BPLOG_INIT(pargc, pargv)
00147 #endif  // BPLOG_INIT
00148 
00149 #ifndef BPLOG
00150 #define BPLOG(severity) BPLOG_ ## severity
00151 #endif  // BPLOG
00152 
00153 #ifndef BPLOG_INFO
00154 #ifndef BPLOG_INFO_STREAM
00155 #define BPLOG_INFO_STREAM std::clog
00156 #endif  // BPLOG_INFO_STREAM
00157 #define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
00158                        google_breakpad::LogStream::SEVERITY_INFO, \
00159                        __FILE__, __LINE__)
00160 #endif  // BPLOG_INFO
00161 
00162 #ifndef BPLOG_ERROR
00163 #ifndef BPLOG_ERROR_STREAM
00164 #define BPLOG_ERROR_STREAM std::cerr
00165 #endif  // BPLOG_ERROR_STREAM
00166 #define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
00167                         google_breakpad::LogStream::SEVERITY_ERROR, \
00168                         __FILE__, __LINE__)
00169 #endif  // BPLOG_ERROR
00170 
00171 #define BPLOG_IF(severity, condition) \
00172     !(condition) ? (void) 0 : \
00173                    google_breakpad::LogMessageVoidify() & BPLOG(severity)
00174 
00175 #endif  // PROCESSOR_LOGGING_H__