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