• 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 #include "ws_logger.h"
17 
18 #include "macros.h"
19 #include "utils/logger.h"
20 
21 #include "websocketpp/logger/levels.hpp"
22 
23 namespace panda::tooling::inspector {
set_channels(Level channels)24 void WsLogger::set_channels(Level channels)
25 {
26     if (channels != 0) {
27         dynamic_channels_ |= channels & static_channels_;
28     } else {
29         dynamic_channels_ = 0;
30     }
31 }
32 
write(Level channel,const std::string & string) const33 void WsLogger::write(Level channel, const std::string &string) const
34 {
35     if (!dynamic_test(channel)) {
36         return;
37     }
38 
39     auto level = channel_log_level(channel);
40     auto component = Logger::Component::DEBUGGER;
41 
42     if (!Logger::IsLoggingOnOrAbort(level, component)) {
43         return;
44     }
45 
46 #ifndef NDEBUG
47     if (Logger::IsMessageSuppressed(level, component)) {
48         return;
49     }
50 #endif
51 
52     Logger::Message message(level, component, false);
53     auto &ms = message.GetStream();
54 
55     if (channel_type_ == websocketpp::log::channel_type_hint::access) {
56         ms << websocketpp::log::alevel::channel_name(channel) << ": ";
57     }
58 
59     ms << string;
60 }
61 
channel_log_level(Level channel) const62 Logger::Level WsLogger::channel_log_level(Level channel) const
63 {
64     if (channel_type_ != websocketpp::log::channel_type_hint::error) {
65         return Logger::Level::INFO;
66     }
67 
68     switch (channel) {
69         case websocketpp::log::elevel::devel:
70             return Logger::Level::DEBUG;
71 
72         case websocketpp::log::elevel::library:
73         case websocketpp::log::elevel::info:
74             return Logger::Level::INFO;
75 
76         case websocketpp::log::elevel::warn:
77             return Logger::Level::WARNING;
78 
79         case websocketpp::log::elevel::rerror:
80             return Logger::Level::ERROR;
81 
82         case websocketpp::log::elevel::fatal:
83             return Logger::Level::FATAL;
84 
85         default:
86             UNREACHABLE();
87     }
88 }
89 }  // namespace panda::tooling::inspector
90