xref: /aosp_15_r20/external/ComputeLibrary/arm_compute/core/utils/logging/Logger.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2019 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 #ifndef ARM_COMPUTE_LOGGING_LOGGER_H
25 #define ARM_COMPUTE_LOGGING_LOGGER_H
26 
27 #include "arm_compute/core/utils/logging/Helpers.h"
28 #include "arm_compute/core/utils/logging/IPrinter.h"
29 #include "arm_compute/core/utils/logging/LogMsgDecorators.h"
30 #include "arm_compute/core/utils/logging/Types.h"
31 
32 #include <memory>
33 #include <sstream>
34 #include <string>
35 #include <vector>
36 
37 namespace arm_compute
38 {
39 namespace logging
40 {
41 /** Logger class */
42 class Logger
43 {
44 public:
45     /** Default Constructor
46      *
47      * @param[in] name      Name of the logger
48      * @param[in] log_level Logger log level
49      * @param[in] printer   Printer to push the messages
50      */
51     Logger(std::string name, LogLevel log_level, std::shared_ptr<Printer> printer);
52     /** Default Constructor
53      *
54      * @param[in] name      Name of the logger
55      * @param[in] log_level Logger log level
56      * @param[in] printers  Printers to push the messages
57      */
58     Logger(std::string name, LogLevel log_level, std::vector<std::shared_ptr<Printer>> printers = {});
59     /** Default Constructor
60      *
61      * @param[in] name       Name of the logger
62      * @param[in] log_level  Logger log level
63      * @param[in] printers   Printers to push the messages
64      * @param[in] decorators Message decorators, which append information in the logged message
65      */
66     Logger(std::string                              name,
67            LogLevel                                 log_level,
68            std::vector<std::shared_ptr<Printer>>    printers,
69            std::vector<std::unique_ptr<IDecorator>> decorators);
70     /** Allow instances of this class to be moved */
71     Logger(Logger &&) = default;
72     /** Prevent instances of this class from being copied (As this class contains pointers) */
73     Logger(const Logger &) = delete;
74     /** Prevent instances of this class from being copied (As this class contains pointers) */
75     Logger &operator=(const Logger &) = delete;
76     /** Allow instances of this class to be moved */
77     Logger &operator=(Logger &&) = default;
78     /** Logs a message
79      *
80      * @param[in] log_level Log level of the message
81      * @param[in] msg       Message to log
82      */
83     void log(LogLevel log_level, const std::string &msg);
84     /** Logs a formatted message
85      *
86      * @param[in] log_level Log level of the message
87      * @param[in] fmt       Message format
88      * @param[in] args      Message arguments
89      */
90     template <typename... Ts>
91     void log(LogLevel log_level, const std::string &fmt, Ts &&... args);
92     /** Sets log level of the logger
93      *
94      * @warning Not thread-safe
95      *
96      * @param[in] log_level Log level to set
97      */
98     void set_log_level(LogLevel log_level);
99     /** Returns logger's log level
100      *
101      * @return Logger's log level
102      */
103     LogLevel log_level() const;
104     /** Returns logger's name
105      *
106      * @return Logger's name
107      */
108     std::string name() const;
109     /** Adds a printer to the logger
110      *
111      * @warning Not thread-safe
112      *
113      * @param[in] printer
114      */
115     void add_printer(std::shared_ptr<Printer> printer);
116     /** Adds a log message decorator to the logger
117      *
118      * @warning Not thread-safe
119      *
120      * @param[in] decorator
121      */
122     void add_decorator(std::unique_ptr<IDecorator> decorator);
123 
124 private:
125     /** Set default message decorators */
126     void set_default_decorators();
127     /** Checks if a message should be logged depending
128      *  on the message log level and the loggers one
129      *
130      * @param[in] log_level Log level
131      *
132      * @return True if message should be logged else false
133      */
134     bool is_loggable(LogLevel log_level);
135     /** Decorate log message
136      *
137      * @param[in] Log message to decorate
138      */
139     void decorate_log_msg(LogMsg &msg);
140     /** Creates final log message by creating the prefix
141      *
142      * @param[in] str       Log message
143      * @param[in] log_level Message's log level
144      *
145      * @return Final log message to print
146      */
147     std::string create_log_msg(const std::string &str, LogLevel log_level);
148     /** Prints the message to all the printers
149      *
150      * @param[in] msg Message to print
151      */
152     void print_all(const std::string &msg);
153 
154 private:
155     std::string                              _name;
156     LogLevel                                 _log_level;
157     std::vector<std::shared_ptr<Printer>>    _printers;
158     std::vector<std::unique_ptr<IDecorator>> _decorators;
159 };
160 
161 template <typename... Ts>
log(LogLevel log_level,const std::string & fmt,Ts &&...args)162 inline void Logger::log(LogLevel log_level, const std::string &fmt, Ts &&... args)
163 {
164     // Return if message shouldn't be logged
165     // i.e. if log level does not match the logger's
166     if(!is_loggable(log_level))
167     {
168         return;
169     }
170 
171     // Print message to all printers
172     print_all(create_log_msg(string_with_format(fmt, args...), log_level));
173 }
174 } // namespace logging
175 } // namespace arm_compute
176 #endif /* ARM_COMPUTE_LOGGING_LOGGER_H */
177