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

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: use ReadWriteLock, add 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
29 bool OnMessage(int severity,
30 const char* file,
31 int line,
32 size_t message_start,
33 const std::string& str) override;
28 34
29 private: 35 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_; 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
63 old_message_handler_ = logging::GetLogMessageHandler();
64 logging::SetLogMessageHandler(&AddFailureForLogMessage);
65 } 55 }
66 56
67 FailureLogHelper::~FailureLogHelper() { 57 FailureLogHelper::~FailureLogHelper() {
68 logging::SetLogMessageHandler(old_message_handler_);
69 old_message_handler_ = NULL;
70
71 if (old_min_log_level_ > kViolationSeverity_) 58 if (old_min_log_level_ > kViolationSeverity_)
72 logging::SetMinLogLevel(old_min_log_level_); 59 logging::SetMinLogLevel(old_min_log_level_);
73 } 60 }
74 61
75 // A logging::LogMessageHandlerFunction that adds a non-fatal test failure 62 // 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 63 // (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 64 // the severity of validation violations. All other messages are sent through
78 // the default logging pipeline. 65 // the default logging pipeline.
79 // static 66 // static
80 bool FailureLogHelper::AddFailureForLogMessage(int severity, 67 bool FailureLogHelper::OnMessage(int severity,
81 const char* file, 68 const char* file,
82 int line, 69 int line,
83 size_t message_start, 70 size_t message_start,
84 const std::string& str) { 71 const std::string& str) {
85 if (severity == kViolationSeverity_ && !str.empty()) { 72 if (severity == kViolationSeverity_ && !str.empty()) {
86 // Remove the trailing newline, if present. 73 // Remove the trailing newline, if present.
87 size_t message_length = str.size() - message_start; 74 size_t message_length = str.size() - message_start;
88 if (*str.rbegin() == '\n') 75 if (*str.rbegin() == '\n')
89 --message_length; 76 --message_length;
90 ADD_FAILURE_AT(file, line) 77 ADD_FAILURE_AT(file, line)
91 << base::StringPiece(str.c_str() + message_start, message_length); 78 << base::StringPiece(str.c_str() + message_start, message_length);
92 return true; 79 return true;
93 } 80 }
94 81
95 if (old_message_handler_ != NULL)
96 return (old_message_handler_)(severity, file, line, message_start, str);
97
98 return false; 82 return false;
99 } 83 }
100 84
101 } // namespace 85 } // namespace
102 86
103 InstallationValidator::InstallationType ExpectValidInstallation( 87 InstallationValidator::InstallationType ExpectValidInstallation(
104 bool system_level) { 88 bool system_level) {
105 FailureLogHelper log_helper; 89 FailureLogHelper log_helper;
106 InstallationValidator::InstallationType found_type = 90 InstallationValidator::InstallationType found_type =
107 InstallationValidator::NO_PRODUCTS; 91 InstallationValidator::NO_PRODUCTS;
(...skipping 21 matching lines...) Expand all
129 } 113 }
130 114
131 void ExpectInstallationOfTypeForState( 115 void ExpectInstallationOfTypeForState(
132 const InstallationState& machine_state, 116 const InstallationState& machine_state,
133 bool system_level, 117 bool system_level,
134 InstallationValidator::InstallationType type) { 118 InstallationValidator::InstallationType type) {
135 EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level)); 119 EXPECT_EQ(type, ExpectValidInstallationForState(machine_state, system_level));
136 } 120 }
137 121
138 } // namespace installer 122 } // namespace installer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698