Compute Library
 21.02
Logger.cpp
Go to the documentation of this file.
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  */
25 
26 #include "arm_compute/core/Error.h"
27 
28 #include <memory>
29 
30 using namespace arm_compute::logging;
31 
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 
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  {
52  ARM_COMPUTE_ERROR_ON(p == nullptr);
53  }
54  // Set default message decorators
55  set_default_decorators();
56 }
57 
58 Logger::Logger(std::string name,
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  {
68  ARM_COMPUTE_ERROR_ON(p == nullptr);
69  }
70  // Check decorators
71  for(const auto &d : _decorators)
72  {
74  ARM_COMPUTE_ERROR_ON(d == nullptr);
75  }
76 }
77 
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 
92 {
93  _log_level = log_level;
94 }
95 
97 {
98  return _log_level;
99 }
100 
101 std::string Logger::name() const
102 {
103  return _name;
104 }
105 
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 
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 
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 
125 bool Logger::is_loggable(LogLevel log_level)
126 {
127  return (log_level >= _log_level);
128 }
129 
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 
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 
149 void Logger::print_all(const std::string &msg)
150 {
151  for(auto &p : _printers)
152  {
153  p->print(msg);
154  }
155 }
void add_decorator(std::unique_ptr< IDecorator > decorator)
Adds a log message decorator to the logger.
Definition: Logger.cpp:112
std::stringstream ss(mlgo_str)
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
void log(LogLevel log_level, const std::string &msg)
Logs a message.
Definition: Logger.cpp:78
void set_log_level(LogLevel log_level)
Sets log level of the logger.
Definition: Logger.cpp:91
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
void add_printer(std::shared_ptr< Printer > printer)
Adds a printer to the logger.
Definition: Logger.cpp:106
const char * name
std::string raw_
Log message.
Definition: Types.h:62
LogLevel
Logging level enumeration.
Definition: Types.h:34
Logger(std::string name, LogLevel log_level, std::shared_ptr< Printer > printer)
Default Constructor.
Definition: Logger.cpp:32
LogLevel log_level() const
Returns logger&#39;s log level.
Definition: Logger.cpp:96
std::string name() const
Returns logger&#39;s name.
Definition: Logger.cpp:101