From f3db22d5d9adcc1514bea4c48bfa5980b4be5b13 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Tue, 9 Jul 2024 15:09:28 -0700 Subject: [PATCH] [Impeller] Validation logs indicate where in code the validation error happened. (flutter/engine#53784) Earlier, all validation logs originated from validation.cc which was not useful at all since you had to find the string in the source. --- .../src/flutter/impeller/base/validation.cc | 27 +++++++++---------- engine/src/flutter/impeller/base/validation.h | 8 +++--- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/engine/src/flutter/impeller/base/validation.cc b/engine/src/flutter/impeller/base/validation.cc index d95a84c811..ebba118524 100644 --- a/engine/src/flutter/impeller/base/validation.cc +++ b/engine/src/flutter/impeller/base/validation.cc @@ -33,11 +33,12 @@ ScopedValidationFatal::~ScopedValidationFatal() { sValidationLogsAreFatal--; } -ValidationLog::ValidationLog() = default; +ValidationLog::ValidationLog(const char* file, int line) + : file_(file), line_(line) {} ValidationLog::~ValidationLog() { if (sValidationLogsDisabledCount <= 0) { - ImpellerValidationBreak(stream_.str().c_str()); + ImpellerValidationBreak(stream_.str().c_str(), file_, line_); } } @@ -45,19 +46,17 @@ std::ostream& ValidationLog::GetStream() { return stream_; } -void ImpellerValidationBreak(const char* message) { - std::stringstream stream; +void ImpellerValidationBreak(const char* message, const char* file, int line) { + const auto severity = + ImpellerValidationErrorsAreFatal() ? fml::LOG_FATAL : fml::LOG_ERROR; + auto fml_log = fml::LogMessage{severity, file, line, nullptr}; + fml_log.stream() << #if FLUTTER_RELEASE - stream << "Impeller validation: " << message; -#else - stream << "Break on '" << __FUNCTION__ - << "' to inspect point of failure: " << message; -#endif - if (sValidationLogsAreFatal > 0) { - FML_LOG(FATAL) << stream.str(); - } else { - FML_LOG(ERROR) << stream.str(); - } + "Impeller validation: " << message; +#else // FLUTTER_RELEASE + "Break on '" << __FUNCTION__ + << "' to inspect point of failure: " << message; +#endif // FLUTTER_RELEASE } bool ImpellerValidationErrorsAreFatal() { diff --git a/engine/src/flutter/impeller/base/validation.h b/engine/src/flutter/impeller/base/validation.h index 6d3ca3a52d..47ef7c20fc 100644 --- a/engine/src/flutter/impeller/base/validation.h +++ b/engine/src/flutter/impeller/base/validation.h @@ -11,13 +11,15 @@ namespace impeller { class ValidationLog { public: - ValidationLog(); + ValidationLog(const char* file, int line); ~ValidationLog(); std::ostream& GetStream(); private: + const char* file_ = nullptr; + int line_ = 0; std::ostringstream stream_; ValidationLog(const ValidationLog&) = delete; @@ -29,7 +31,7 @@ class ValidationLog { ValidationLog& operator=(ValidationLog&&) = delete; }; -void ImpellerValidationBreak(const char* message); +void ImpellerValidationBreak(const char* message, const char* file, int line); void ImpellerValidationErrorsSetFatal(bool fatal); @@ -70,6 +72,6 @@ struct ScopedValidationFatal { /// are fatal. The runtime-mode restriction still applies. This usually /// happens in test environments. /// -#define VALIDATION_LOG ::impeller::ValidationLog{}.GetStream() +#define VALIDATION_LOG ::impeller::ValidationLog{__FILE__, __LINE__}.GetStream() #endif // FLUTTER_IMPELLER_BASE_VALIDATION_H_