Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: mojo/public/cpp/bindings/lib/validation_errors.cc

Issue 2448203002: Mojo: Suppress validation error logging in some tests (Closed)
Patch Set: moar comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/lib/validation_errors.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/cpp/bindings/lib/validation_errors.h" 5 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
6 6
7 #include "base/strings/stringprintf.h" 7 #include "base/strings/stringprintf.h"
8 #include "mojo/public/cpp/bindings/message.h" 8 #include "mojo/public/cpp/bindings/message.h"
9 9
10 namespace mojo { 10 namespace mojo {
11 namespace internal { 11 namespace internal {
12 namespace { 12 namespace {
13 13
14 ValidationErrorObserverForTesting* g_validation_error_observer = nullptr; 14 ValidationErrorObserverForTesting* g_validation_error_observer = nullptr;
15 SerializationWarningObserverForTesting* g_serialization_warning_observer = 15 SerializationWarningObserverForTesting* g_serialization_warning_observer =
16 nullptr; 16 nullptr;
17 bool g_suppress_logging = false;
17 18
18 } // namespace 19 } // namespace
19 20
20 const char* ValidationErrorToString(ValidationError error) { 21 const char* ValidationErrorToString(ValidationError error) {
21 switch (error) { 22 switch (error) {
22 case VALIDATION_ERROR_NONE: 23 case VALIDATION_ERROR_NONE:
23 return "VALIDATION_ERROR_NONE"; 24 return "VALIDATION_ERROR_NONE";
24 case VALIDATION_ERROR_MISALIGNED_OBJECT: 25 case VALIDATION_ERROR_MISALIGNED_OBJECT:
25 return "VALIDATION_ERROR_MISALIGNED_OBJECT"; 26 return "VALIDATION_ERROR_MISALIGNED_OBJECT";
26 case VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE: 27 case VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE:
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 65
65 void ReportValidationError(ValidationContext* context, 66 void ReportValidationError(ValidationContext* context,
66 ValidationError error, 67 ValidationError error,
67 const char* description) { 68 const char* description) {
68 if (g_validation_error_observer) { 69 if (g_validation_error_observer) {
69 g_validation_error_observer->set_last_error(error); 70 g_validation_error_observer->set_last_error(error);
70 return; 71 return;
71 } 72 }
72 73
73 if (description) { 74 if (description) {
74 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error) << " (" 75 if (!g_suppress_logging) {
75 << description << ")"; 76 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error)
77 << " (" << description << ")";
78 }
76 if (context->message()) { 79 if (context->message()) {
77 context->message()->NotifyBadMessage( 80 context->message()->NotifyBadMessage(
78 base::StringPrintf("Validation failed for %s [%s (%s)]", 81 base::StringPrintf("Validation failed for %s [%s (%s)]",
79 context->description().data(), 82 context->description().data(),
80 ValidationErrorToString(error), description)); 83 ValidationErrorToString(error), description));
81 } 84 }
82 } else { 85 } else {
83 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error); 86 if (!g_suppress_logging)
87 LOG(ERROR) << "Invalid message: " << ValidationErrorToString(error);
84 if (context->message()) { 88 if (context->message()) {
85 context->message()->NotifyBadMessage( 89 context->message()->NotifyBadMessage(
86 base::StringPrintf("Validation failed for %s [%s]", 90 base::StringPrintf("Validation failed for %s [%s]",
87 context->description().data(), 91 context->description().data(),
88 ValidationErrorToString(error))); 92 ValidationErrorToString(error)));
89 } 93 }
90 } 94 }
91 } 95 }
92 96
93 void ReportValidationErrorForMessage( 97 void ReportValidationErrorForMessage(
94 mojo::Message* message, 98 mojo::Message* message,
95 ValidationError error, 99 ValidationError error,
96 const char* description) { 100 const char* description) {
97 ValidationContext validation_context( 101 ValidationContext validation_context(
98 message->data(), message->data_num_bytes(), 102 message->data(), message->data_num_bytes(),
99 message->handles()->size(), message, 103 message->handles()->size(), message,
100 description); 104 description);
101 ReportValidationError(&validation_context, error); 105 ReportValidationError(&validation_context, error);
102 } 106 }
103 107
108 void SuppressValidationErrorLoggingForTests(bool suppress) {
yzshen1 2016/11/02 16:44:20 This function is not used (or declared in the .h f
Ken Rockot(use gerrit already) 2016/11/02 17:23:15 Oops, forgot to delete. Done.
109 g_suppress_logging = suppress;
110 }
111
112 ScopedSuppressValidationErrorLoggingForTests
113 ::ScopedSuppressValidationErrorLoggingForTests()
114 : was_suppressed_(g_suppress_logging) {
115 g_suppress_logging = true;
116 }
117
118 ScopedSuppressValidationErrorLoggingForTests
119 ::~ScopedSuppressValidationErrorLoggingForTests() {
120 g_suppress_logging = was_suppressed_;
121 }
122
104 ValidationErrorObserverForTesting::ValidationErrorObserverForTesting( 123 ValidationErrorObserverForTesting::ValidationErrorObserverForTesting(
105 const base::Closure& callback) 124 const base::Closure& callback)
106 : last_error_(VALIDATION_ERROR_NONE), callback_(callback) { 125 : last_error_(VALIDATION_ERROR_NONE), callback_(callback) {
107 DCHECK(!g_validation_error_observer); 126 DCHECK(!g_validation_error_observer);
108 g_validation_error_observer = this; 127 g_validation_error_observer = this;
109 } 128 }
110 129
111 ValidationErrorObserverForTesting::~ValidationErrorObserverForTesting() { 130 ValidationErrorObserverForTesting::~ValidationErrorObserverForTesting() {
112 DCHECK(g_validation_error_observer == this); 131 DCHECK(g_validation_error_observer == this);
113 g_validation_error_observer = nullptr; 132 g_validation_error_observer = nullptr;
(...skipping 15 matching lines...) Expand all
129 } 148 }
130 149
131 SerializationWarningObserverForTesting:: 150 SerializationWarningObserverForTesting::
132 ~SerializationWarningObserverForTesting() { 151 ~SerializationWarningObserverForTesting() {
133 DCHECK(g_serialization_warning_observer == this); 152 DCHECK(g_serialization_warning_observer == this);
134 g_serialization_warning_observer = nullptr; 153 g_serialization_warning_observer = nullptr;
135 } 154 }
136 155
137 } // namespace internal 156 } // namespace internal
138 } // namespace mojo 157 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/validation_errors.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698