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