aboutsummaryrefslogtreecommitdiff
path: root/src/processor/logging.h
blob: 642506d5d6c5eac17d232f5f95d5d1637b7a23ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// logging.h: Breakpad logging
//
// Breakpad itself uses Breakpad logging with statements of the form:
//   BPLOG(severity) << "message";
// severity may be INFO, ERROR, or other values defined in this file.
//
// BPLOG is an overridable macro so that users can customize Breakpad's
// logging.  Left at the default, logging messages are sent to stderr along
// with a timestamp and the source code location that produced a message.
// The streams may be changed by redefining BPLOG_*_STREAM, the logging
// behavior may be changed by redefining BPLOG_*, and the entire logging
// system may be overridden by redefining BPLOG(severity).  These
// redefinitions may be passed to the preprocessor as a command-line flag
// (-D).
//
// If an additional header is required to override Breakpad logging, it can
// be specified by the BP_LOGGING_INCLUDE macro.  If defined, this header
// will #include the header specified by that macro.
//
// If any initialization is needed before logging, it can be performed by
// a function called through the BPLOG_INIT macro.  Each main function of
// an executable program in the Breakpad processor library calls
// BPLOG_INIT(&argc, &argv); before any logging can be performed; define
// BPLOG_INIT appropriately if initialization is required.
//
// Author: Mark Mentovai

#ifndef PROCESSOR_LOGGING_H__
#define PROCESSOR_LOGGING_H__

#include <iostream>
#include <string>

#include "google_breakpad/common/breakpad_types.h"

#ifdef BP_LOGGING_INCLUDE
#include BP_LOGGING_INCLUDE
#endif  // BP_LOGGING_INCLUDE

namespace google_breakpad {

// These are defined in Microsoft headers.
#ifdef SEVERITY_ERROR
#undef SEVERITY_ERROR
#endif

#ifdef ERROR
#undef ERROR
#endif

class LogStream {
 public:
  enum Severity {
    SEVERITY_INFO,
    SEVERITY_ERROR
  };

  // Begin logging a message to the stream identified by |stream|, at the
  // indicated severity.  The file and line parameters should be set so as to
  // identify the line of source code that is producing a message.
  LogStream(std::ostream &stream, Severity severity,
            const char *file, int line);

  // Finish logging by printing a newline and flushing the output stream.
  ~LogStream();

  template<typename T> std::ostream& operator<<(const T &t) {
    return stream_ << t;
  }

 private:
  std::ostream &stream_;

  // Disallow copy constructor and assignment operator
  explicit LogStream(const LogStream &that);
  void operator=(const LogStream &that);
};

// This class is used to explicitly ignore values in the conditional logging
// macros.  This avoids compiler warnings like "value computed is not used"
// and "statement has no effect".
class LogMessageVoidify {
 public:
  LogMessageVoidify() {}

  // This has to be an operator with a precedence lower than << but higher
  // than ?:
  void operator&(std::ostream &) {}
};

// Returns number formatted as a hexadecimal string, such as "0x7b".
std::string HexString(u_int32_t number);
std::string HexString(u_int64_t number);
std::string HexString(int number);

// Returns the error code as set in the global errno variable, and sets
// error_string, a required argument, to a string describing that error
// code.
int ErrnoString(std::string *error_string);

}  // namespace google_breakpad

#ifndef BPLOG_INIT
#define BPLOG_INIT(pargc, pargv)
#endif  // BPLOG_INIT

#ifndef BPLOG
#define BPLOG(severity) BPLOG_ ## severity
#endif  // BPLOG

#ifndef BPLOG_INFO
#ifndef BPLOG_INFO_STREAM
#define BPLOG_INFO_STREAM std::clog
#endif  // BPLOG_INFO_STREAM
#define BPLOG_INFO google_breakpad::LogStream(BPLOG_INFO_STREAM, \
                       google_breakpad::LogStream::SEVERITY_INFO, \
                       __FILE__, __LINE__)
#endif  // BPLOG_INFO

#ifndef BPLOG_ERROR
#ifndef BPLOG_ERROR_STREAM
#define BPLOG_ERROR_STREAM std::cerr
#endif  // BPLOG_ERROR_STREAM
#define BPLOG_ERROR google_breakpad::LogStream(BPLOG_ERROR_STREAM, \
                        google_breakpad::LogStream::SEVERITY_ERROR, \
                        __FILE__, __LINE__)
#endif  // BPLOG_ERROR

#define BPLOG_IF(severity, condition) \
    !(condition) ? (void) 0 : \
                   google_breakpad::LogMessageVoidify() & BPLOG(severity)

#endif  // PROCESSOR_LOGGING_H__