• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Logger.h"
25 
26 #include "arm_compute/core/Error.h"
27 
28 #include <memory>
29 
30 using namespace arm_compute::logging;
31 
Logger(std::string name,LogLevel log_level,std::shared_ptr<Printer> printer)32 Logger::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 
Logger(std::string name,LogLevel log_level,std::vector<std::shared_ptr<Printer>> printers)45 Logger::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 
Logger(std::string name,LogLevel log_level,std::vector<std::shared_ptr<Printer>> printers,std::vector<std::unique_ptr<IDecorator>> decorators)58 Logger::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 
log(LogLevel log_level,const std::string & msg)78 void 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 
set_log_level(LogLevel log_level)91 void Logger::set_log_level(LogLevel log_level)
92 {
93     _log_level = log_level;
94 }
95 
log_level() const96 LogLevel Logger::log_level() const
97 {
98     return _log_level;
99 }
100 
name() const101 std::string Logger::name() const
102 {
103     return _name;
104 }
105 
add_printer(std::shared_ptr<Printer> printer)106 void 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 
add_decorator(std::unique_ptr<IDecorator> decorator)112 void 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 
set_default_decorators()118 void Logger::set_default_decorators()
119 {
120     _decorators.emplace_back(std::make_unique<StringDecorator>(_name));
121     _decorators.emplace_back(std::make_unique<DateDecorator>());
122     _decorators.emplace_back(std::make_unique<LogLevelDecorator>());
123 }
124 
is_loggable(LogLevel log_level)125 bool Logger::is_loggable(LogLevel log_level)
126 {
127     return (log_level >= _log_level);
128 }
129 
decorate_log_msg(LogMsg & msg)130 void 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 
create_log_msg(const std::string & str,LogLevel log_level)139 std::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 
print_all(const std::string & msg)149 void Logger::print_all(const std::string &msg)
150 {
151     for(auto &p : _printers)
152     {
153         p->print(msg);
154     }
155 }
156