1 /*
2 * Copyright (c) 2017-2020 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #include "arm_compute/core/utils/logging/LoggerRegistry.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "support/Mutex.h"
28
29 using namespace arm_compute::logging;
30
31 /** Reserved logger used by the library */
32 std::set<std::string> LoggerRegistry::_reserved_loggers = { "CORE", "RUNTIME", "GRAPH" };
33
LoggerRegistry()34 LoggerRegistry::LoggerRegistry()
35 : _mtx(), _loggers()
36 {
37 }
38
get()39 LoggerRegistry &LoggerRegistry::get()
40 {
41 static LoggerRegistry _instance;
42 return _instance;
43 }
44
create_logger(const std::string & name,LogLevel log_level,const std::vector<std::shared_ptr<Printer>> & printers)45 void LoggerRegistry::create_logger(const std::string &name, LogLevel log_level, const std::vector<std::shared_ptr<Printer>> &printers)
46 {
47 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
48 if((_loggers.find(name) == _loggers.end()) && (_reserved_loggers.find(name) == _reserved_loggers.end()))
49 {
50 _loggers[name] = std::make_shared<Logger>(name, log_level, printers);
51 }
52 }
53
remove_logger(const std::string & name)54 void LoggerRegistry::remove_logger(const std::string &name)
55 {
56 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
57 if(_loggers.find(name) != _loggers.end())
58 {
59 _loggers.erase(name);
60 }
61 }
62
logger(const std::string & name)63 std::shared_ptr<Logger> LoggerRegistry::logger(const std::string &name)
64 {
65 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
66 return (_loggers.find(name) != _loggers.end()) ? _loggers[name] : nullptr;
67 }
68
create_reserved_loggers(LogLevel log_level,const std::vector<std::shared_ptr<Printer>> & printers)69 void LoggerRegistry::create_reserved_loggers(LogLevel log_level, const std::vector<std::shared_ptr<Printer>> &printers)
70 {
71 arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx);
72 for(const auto &r : _reserved_loggers)
73 {
74 if(_loggers.find(r) == _loggers.end())
75 {
76 _loggers[r] = std::make_shared<Logger>(r, log_level, printers);
77 }
78 }
79 }
80