blob: 9bc5fc85c661b40055f5523b2e1be0337a4dcf9a [file] [log] [blame]
Hans Wennborg12aea3e2020-04-14 15:29:001// Copyright 2020 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/check.h"
6
7// check.h is a widely included header and its size has significant impact on
8// build time. Try not to raise this limit unless absolutely necessary. See
9// https://chromium.googlesource.com/chromium/src/+/HEAD/docs/wmax_tokens.md
10#ifndef NACL_TC_REV
11#pragma clang max_tokens_here 17000
12#endif
13
Hans Wennborgafeb3902020-06-17 14:42:2914#include "base/check_op.h"
Hans Wennborg12aea3e2020-04-14 15:29:0015#include "base/logging.h"
16#include "build/build_config.h"
17
18namespace logging {
19
20CheckError CheckError::Check(const char* file,
21 int line,
22 const char* condition) {
23 CheckError check_error(new LogMessage(file, line, LOG_FATAL));
24 check_error.stream() << "Check failed: " << condition << ". ";
25 return check_error;
26}
27
28CheckError CheckError::CheckOp(const char* file,
29 int line,
30 CheckOpResult* check_op_result) {
31 CheckError check_error(new LogMessage(file, line, LOG_FATAL));
32 check_error.stream() << "Check failed: " << check_op_result->message_;
33 free(check_op_result->message_);
34 check_op_result->message_ = nullptr;
35 return check_error;
36}
37
38CheckError CheckError::DCheck(const char* file,
39 int line,
40 const char* condition) {
41 CheckError check_error(new LogMessage(file, line, LOG_DCHECK));
42 check_error.stream() << "Check failed: " << condition << ". ";
43 return check_error;
44}
45
46CheckError CheckError::DCheckOp(const char* file,
47 int line,
48 CheckOpResult* check_op_result) {
49 CheckError check_error(new LogMessage(file, line, LOG_DCHECK));
50 check_error.stream() << "Check failed: " << check_op_result->message_;
51 free(check_op_result->message_);
52 check_op_result->message_ = nullptr;
53 return check_error;
54}
55
56CheckError CheckError::PCheck(const char* file,
57 int line,
58 const char* condition) {
59 SystemErrorCode err_code = logging::GetLastSystemErrorCode();
60#if defined(OS_WIN)
61 CheckError check_error(
62 new Win32ErrorLogMessage(file, line, LOG_FATAL, err_code));
63#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
64 CheckError check_error(new ErrnoLogMessage(file, line, LOG_FATAL, err_code));
65#endif
66 check_error.stream() << "Check failed: " << condition << ". ";
67 return check_error;
68}
69
70CheckError CheckError::PCheck(const char* file, int line) {
71 return PCheck(file, line, "");
72}
73
74CheckError CheckError::DPCheck(const char* file,
75 int line,
76 const char* condition) {
77 SystemErrorCode err_code = logging::GetLastSystemErrorCode();
78#if defined(OS_WIN)
79 CheckError check_error(
80 new Win32ErrorLogMessage(file, line, LOG_DCHECK, err_code));
81#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
82 CheckError check_error(new ErrnoLogMessage(file, line, LOG_DCHECK, err_code));
83#endif
84 check_error.stream() << "Check failed: " << condition << ". ";
85 return check_error;
86}
87
Hans Wennborg0472f8c2020-04-23 19:27:2788CheckError CheckError::NotImplemented(const char* file,
89 int line,
90 const char* function) {
91 CheckError check_error(new LogMessage(file, line, LOG_ERROR));
Hans Wennborgb8ede0762020-04-27 15:44:2992 check_error.stream() << "Not implemented reached in " << function;
Hans Wennborg0472f8c2020-04-23 19:27:2793 return check_error;
94}
95
Hans Wennborg12aea3e2020-04-14 15:29:0096std::ostream& CheckError::stream() {
97 return log_message_->stream();
98}
99
100CheckError::~CheckError() {
Hans Wennborg1ab93ba2020-04-15 16:06:30101 // Note: This function ends up in crash stack traces. If its full name
102 // changes, the crash server's magic signature logic needs to be updated.
103 // See cl/306632920.
Hans Wennborg12aea3e2020-04-14 15:29:00104 delete log_message_;
105}
106
107CheckError::CheckError(LogMessage* log_message) : log_message_(log_message) {}
108
109void RawCheck(const char* message) {
110 RawLog(LOG_FATAL, message);
111}
112
113} // namespace logging