Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/security/sandbox/chromium-shim/base/logging.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
// This is a stripped down version of the Chromium source file base/logging.cc
8
// This prevents dependency on the Chromium logging and dependency creep in
9
// general.
10
// At some point we should find a way to hook this into our own logging see
11
// bug 1013988.
12
// The formatting in this file matches the original Chromium file to aid future
13
// merging.
14
15
#include "base/logging.h"
16
17
#if defined(OS_WIN)
18
#include <windows.h>
19
#endif
20
21
#if defined(OS_POSIX)
22
#include <errno.h>
23
#endif
24
25
#if defined(OS_WIN)
26
#include "base/strings/utf_string_conversions.h"
27
#endif
28
29
#include <algorithm>
30
31
#include "mozilla/Assertions.h"
32
#include "mozilla/Unused.h"
33
34
namespace logging {
35
36
namespace {
37
38
int g_min_log_level = 0;
39
40
LoggingDestination g_logging_destination = LOG_DEFAULT;
41
42
// For LOG_ERROR and above, always print to stderr.
43
const int kAlwaysPrintErrorLevel = LOG_ERROR;
44
45
// A log message handler that gets notified of every log message we process.
46
LogMessageHandlerFunction log_message_handler = nullptr;
47
48
}  // namespace
49
50
// This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have
51
// an object of the correct type on the LHS of the unused part of the ternary
52
// operator.
53
std::ostream* g_swallow_stream;
54
55
0
void SetMinLogLevel(int level) {
56
0
  g_min_log_level = std::min(LOG_FATAL, level);
57
0
}
58
59
0
int GetMinLogLevel() {
60
0
  return g_min_log_level;
61
0
}
62
63
0
bool ShouldCreateLogMessage(int severity) {
64
0
  if (severity < g_min_log_level)
65
0
    return false;
66
0
67
0
  // Return true here unless we know ~LogMessage won't do anything. Note that
68
0
  // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even
69
0
  // when g_logging_destination is LOG_NONE.
70
0
  return g_logging_destination != LOG_NONE || log_message_handler ||
71
0
         severity >= kAlwaysPrintErrorLevel;
72
0
}
73
74
0
int GetVlogLevelHelper(const char* file, size_t N) {
75
0
  return 0;
76
0
}
77
78
// Explicit instantiations for commonly used comparisons.
79
template std::string* MakeCheckOpString<int, int>(
80
    const int&, const int&, const char* names);
81
template std::string* MakeCheckOpString<unsigned long, unsigned long>(
82
    const unsigned long&, const unsigned long&, const char* names);
83
template std::string* MakeCheckOpString<unsigned long, unsigned int>(
84
    const unsigned long&, const unsigned int&, const char* names);
85
template std::string* MakeCheckOpString<unsigned int, unsigned long>(
86
    const unsigned int&, const unsigned long&, const char* names);
87
template std::string* MakeCheckOpString<std::string, std::string>(
88
    const std::string&, const std::string&, const char* name);
89
90
#if defined(OS_WIN)
91
LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) {
92
}
93
94
LogMessage::SaveLastError::~SaveLastError() {
95
  ::SetLastError(last_error_);
96
}
97
#endif  // defined(OS_WIN)
98
99
LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
100
0
    : severity_(severity), file_(file), line_(line) {
101
0
}
102
103
LogMessage::LogMessage(const char* file, int line, const char* condition)
104
0
    : severity_(LOG_FATAL), file_(file), line_(line) {
105
0
}
106
107
LogMessage::LogMessage(const char* file, int line, std::string* result)
108
0
    : severity_(LOG_FATAL), file_(file), line_(line) {
109
0
  delete result;
110
0
}
111
112
LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
113
                       std::string* result)
114
0
    : severity_(severity), file_(file), line_(line) {
115
0
  delete result;
116
0
}
117
118
0
LogMessage::~LogMessage() {
119
0
  if (severity_ == LOG_FATAL) {
120
0
    MOZ_CRASH("Hit fatal chromium sandbox condition.");
121
0
  }
122
0
}
123
124
0
SystemErrorCode GetLastSystemErrorCode() {
125
#if defined(OS_WIN)
126
  return ::GetLastError();
127
#elif defined(OS_POSIX)
128
  return errno;
129
#else
130
#error Not implemented
131
#endif
132
}
133
134
#if defined(OS_WIN)
135
Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file,
136
                                           int line,
137
                                           LogSeverity severity,
138
                                           SystemErrorCode err)
139
    : err_(err),
140
      log_message_(file, line, severity) {
141
  mozilla::Unused << err_;
142
}
143
144
Win32ErrorLogMessage::~Win32ErrorLogMessage() {
145
}
146
#elif defined(OS_POSIX)
147
ErrnoLogMessage::ErrnoLogMessage(const char* file,
148
                                 int line,
149
                                 LogSeverity severity,
150
                                 SystemErrorCode err)
151
    : err_(err),
152
0
      log_message_(file, line, severity) {
153
0
  mozilla::Unused << err_;
154
0
}
155
156
0
ErrnoLogMessage::~ErrnoLogMessage() {
157
0
}
158
#endif  // OS_WIN
159
160
0
void RawLog(int level, const char* message) {
161
0
}
162
163
} // namespace logging
164
165
#if defined(OS_WIN)
166
std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) {
167
  return out << base::WideToUTF8(std::wstring(wstr));
168
}
169
#endif