Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 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" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 27 | |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 28 | #include "support/Mutex.h" |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 29 | |
| 30 | using namespace arm_compute::logging; |
| 31 | |
| 32 | /** Reserved logger used by the library */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | std::set<std::string> LoggerRegistry::_reserved_loggers = {"CORE", "RUNTIME", "GRAPH"}; |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 34 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 35 | LoggerRegistry::LoggerRegistry() : _mtx(), _loggers() |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | LoggerRegistry &LoggerRegistry::get() |
| 40 | { |
| 41 | static LoggerRegistry _instance; |
| 42 | return _instance; |
| 43 | } |
| 44 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | void LoggerRegistry::create_logger(const std::string &name, |
| 46 | LogLevel log_level, |
| 47 | const std::vector<std::shared_ptr<Printer>> &printers) |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 48 | { |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 49 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | if ((_loggers.find(name) == _loggers.end()) && (_reserved_loggers.find(name) == _reserved_loggers.end())) |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 51 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 52 | _loggers[name] = std::make_shared<Logger>(name, log_level, printers); |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | void LoggerRegistry::remove_logger(const std::string &name) |
| 57 | { |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 58 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 59 | if (_loggers.find(name) != _loggers.end()) |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 60 | { |
| 61 | _loggers.erase(name); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | std::shared_ptr<Logger> LoggerRegistry::logger(const std::string &name) |
| 66 | { |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 67 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 68 | return (_loggers.find(name) != _loggers.end()) ? _loggers[name] : nullptr; |
| 69 | } |
| 70 | |
Michalis Spyrou | 299fdd3 | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 71 | void LoggerRegistry::create_reserved_loggers(LogLevel log_level, const std::vector<std::shared_ptr<Printer>> &printers) |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 72 | { |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 73 | arm_compute::lock_guard<arm_compute::Mutex> lock(_mtx); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | for (const auto &r : _reserved_loggers) |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 75 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | if (_loggers.find(r) == _loggers.end()) |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 77 | { |
| 78 | _loggers[r] = std::make_shared<Logger>(r, log_level, printers); |
| 79 | } |
| 80 | } |
| 81 | } |