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

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: address grt's comments Created 4 years, 4 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
11 #include <stddef.h> 11 #include <stddef.h>
12 12
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/strings/string_piece.h" 14 #include "base/strings/string_piece.h"
15 #include "chrome/installer/util/installation_state.h" 15 #include "chrome/installer/util/installation_state.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 namespace installer { 18 namespace installer {
19 19
20 namespace { 20 namespace {
21 21
22 // A helper class that installs a log message handler to add a test failure for 22 // A helper class that installs a log message handler to add a test failure for
23 // each ERROR message. Only one instance of this class may be live at a time. 23 // each ERROR message. Only one instance of this class may be live at a time.
24 class FailureLogHelper { 24 class FailureLogHelper : logging::LogMessageHandler {
25 public: 25 public:
26 FailureLogHelper(); 26 FailureLogHelper();
27 ~FailureLogHelper(); 27 ~FailureLogHelper() override;
28 28
29 bool OnMessage(int severity,
30 const char* file,
31 int line,
32 size_t message_start,
33 const std::string& str) override;
29 private: 34 private:
30 static bool AddFailureForLogMessage(int severity,
31 const char* file,
32 int line,
33 size_t message_start,
34 const std::string& str);
35
36 static const logging::LogSeverity kViolationSeverity_; 35 static const logging::LogSeverity kViolationSeverity_;
37 static logging::LogMessageHandlerFunction old_message_handler_;
38 static int old_min_log_level_; 36 static int old_min_log_level_;
39 }; 37 };
40 38
41 // InstallationValidator logs all violations at ERROR level. 39 // InstallationValidator logs all violations at ERROR level.
42 // static 40 // static
43 const logging::LogSeverity 41 const logging::LogSeverity
44 FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR; 42 FailureLogHelper::kViolationSeverity_ = logging::LOG_ERROR;
45 43
46 // static 44 // static
47 logging::LogMessageHandlerFunction
48 FailureLogHelper::old_message_handler_ = NULL;
49
50 // static
51 int FailureLogHelper::old_min_log_level_ = 45 int FailureLogHelper::old_min_log_level_ =
52 FailureLogHelper::kViolationSeverity_; 46 FailureLogHelper::kViolationSeverity_;
53 47
54 FailureLogHelper::FailureLogHelper() { 48 FailureLogHelper::FailureLogHelper() {
55 LOG_ASSERT(old_message_handler_ == NULL);
56
57 // The validator logs at ERROR level. Ensure that it generates messages so we 49 // The validator logs at ERROR level. Ensure that it generates messages so we
58 // can transform them into test failures. 50 // can transform them into test failures.
59 old_min_log_level_ = logging::GetMinLogLevel(); 51 old_min_log_level_ = logging::GetMinLogLevel();
60 if (old_min_log_level_ > kViolationSeverity_) 52 if (old_min_log_level_ > kViolationSeverity_)
61 logging::SetMinLogLevel(kViolationSeverity_); 53 logging::SetMinLogLevel(kViolationSeverity_);
62
63 old_message_handler_ = logging::GetLogMessageHandler();
64 logging::SetLogMessageHandler(&AddFailureForLogMessage);
65 } 54 }
66 55
67 FailureLogHelper::~FailureLogHelper() { 56 FailureLogHelper::~FailureLogHelper() {
68 logging::SetLogMessageHandler(old_message_handler_);
69 old_message_handler_ = NULL;
70
71 if (old_min_log_level_ > kViolationSeverity_) 57 if (old_min_log_level_ > kViolationSeverity_)
72 logging::SetMinLogLevel(old_min_log_level_); 58 logging::SetMinLogLevel(old_min_log_level_);
73 } 59 }
74 60
75 // A logging::LogMessageHandlerFunction that adds a non-fatal test failure 61 // A logging::LogMessageHandler that adds a non-fatal test failure
76 // (i.e., similar to an unmet EXPECT_FOO) for each non-empty message logged at 62 // (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 63 // the severity of validation violations. All other messages are sent through
78 // the default logging pipeline. 64 // the default logging pipeline.
79 // static 65 // static
80 bool FailureLogHelper::AddFailureForLogMessage(int severity, 66 bool FailureLogHelper::OnMessage(int severity,
81 const char* file, 67 const char* file,
82 int line, 68 int line,
83 size_t message_start, 69 size_t message_start,
84 const std::string& str) { 70 const std::string& str) {
85 if (severity == kViolationSeverity_ && !str.empty()) { 71 if (severity == kViolationSeverity_ && !str.empty()) {
86 // Remove the trailing newline, if present. 72 // Remove the trailing newline, if present.
87 size_t message_length = str.size() - message_start; 73 size_t message_length = str.size() - message_start;
88 if (*str.rbegin() == '\n') 74 if (*str.rbegin() == '\n')
89 --message_length; 75 --message_length;
90 ADD_FAILURE_AT(file, line) 76 ADD_FAILURE_AT(file, line)
91 << base::StringPiece(str.c_str() + message_start, message_length); 77 << base::StringPiece(str.c_str() + message_start, message_length);
92 return true; 78 return true;
93 } 79 }
94 80
95 if (old_message_handler_ != NULL)
96 return (old_message_handler_)(severity, file, line, message_start, str);
97
98 return false; 81 return false;
99 } 82 }
100 83
101 } // namespace 84 } // namespace
102 85
103 InstallationValidator::InstallationType ExpectValidInstallation( 86 InstallationValidator::InstallationType ExpectValidInstallation(
104 bool system_level) { 87 bool system_level) {
105 FailureLogHelper log_helper; 88 FailureLogHelper log_helper;
106 InstallationValidator::InstallationType found_type = 89 InstallationValidator::InstallationType found_type =
107 InstallationValidator::NO_PRODUCTS; 90 InstallationValidator::NO_PRODUCTS;
(...skipping 21 matching lines...) Expand all
129 } 112 }
130 113
131 void ExpectInstallationOfTypeForState( 114 void ExpectInstallationOfTypeForState(
132 const InstallationState& machine_state, 115 const InstallationState& machine_state,
133 bool system_level, 116 bool system_level,
134 InstallationValidator::InstallationType type) { 117 InstallationValidator::InstallationType type) {
135 EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level)); 118 EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level));
136 } 119 }
137 120
138 } // namespace installer 121 } // namespace installer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698