| Flutter Impeller |

validation.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "impeller/base/validation.h"
6
7 #include <atomic>
8
9 #include "flutter/fml/logging.h"
10
11 namespace impeller {
12
13 static std::atomic_int32_t sValidationLogsDisabledCount = 0;
14 static std::atomic_int32_t sValidationLogsAreFatal = 0;
15 static ValidationFailureCallback sValidationFailureCallback;
16
17 void ImpellerValidationErrorsSetFatal(bool fatal) {
18 sValidationLogsAreFatal = fatal;
19 }
20
21 void ImpellerValidationErrorsSetCallback(ValidationFailureCallback callback) {
22 sValidationFailureCallback = std::move(callback);
23 }
24
25 ScopedValidationDisable::ScopedValidationDisable() {
26 sValidationLogsDisabledCount++;
27 }
28
29 ScopedValidationDisable::~ScopedValidationDisable() {
30 sValidationLogsDisabledCount--;
31 }
32
33 ScopedValidationFatal::ScopedValidationFatal() {
34 sValidationLogsAreFatal++;
35 }
36
37 ScopedValidationFatal::~ScopedValidationFatal() {
38 sValidationLogsAreFatal--;
39 }
40
41 ValidationLog::ValidationLog(const char* file, int line)
42 : file_(file), line_(line) {}
43
44 ValidationLog::~ValidationLog() {
45 if (sValidationLogsDisabledCount <= 0) {
46 ImpellerValidationBreak(stream_.str().c_str(), file_, line_);
47 }
48 }
49
50 std::ostream& ValidationLog::GetStream() {
51 return stream_;
52 }
53
54 void ImpellerValidationBreak(const char* message, const char* file, int line) {
55 if (sValidationFailureCallback &&
56 sValidationFailureCallback(message, file, line)) {
57 return;
58 }
59 const auto severity =
60 ImpellerValidationErrorsAreFatal() ? fml::LOG_FATAL : fml::LOG_ERROR;
61 auto fml_log = fml::LogMessage{severity, file, line, nullptr};
62 fml_log.stream() <<
63 #if FLUTTER_RELEASE
64 "Impeller validation: " << message;
65 #else // FLUTTER_RELEASE
66 "Break on '" << __FUNCTION__
67 << "' to inspect point of failure: " << message;
68 #endif // FLUTTER_RELEASE
69 }
70
71 bool ImpellerValidationErrorsAreFatal() {
72 return sValidationLogsAreFatal;
73 }
74
75 } // namespace impeller
impeller::ScopedValidationFatal::ScopedValidationFatal
ScopedValidationFatal()
Definition: validation.cc:33
impeller::sValidationLogsAreFatal
static std::atomic_int32_t sValidationLogsAreFatal
Definition: validation.cc:14
impeller::ScopedValidationDisable::~ScopedValidationDisable
~ScopedValidationDisable()
Definition: validation.cc:29
impeller::ScopedValidationFatal::~ScopedValidationFatal
~ScopedValidationFatal()
Definition: validation.cc:37
validation.h
impeller::ValidationLog::~ValidationLog
~ValidationLog()
Definition: validation.cc:44
impeller::ValidationFailureCallback
std::function< bool(const char *message, const char *file, int line)> ValidationFailureCallback
Definition: validation.h:42
impeller::ScopedValidationDisable::ScopedValidationDisable
ScopedValidationDisable()
Definition: validation.cc:25
impeller::sValidationLogsDisabledCount
static std::atomic_int32_t sValidationLogsDisabledCount
Definition: validation.cc:13
impeller::ImpellerValidationErrorsSetFatal
void ImpellerValidationErrorsSetFatal(bool fatal)
Definition: validation.cc:17
impeller::ImpellerValidationErrorsAreFatal
bool ImpellerValidationErrorsAreFatal()
Definition: validation.cc:71
impeller::ValidationLog::ValidationLog
ValidationLog(const char *file, int line)
Definition: validation.cc:41
impeller::ImpellerValidationBreak
void ImpellerValidationBreak(const char *message, const char *file, int line)
Definition: validation.cc:54
impeller::ValidationLog::GetStream
std::ostream & GetStream()
Definition: validation.cc:50
impeller::sValidationFailureCallback
static ValidationFailureCallback sValidationFailureCallback
Definition: validation.cc:15
impeller
Definition: allocation.cc:12
impeller::ImpellerValidationErrorsSetCallback
void ImpellerValidationErrorsSetCallback(ValidationFailureCallback callback)
Sets a callback that callers (usually tests) can set to intercept validation failures.
Definition: validation.cc:21