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