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/Logger.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 27 | #include "support/MemorySupport.h" |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 28 | |
| 29 | using namespace arm_compute::logging; |
| 30 | |
| 31 | Logger::Logger(std::string name, LogLevel log_level, std::shared_ptr<Printer> printer) |
| 32 | : _name(std::move(name)), _log_level(log_level), _printers( |
| 33 | { |
| 34 | std::move(printer) |
| 35 | }), _decorators() |
| 36 | { |
| 37 | // Check printer |
| 38 | ARM_COMPUTE_ERROR_ON(printer == nullptr); |
| 39 | |
| 40 | // Set default message decorators |
| 41 | set_default_decorators(); |
| 42 | } |
| 43 | |
| 44 | Logger::Logger(std::string name, LogLevel log_level, std::vector<std::shared_ptr<Printer>> printers) |
| 45 | : _name(std::move(name)), _log_level(log_level), _printers(std::move(printers)), _decorators() |
| 46 | { |
| 47 | // Check printers |
| 48 | for(const auto &p : _printers) |
| 49 | { |
| 50 | ARM_COMPUTE_UNUSED(p); |
| 51 | ARM_COMPUTE_ERROR_ON(p == nullptr); |
| 52 | } |
| 53 | // Set default message decorators |
| 54 | set_default_decorators(); |
| 55 | } |
| 56 | |
| 57 | Logger::Logger(std::string name, |
| 58 | LogLevel log_level, |
| 59 | std::vector<std::shared_ptr<Printer>> printers, |
| 60 | std::vector<std::unique_ptr<IDecorator>> decorators) |
| 61 | : _name(std::move(name)), _log_level(log_level), _printers(std::move(printers)), _decorators(std::move(decorators)) |
| 62 | { |
| 63 | // Check printers |
| 64 | for(const auto &p : _printers) |
| 65 | { |
| 66 | ARM_COMPUTE_UNUSED(p); |
| 67 | ARM_COMPUTE_ERROR_ON(p == nullptr); |
| 68 | } |
| 69 | // Check decorators |
| 70 | for(const auto &d : _decorators) |
| 71 | { |
| 72 | ARM_COMPUTE_UNUSED(d); |
| 73 | ARM_COMPUTE_ERROR_ON(d == nullptr); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void Logger::log(LogLevel log_level, const std::string &msg) |
| 78 | { |
| 79 | // Return if message shouldn't be logged |
| 80 | // i.e. if log level does not match the logger's |
| 81 | if(!is_loggable(log_level)) |
| 82 | { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // Print message to all printers |
| 87 | print_all(create_log_msg(msg, log_level)); |
| 88 | } |
| 89 | |
| 90 | void Logger::set_log_level(LogLevel log_level) |
| 91 | { |
| 92 | _log_level = log_level; |
| 93 | } |
| 94 | |
| 95 | LogLevel Logger::log_level() const |
| 96 | { |
| 97 | return _log_level; |
| 98 | } |
| 99 | |
| 100 | std::string Logger::name() const |
| 101 | { |
| 102 | return _name; |
| 103 | } |
| 104 | |
| 105 | void Logger::add_printer(std::shared_ptr<Printer> printer) |
| 106 | { |
| 107 | ARM_COMPUTE_ERROR_ON(printer == nullptr); |
| 108 | _printers.push_back(std::move(printer)); |
| 109 | } |
| 110 | |
| 111 | void Logger::add_decorator(std::unique_ptr<IDecorator> decorator) |
| 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON(decorator == nullptr); |
| 114 | _decorators.push_back(std::move(decorator)); |
| 115 | } |
| 116 | |
| 117 | void Logger::set_default_decorators() |
| 118 | { |
| 119 | _decorators.emplace_back(support::cpp14::make_unique<StringDecorator>(_name)); |
| 120 | _decorators.emplace_back(support::cpp14::make_unique<DateDecorator>()); |
| 121 | _decorators.emplace_back(support::cpp14::make_unique<LogLevelDecorator>()); |
| 122 | } |
| 123 | |
| 124 | bool Logger::is_loggable(LogLevel log_level) |
| 125 | { |
| 126 | return (log_level >= _log_level); |
| 127 | } |
| 128 | |
| 129 | void Logger::decorate_log_msg(LogMsg &msg) |
| 130 | { |
| 131 | for(const auto &d : _decorators) |
| 132 | { |
| 133 | d->decorate(msg); |
| 134 | } |
| 135 | msg.raw_ += std::string(" "); |
| 136 | } |
| 137 | |
| 138 | std::string Logger::create_log_msg(const std::string &str, LogLevel log_level) |
| 139 | { |
| 140 | // Adding space string to avoid Android failures |
| 141 | LogMsg log_msg(" ", log_level); |
| 142 | decorate_log_msg(log_msg); |
| 143 | std::ostringstream ss; |
| 144 | ss << log_msg.raw_ << " " << str; |
| 145 | return ss.str(); |
| 146 | } |
| 147 | |
| 148 | void Logger::print_all(const std::string &msg) |
| 149 | { |
| 150 | for(auto &p : _printers) |
| 151 | { |
| 152 | p->print(msg); |
| 153 | } |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [diff] [blame] | 154 | } |