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

Side by Side Diff: chrome/installer/util/installation_validation_helper.cc

Issue 2034393004: Allow multiple logging::LogMessage{Handler,Listener}s Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean up, MockLog uses listener Created 4 years, 5 months 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // This file declares helper functions for use in tests that expect a valid 5 // This file declares helper functions for use in tests that expect a valid
6 // installation, possibly of a specific type. Validation violations result in 6 // installation, possibly of a specific type. Validation violations result in
7 // test failures. 7 // test failures.
8 8
9 #include "chrome/installer/util/installation_validation_helper.h" 9 #include "chrome/installer/util/installation_validation_helper.h"
10 10
(...skipping 16 matching lines...) Expand all
27 ~FailureLogHelper(); 27 ~FailureLogHelper();
28 28
29 private: 29 private:
30 static bool AddFailureForLogMessage(int severity, 30 static bool AddFailureForLogMessage(int severity,
31 const char* file, 31 const char* file,
32 int line, 32 int line,
33 size_t message_start, 33 size_t message_start,
34 const std::string& str); 34 const std::string& str);
35 35
36 static const logging::LogSeverity kViolationSeverity_; 36 static const logging::LogSeverity kViolationSeverity_;
37 static logging::LogMessageHandlerFunction old_message_handler_;
38 static int old_min_log_level_; 37 static int old_min_log_level_;
39 }; 38 };
40 39
41 // InstallationValidator logs all violations at ERROR level. 40 // InstallationValidator logs all violations at ERROR level.
42 // static 41 // static
43 const logging::LogSeverity 42 const logging::LogSeverity
44 FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR; 43 FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR;
45 44
46 // static 45 // static
47 logging::LogMessageHandlerFunction
48 FailureLogHelper::old_message_handler_ = NULL;
49
50 // static
51 int FailureLogHelper::old_min_log_level_ = 46 int FailureLogHelper::old_min_log_level_ =
52 FailureLogHelper::kViolationSeverity_; 47 FailureLogHelper::kViolationSeverity_;
53 48
54 FailureLogHelper::FailureLogHelper() { 49 FailureLogHelper::FailureLogHelper() {
55 LOG_ASSERT(old_message_handler_ == NULL);
56
57 // The validator logs at ERROR level. Ensure that it generates messages so we 50 // The validator logs at ERROR level. Ensure that it generates messages so we
58 // can transform them into test failures. 51 // can transform them into test failures.
59 old_min_log_level_ = logging::GetMinLogLevel(); 52 old_min_log_level_ = logging::GetMinLogLevel();
60 if (old_min_log_level_ > kViolationSeverity_) 53 if (old_min_log_level_ > kViolationSeverity_)
61 logging::SetMinLogLevel(kViolationSeverity_); 54 logging::SetMinLogLevel(kViolationSeverity_);
62 55
63 old_message_handler_ = logging::GetLogMessageHandler(); 56 logging::PushLogMessageHandler(AddFailureForLogMessage);
64 logging::SetLogMessageHandler(&AddFailureForLogMessage);
65 } 57 }
66 58
67 FailureLogHelper::~FailureLogHelper() { 59 FailureLogHelper::~FailureLogHelper() {
68 logging::SetLogMessageHandler(old_message_handler_); 60 logging::PopLogMessageHandler(AddFailureForLogMessage);
Dan Beam 2016/07/15 03:11:19 awesome
69 old_message_handler_ = NULL;
70 61
71 if (old_min_log_level_ > kViolationSeverity_) 62 if (old_min_log_level_ > kViolationSeverity_)
72 logging::SetMinLogLevel(old_min_log_level_); 63 logging::SetMinLogLevel(old_min_log_level_);
73 } 64 }
74 65
75 // A logging::LogMessageHandlerFunction that adds a non-fatal test failure 66 // A logging::LogMessageHandlerFunction that adds a non-fatal test failure
76 // (i.e., similar to an unmet EXPECT_FOO) for each non-empty message logged at 67 // (i.e., similar to an unmet EXPECT_FOO) for each non-empty message logged at
77 // the severity of validation violations. All other messages are sent through 68 // the severity of validation violations. All other messages are sent through
78 // the default logging pipeline. 69 // the default logging pipeline.
79 // static 70 // static
80 bool FailureLogHelper::AddFailureForLogMessage(int severity, 71 bool FailureLogHelper::AddFailureForLogMessage(int severity,
81 const char* file, 72 const char* file,
82 int line, 73 int line,
83 size_t message_start, 74 size_t message_start,
84 const std::string& str) { 75 const std::string& str) {
85 if (severity == kViolationSeverity_ && !str.empty()) { 76 if (severity == kViolationSeverity_ && !str.empty()) {
86 // Remove the trailing newline, if present. 77 // Remove the trailing newline, if present.
87 size_t message_length = str.size() - message_start; 78 size_t message_length = str.size() - message_start;
88 if (*str.rbegin() == '\n') 79 if (*str.rbegin() == '\n')
89 --message_length; 80 --message_length;
90 ADD_FAILURE_AT(file, line) 81 ADD_FAILURE_AT(file, line)
91 << base::StringPiece(str.c_str() + message_start, message_length); 82 << base::StringPiece(str.c_str() + message_start, message_length);
92 return true; 83 return true;
93 } 84 }
94 85
95 if (old_message_handler_ != NULL)
96 return (old_message_handler_)(severity, file, line, message_start, str);
97
98 return false; 86 return false;
99 } 87 }
100 88
101 } // namespace 89 } // namespace
102 90
103 InstallationValidator::InstallationType ExpectValidInstallation( 91 InstallationValidator::InstallationType ExpectValidInstallation(
104 bool system_level) { 92 bool system_level) {
105 FailureLogHelper log_helper; 93 FailureLogHelper log_helper;
106 InstallationValidator::InstallationType found_type = 94 InstallationValidator::InstallationType found_type =
107 InstallationValidator::NO_PRODUCTS; 95 InstallationValidator::NO_PRODUCTS;
(...skipping 21 matching lines...) Expand all
129 } 117 }
130 118
131 void ExpectInstallationOfTypeForState( 119 void ExpectInstallationOfTypeForState(
132 const InstallationState& machine_state, 120 const InstallationState& machine_state,
133 bool system_level, 121 bool system_level,
134 InstallationValidator::InstallationType type) { 122 InstallationValidator::InstallationType type) {
135 EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level)); 123 EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level));
136 } 124 }
137 125
138 } // namespace installer 126 } // namespace installer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698