/ src / processor / logging.h
logging.h
  1  // Copyright 2007 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // logging.h: Breakpad logging
 30  //
 31  // Breakpad itself uses Breakpad logging with statements of the form:
 32  //   BPLOG(severity) << "message";
 33  // severity may be INFO, ERROR, or other values defined in this file.
 34  //
 35  // BPLOG is an overridable macro so that users can customize Breakpad's
 36  // logging.  Left at the default, logging messages are sent to stderr along
 37  // with a timestamp and the source code location that produced a message.
 38  // The streams may be changed by redefining BPLOG_*_STREAM, the logging
 39  // behavior may be changed by redefining BPLOG_*, and the entire logging
 40  // system may be overridden by redefining BPLOG(severity).  These
 41  // redefinitions may be passed to the preprocessor as a command-line flag
 42  // (-D).
 43  //
 44  // If an additional header is required to override Breakpad logging, it can
 45  // be specified by the BP_LOGGING_INCLUDE macro.  If defined, this header
 46  // will #include the header specified by that macro.
 47  //
 48  // If any initialization is needed before logging, it can be performed by
 49  // a function called through the BPLOG_INIT macro.  Each main function of
 50  // an executable program in the Breakpad processor library calls
 51  // BPLOG_INIT(&argc, &argv); before any logging can be performed; define
 52  // BPLOG_INIT appropriately if initialization is required.
 53  //
 54  // Author: Mark Mentovai
 55  
 56  #ifndef PROCESSOR_LOGGING_H__
 57  #define PROCESSOR_LOGGING_H__
 58  
 59  #include <iostream>
 60  #include <string>
 61  
 62  #include "common/using_std_string.h"
 63  #include "google_breakpad/common/breakpad_types.h"
 64  
 65  #ifdef BP_LOGGING_INCLUDE
 66  #include BP_LOGGING_INCLUDE
 67  #endif  // BP_LOGGING_INCLUDE
 68  
 69  namespace google_breakpad {
 70  
 71  // These are defined in Microsoft headers.
 72  #ifdef SEVERITY_ERROR
 73  #undef SEVERITY_ERROR
 74  #endif
 75  
 76  #ifdef ERROR
 77  #undef ERROR
 78  #endif
 79  
 80  class LogStream {
 81   public:
 82    enum Severity {
 83      SEVERITY_INFO,
 84      SEVERITY_ERROR,
 85      SEVERITY_CRITICAL
 86    };
 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".
112  class 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".
122  string HexString(uint32_t number);
123  string HexString(uint64_t number);
124  string HexString(int number);
125  
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.
129  int ErrnoString(string* error_string);
130  
131  }  // namespace google_breakpad
132  
133  #ifndef BPLOG_INIT
134  #define BPLOG_INIT(pargc, pargv)
135  #endif  // BPLOG_INIT
136  
137  #ifndef BPLOG_LAZY_STREAM
138  #define BPLOG_LAZY_STREAM(stream, condition) \
139      !(condition) ? (void) 0 : \
140                     google_breakpad::LogMessageVoidify() & (BPLOG_ ## stream)
141  #endif
142  
143  #ifndef BPLOG_MINIMUM_SEVERITY
144  #define BPLOG_MINIMUM_SEVERITY SEVERITY_INFO
145  #endif
146  
147  #define BPLOG_LOG_IS_ON(severity) \
148      ((google_breakpad::LogStream::SEVERITY_ ## severity) >= \
149       (google_breakpad::LogStream::BPLOG_MINIMUM_SEVERITY))
150  
151  #ifndef BPLOG
152  #define BPLOG(severity) BPLOG_LAZY_STREAM(severity, BPLOG_LOG_IS_ON(severity))
153  #endif  // BPLOG
154  
155  #ifndef BPLOG_INFO
156  #ifndef BPLOG_INFO_STREAM
157  #define BPLOG_INFO_STREAM std::clog
158  #endif  // BPLOG_INFO_STREAM
159  #define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
160                         google_breakpad::LogStream::SEVERITY_INFO, \
161                         __FILE__, __LINE__)
162  #endif  // BPLOG_INFO
163  
164  #ifndef BPLOG_ERROR
165  #ifndef BPLOG_ERROR_STREAM
166  #define BPLOG_ERROR_STREAM std::cerr
167  #endif  // BPLOG_ERROR_STREAM
168  #define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
169                          google_breakpad::LogStream::SEVERITY_ERROR, \
170                          __FILE__, __LINE__)
171  #endif  // BPLOG_ERROR
172  
173  #ifndef BPLOG_CRITICAL
174  #ifndef BPLOG_CRITICAL_STREAM
175  #define BPLOG_CRITICAL_STREAM std::cerr
176  #endif  // BPLOG_CRITICAL_STREAM
177  #define BPLOG_CRITICAL google_breakpad::LogStream(BPLOG_CRITICAL_STREAM, \
178                          google_breakpad::LogStream::SEVERITY_CRITICAL, \
179                          __FILE__, __LINE__)
180  #endif  // BPLOG_CRITICAL
181  
182  #ifndef BPLOG_IF
183  #define BPLOG_IF(severity, condition) \
184      BPLOG_LAZY_STREAM(severity, ((condition) && BPLOG_LOG_IS_ON(severity)))
185  #endif  // BPLOG_IF
186  
187  #endif  // PROCESSOR_LOGGING_H__