blob: d6681f817959c6d495da4be6039bcee220f45fc1 [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)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033 : _name(std::move(name)), _log_level(log_level), _printers({std::move(printer)}), _decorators()
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010034{
35 // Check printer
36 ARM_COMPUTE_ERROR_ON(printer == nullptr);
37
38 // Set default message decorators
39 set_default_decorators();
40}
41
42Logger::Logger(std::string name, LogLevel log_level, std::vector<std::shared_ptr<Printer>> printers)
43 : _name(std::move(name)), _log_level(log_level), _printers(std::move(printers)), _decorators()
44{
45 // Check printers
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 for (const auto &p : _printers)
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010047 {
48 ARM_COMPUTE_UNUSED(p);
49 ARM_COMPUTE_ERROR_ON(p == nullptr);
50 }
51 // Set default message decorators
52 set_default_decorators();
53}
54
55Logger::Logger(std::string name,
56 LogLevel log_level,
57 std::vector<std::shared_ptr<Printer>> printers,
58 std::vector<std::unique_ptr<IDecorator>> decorators)
59 : _name(std::move(name)), _log_level(log_level), _printers(std::move(printers)), _decorators(std::move(decorators))
60{
61 // Check printers
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062 for (const auto &p : _printers)
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010063 {
64 ARM_COMPUTE_UNUSED(p);
65 ARM_COMPUTE_ERROR_ON(p == nullptr);
66 }
67 // Check decorators
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 for (const auto &d : _decorators)
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010069 {
70 ARM_COMPUTE_UNUSED(d);
71 ARM_COMPUTE_ERROR_ON(d == nullptr);
72 }
73}
74
75void Logger::log(LogLevel log_level, const std::string &msg)
76{
77 // Return if message shouldn't be logged
78 // i.e. if log level does not match the logger's
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010079 if (!is_loggable(log_level))
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010080 {
81 return;
82 }
83
84 // Print message to all printers
85 print_all(create_log_msg(msg, log_level));
86}
87
88void Logger::set_log_level(LogLevel log_level)
89{
90 _log_level = log_level;
91}
92
93LogLevel Logger::log_level() const
94{
95 return _log_level;
96}
97
98std::string Logger::name() const
99{
100 return _name;
101}
102
103void Logger::add_printer(std::shared_ptr<Printer> printer)
104{
105 ARM_COMPUTE_ERROR_ON(printer == nullptr);
106 _printers.push_back(std::move(printer));
107}
108
109void Logger::add_decorator(std::unique_ptr<IDecorator> decorator)
110{
111 ARM_COMPUTE_ERROR_ON(decorator == nullptr);
112 _decorators.push_back(std::move(decorator));
113}
114
115void Logger::set_default_decorators()
116{
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000117 _decorators.emplace_back(std::make_unique<StringDecorator>(_name));
118 _decorators.emplace_back(std::make_unique<DateDecorator>());
119 _decorators.emplace_back(std::make_unique<LogLevelDecorator>());
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100120}
121
122bool Logger::is_loggable(LogLevel log_level)
123{
124 return (log_level >= _log_level);
125}
126
127void Logger::decorate_log_msg(LogMsg &msg)
128{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129 for (const auto &d : _decorators)
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100130 {
131 d->decorate(msg);
132 }
133 msg.raw_ += std::string(" ");
134}
135
136std::string Logger::create_log_msg(const std::string &str, LogLevel log_level)
137{
138 // Adding space string to avoid Android failures
139 LogMsg log_msg(" ", log_level);
140 decorate_log_msg(log_msg);
141 std::ostringstream ss;
142 ss << log_msg.raw_ << " " << str;
143 return ss.str();
144}
145
146void Logger::print_all(const std::string &msg)
147{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 for (auto &p : _printers)
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100149 {
150 p->print(msg);
151 }
Matthew Bentham92046462020-03-07 22:15:55 +0000152}