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
634e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.com#include "common/using_std_string.h"
64af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#include "google_breakpad/common/breakpad_types.h"
65af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
66af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#ifdef BP_LOGGING_INCLUDE
67af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#include BP_LOGGING_INCLUDE
68af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#endif  // BP_LOGGING_INCLUDE
69af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
707deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org#ifndef THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
717deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.orgnamespace base_logging {
727deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org
737deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org// The open-source copy of logging.h has diverged from Google's internal copy
747deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org// (temporarily, at least).  To support the transition to structured logging
757deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org// a definition for base_logging::LogMessage is needed, which is a ostream-
767deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org// like object for streaming arguments to construct a log message.
777deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.orgtypedef std::ostream LogMessage;
787deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org
797deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org}  // namespace base_logging
807deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org#endif  // THIRD_PARTY_BREAKPAD_GOOGLE_GLUE_LOGGING_H_
817deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org
82af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovainamespace google_breakpad {
83af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
84c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek// These are defined in Microsoft headers.
85c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek#ifdef SEVERITY_ERROR
86c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek#undef SEVERITY_ERROR
87c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek#endif
88c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek
89c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek#ifdef ERROR
90c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek#undef ERROR
91c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek#endif
92c77fc8a32c8af421c7e0d5fe81eccfb62329dff2ted.mielczarek
93af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovaiclass LogStream {
94af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai public:
95af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  enum Severity {
96af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai    SEVERITY_INFO,
972e0e2234b9e9d7d82c4c3c20396bdf8f18007e6cmmentovai    SEVERITY_ERROR
98af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  };
99af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
100af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  // Begin logging a message to the stream identified by |stream|, at the
101af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  // indicated severity.  The file and line parameters should be set so as to
102af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  // identify the line of source code that is producing a message.
103af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  LogStream(std::ostream &stream, Severity severity,
104af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai            const char *file, int line);
105af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
106af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  // Finish logging by printing a newline and flushing the output stream.
107af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  ~LogStream();
108af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
109af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  template<typename T> std::ostream& operator<<(const T &t) {
110af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai    return stream_ << t;
111af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  }
112af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
113af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai private:
114af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  std::ostream &stream_;
115af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
116af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  // Disallow copy constructor and assignment operator
117af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  explicit LogStream(const LogStream &that);
118af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  void operator=(const LogStream &that);
119af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai};
120af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
121af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai// This class is used to explicitly ignore values in the conditional logging
122af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai// macros.  This avoids compiler warnings like "value computed is not used"
123af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai// and "statement has no effect".
124af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovaiclass LogMessageVoidify {
125af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai public:
126af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  LogMessageVoidify() {}
127af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
128af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  // This has to be an operator with a precedence lower than << but higher
129af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai  // than ?:
1307deb13d22a2a140d63107add9f57b36e390ffad1mark@chromium.org  void operator&(base_logging::LogMessage &) {}
131af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai};
132af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
133af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai// Returns number formatted as a hexadecimal string, such as "0x7b".
1346162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.comstring HexString(uint32_t number);
1356162aed3c3fcfc53373c963ac375d39a5dfa5a25ted.mielczarek@gmail.comstring HexString(uint64_t number);
1364e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.comstring HexString(int number);
137af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
138af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai// Returns the error code as set in the global errno variable, and sets
139af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai// error_string, a required argument, to a string describing that error
140af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai// code.
1414e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.comint ErrnoString(string *error_string);
142af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
143af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai}  // namespace google_breakpad
144af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
14532b802dba3d49880a0414d066e71cdc20ab09901mmentovai#ifndef BPLOG_INIT
14632b802dba3d49880a0414d066e71cdc20ab09901mmentovai#define BPLOG_INIT(pargc, pargv)
14732b802dba3d49880a0414d066e71cdc20ab09901mmentovai#endif  // BPLOG_INIT
14832b802dba3d49880a0414d066e71cdc20ab09901mmentovai
1498d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org#define BPLOG_LAZY_STREAM(stream, condition) \
1508d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org    !(condition) ? (void) 0 : \
1518d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org                   google_breakpad::LogMessageVoidify() & (BPLOG_ ## stream)
1528d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org
1538d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org#ifndef BPLOG_MINIMUM_SEVERITY
1548d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org#define BPLOG_MINIMUM_SEVERITY SEVERITY_INFO
1558d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org#endif
1568d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org
1578d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org#define BPLOG_LOG_IS_ON(severity) \
1588d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org    ((google_breakpad::LogStream::SEVERITY_ ## severity) >= \
1598d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org     (google_breakpad::LogStream::BPLOG_MINIMUM_SEVERITY))
1608d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org
161af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#ifndef BPLOG
1628d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org#define BPLOG(severity) BPLOG_LAZY_STREAM(severity, BPLOG_LOG_IS_ON(severity))
163af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#endif  // BPLOG
164af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
165af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#ifndef BPLOG_INFO
166af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#ifndef BPLOG_INFO_STREAM
167af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#define BPLOG_INFO_STREAM std::clog
168af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#endif  // BPLOG_INFO_STREAM
16932b802dba3d49880a0414d066e71cdc20ab09901mmentovai#define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
17032b802dba3d49880a0414d066e71cdc20ab09901mmentovai                       google_breakpad::LogStream::SEVERITY_INFO, \
17132b802dba3d49880a0414d066e71cdc20ab09901mmentovai                       __FILE__, __LINE__)
172af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#endif  // BPLOG_INFO
173af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
174af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#ifndef BPLOG_ERROR
175af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#ifndef BPLOG_ERROR_STREAM
176af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#define BPLOG_ERROR_STREAM std::cerr
177af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#endif  // BPLOG_ERROR_STREAM
17832b802dba3d49880a0414d066e71cdc20ab09901mmentovai#define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
17932b802dba3d49880a0414d066e71cdc20ab09901mmentovai                        google_breakpad::LogStream::SEVERITY_ERROR, \
18032b802dba3d49880a0414d066e71cdc20ab09901mmentovai                        __FILE__, __LINE__)
181af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#endif  // BPLOG_ERROR
182af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
183af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#define BPLOG_IF(severity, condition) \
1848d12b634d339096798b2636c3cd0c3372e0c6f6amark@chromium.org    BPLOG_LAZY_STREAM(severity, ((condition) && BPLOG_LOG_IS_ON(severity)))
185af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai
186af3c43f00e98047bc7f80dcf4c16b876e095769fmmentovai#endif  // PROCESSOR_LOGGING_H__
187