Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2024 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include "ClientLogControl.h"
21 : #include "RialtoClientLogging.h"
22 :
23 : namespace
24 : {
25 : const std::vector<RIALTO_COMPONENT> kAllComponentsToLog{RIALTO_COMPONENT_CLIENT, RIALTO_COMPONENT_IPC,
26 : RIALTO_COMPONENT_COMMON};
27 :
28 8 : firebolt::rialto::IClientLogHandler::Level convertLevel(const RIALTO_DEBUG_LEVEL &level)
29 : {
30 8 : switch (level)
31 : {
32 0 : case RIALTO_DEBUG_LEVEL_FATAL:
33 0 : return firebolt::rialto::IClientLogHandler::Level::Fatal;
34 3 : case RIALTO_DEBUG_LEVEL_ERROR:
35 3 : return firebolt::rialto::IClientLogHandler::Level::Error;
36 1 : case RIALTO_DEBUG_LEVEL_WARNING:
37 1 : return firebolt::rialto::IClientLogHandler::Level::Warning;
38 0 : case RIALTO_DEBUG_LEVEL_MILESTONE:
39 0 : return firebolt::rialto::IClientLogHandler::Level::Milestone;
40 4 : case RIALTO_DEBUG_LEVEL_INFO:
41 4 : return firebolt::rialto::IClientLogHandler::Level::Info;
42 0 : case RIALTO_DEBUG_LEVEL_DEBUG:
43 0 : return firebolt::rialto::IClientLogHandler::Level::Debug;
44 0 : case RIALTO_DEBUG_LEVEL_EXTERNAL:
45 0 : return firebolt::rialto::IClientLogHandler::Level::External;
46 0 : case RIALTO_DEBUG_LEVEL_DEFAULT:
47 0 : return firebolt::rialto::IClientLogHandler::Level::Milestone;
48 : }
49 0 : return firebolt::rialto::IClientLogHandler::Level::Debug;
50 : }
51 : } // namespace
52 :
53 : namespace firebolt::rialto
54 : {
55 3 : std::shared_ptr<IClientLogControlFactory> IClientLogControlFactory::createFactory()
56 : {
57 3 : return client::ClientLogControlFactory::createFactory();
58 : }
59 : } // namespace firebolt::rialto
60 :
61 : namespace firebolt::rialto::client
62 : {
63 8 : std::shared_ptr<IClientLogControlFactory> ClientLogControlFactory::createFactory()
64 : {
65 8 : return std::make_shared<client::ClientLogControlFactory>();
66 : }
67 :
68 8 : IClientLogControl &ClientLogControlFactory::createClientLogControl()
69 : {
70 8 : static std::unique_ptr<IClientLogControl> clientLogControl{std::make_unique<ClientLogControl>()};
71 8 : return *clientLogControl;
72 : }
73 :
74 1 : ClientLogControl::ClientLogControl()
75 : {
76 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
77 : }
78 :
79 2 : ClientLogControl::~ClientLogControl()
80 : {
81 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
82 1 : std::unique_lock<std::mutex> lock{m_logHandlerMutex};
83 1 : if (m_logHandler)
84 0 : cancelLogHandler();
85 2 : }
86 :
87 9 : bool ClientLogControl::registerLogHandler(const std::shared_ptr<IClientLogHandler> &handler, bool ignoreLogLevels)
88 : {
89 9 : std::unique_lock<std::mutex> lock{m_logHandlerMutex};
90 :
91 9 : if (m_logHandler)
92 : {
93 4 : if (handler)
94 : {
95 1 : RIALTO_CLIENT_LOG_WARN("Replacing old log handler");
96 : }
97 4 : cancelLogHandler();
98 : }
99 :
100 9 : m_logHandler = handler;
101 :
102 9 : if (!handler)
103 5 : return true;
104 :
105 16 : for (auto component : kAllComponentsToLog)
106 : {
107 12 : if (firebolt::rialto::logging::setLogHandler(component,
108 24 : std::bind(&ClientLogControl::forwardLog, this, component,
109 : std::placeholders::_1, std::placeholders::_2,
110 : std::placeholders::_3, std::placeholders::_4,
111 : std::placeholders::_5, std::placeholders::_6),
112 12 : ignoreLogLevels) !=
113 : firebolt::rialto::logging::RIALTO_LOGGING_STATUS_OK)
114 : {
115 0 : RIALTO_CLIENT_LOG_WARN("Unable to register log handler");
116 0 : cancelLogHandler();
117 0 : return false;
118 : }
119 : }
120 4 : return true;
121 9 : }
122 :
123 4 : void ClientLogControl::cancelLogHandler()
124 : {
125 4 : RIALTO_CLIENT_LOG_INFO("Cancelling log handler");
126 16 : for (auto component : kAllComponentsToLog)
127 : {
128 12 : firebolt::rialto::logging::setLogHandler(component, nullptr, false);
129 : }
130 4 : m_logHandler = nullptr;
131 : }
132 :
133 8 : void ClientLogControl::forwardLog(RIALTO_COMPONENT component, RIALTO_DEBUG_LEVEL level, const char *file, int line,
134 : const char *function, const char *message, std::size_t messageLen)
135 : {
136 : // Take a local copy to ensure thread safety
137 8 : std::shared_ptr<IClientLogHandler> logHandler{m_logHandler};
138 8 : if (logHandler)
139 : {
140 8 : logHandler->log(convertLevel(level), std::string(file), line, std::string(function), std::string(message));
141 : }
142 : }
143 : }; // namespace firebolt::rialto::client
|