• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PANDA_TOOLING_INSPECTOR_TEST_TEST_LOGGER_H
17 #define PANDA_TOOLING_INSPECTOR_TEST_TEST_LOGGER_H
18 
19 #include "utils/logger.h"
20 
21 #include "gmock/gmock.h"
22 
23 #include <sstream>
24 #include <string>
25 #include <string_view>
26 
27 namespace panda::tooling::inspector::test {
28 class TestLogger : public Logger {
29 public:
30     enum Flag : unsigned {
31         FAIL_ON_ERROR = 0b001,
32         OUTPUT_ON_FAIL = 0b010,
33         OUTPUT_UNBUFFERED = 0b100,
34     };
35 
36     explicit TestLogger(unsigned flags = 0);
37     ~TestLogger() override;
38     NO_COPY_SEMANTIC(TestLogger);
39     NO_MOVE_SEMANTIC(TestLogger);
40 
SetFlag(Flag flag,bool state)41     void SetFlag(Flag flag, bool state)
42     {
43         flags_ = state ? flags_ | flag : flags_ & ~flag;
44     }
45 
46     std::string_view SetPrefix(std::string_view prefix = {})
47     {
48         prefix_.swap(prefix);
49         return prefix;
50     }
51 
52     MOCK_METHOD(void, LogLineInternal, (Level, Component, const std::string &), (override));
53 
54 private:
55     std::string FormatLogLine(Level level, Component component, const std::string &message) const;
SyncOutputResource()56     void SyncOutputResource() override {}
57 
58     unsigned flags_;
59     std::string_view prefix_;
60     Logger *oldLogger_;
61     std::stringstream log_;
62 };
63 }  // namespace panda::tooling::inspector::test
64 
65 #endif  // PANDA_TOOLING_INSPECTOR_TEST_TEST_LOGGER_H
66