xref: /aosp_15_r20/external/ComputeLibrary/src/common/utils/Log.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2021 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 SRC_COMMON_LOG_H
25 #define SRC_COMMON_LOG_H
26 
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/utils/logging/Macros.h"
29 #include "utils/TypePrinter.h"
30 
31 #ifdef ARM_COMPUTE_LOGGING_ENABLED
32 /** Create a logger
33  *
34  * @note It will eventually create all default loggers in don't exist
35  */
36 #define ARM_COMPUTE_CREATE_ACL_LOGGER()                                                                                        \
37     do                                                                                                                         \
38     {                                                                                                                          \
39         if(arm_compute::logging::LoggerRegistry::get().logger("ComputeLibrary") == nullptr)                                    \
40         {                                                                                                                      \
41             arm_compute::logging::LoggerRegistry::get().create_logger("ComputeLibrary", arm_compute::logging::LogLevel::INFO); \
42         }                                                                                                                      \
43     } while(false)
44 #else /* ARM_COMPUTE_LOGGING_ENABLED */
45 #define ARM_COMPUTE_CREATE_ACL_LOGGER()
46 #endif /* ARM_COMPUTE_LOGGING_ENABLED */
47 
48 /** Log a message to the logger
49  *
50  * @param[in] log_level Logging level
51  * @param[in] msg       Message to log
52  */
53 #define ARM_COMPUTE_LOG_MSG_ACL(log_level, msg)                \
54     do                                                         \
55     {                                                          \
56         ARM_COMPUTE_CREATE_ACL_LOGGER();                       \
57         ARM_COMPUTE_LOG_MSG("ComputeLibrary", log_level, msg); \
58     } while(false)
59 
60 /** Log a message with format to the logger
61  *
62  * @param[in] log_level Logging level
63  * @param[in] fmt       String format (printf style)
64  * @param[in] ...       Message arguments
65  */
66 #define ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(log_level, fmt, ...)                        \
67     do                                                                                  \
68     {                                                                                   \
69         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                \
70         ARM_COMPUTE_LOG_MSG_WITH_FORMAT("ComputeLibrary", log_level, fmt, __VA_ARGS__); \
71     } while(false)
72 
73 /** Log an error message to the logger
74  *
75  * @param[in] msg Message to log
76  */
77 #define ARM_COMPUTE_LOG_ERROR_ACL(msg)                                                     \
78     do                                                                                     \
79     {                                                                                      \
80         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                   \
81         ARM_COMPUTE_LOG_MSG("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \
82     } while(false)
83 
84 /** Log an error message to the logger with function name before the message
85  *
86  * @param[in] msg Message to log
87  */
88 #define ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL(msg)                                                     \
89     do                                                                                                   \
90     {                                                                                                    \
91         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                                 \
92         ARM_COMPUTE_LOG_MSG_WITH_FUNCNAME("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \
93     } while(false)
94 
95 /** Log an information message to the logger with function name before the message
96  *
97  * @param[in] msg Message to log
98  */
99 #define ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(msg)                                                     \
100     do                                                                                                  \
101     {                                                                                                   \
102         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                                \
103         ARM_COMPUTE_LOG_MSG_WITH_FUNCNAME("ComputeLibrary", arm_compute::logging::LogLevel::INFO, msg); \
104     } while(false)
105 
106 /** Function template specialization for the out of bound element at index = tuple_size
107  *
108  * @param[in,out] data_registry   Reference to the input parameters data in a string format
109  * @param[in]     in_params_tuple Tuple of different input data types
110  */
111 template <std::size_t Index, typename... Tp>
112 inline typename std::enable_if<Index == sizeof...(Tp), void>::type
logParamsImpl(std::vector<std::string> & data_registry,const std::tuple<Tp...> & in_params_tuple)113 logParamsImpl(std::vector<std::string> &data_registry, const std::tuple<Tp...> &in_params_tuple)
114 {
115     // Because it is out of bound index so do nothing
116     ARM_COMPUTE_UNUSED(data_registry);
117     ARM_COMPUTE_UNUSED(in_params_tuple);
118 }
119 
120 /** Function template to iterate over all input parameters tuple at compile time:
121  *
122  * @param[in,out] data_registry   Reference to a vector of input parameters data in a string format
123  * @param[in]     in_params_tuple Constant reference to a tuple of different input data types
124  */
125 template <std::size_t Index, typename... Tp>
126 inline typename std::enable_if < Index<sizeof...(Tp), void>::type
127 logParamsImpl(std::vector<std::string> &data_registry, const std::tuple<Tp...> &in_params_tuple)
128 {
129     data_registry.push_back(arm_compute::to_string(std::get<Index>(in_params_tuple)));
130     // Unfold the next tuple element
131     logParamsImpl < Index + 1, Tp... > (data_registry, in_params_tuple);
132 }
133 
134 /** Function Template with variable number of inputs to collect all the passed parameters from
135  *  the logging macro ARM_COMPUTE_LOG_PARAMS(...)
136  *
137  * @param[in] ...ins The input parameters in the variadic template, taken by universal references Ts.. &&, (not by value)
138  *                   to avoid detecting T as an abstract data type when passing any of these parameters as an L-value
139  *                   reference to an abstract type.
140  *
141  * @return  Vector of the parameters' data in a string format
142  */
143 template <typename... Ts>
logParams(Ts &&...ins)144 const std::vector<std::string> logParams(Ts &&... ins)
145 {
146     std::vector<std::string> data_registry{};
147     std::tuple<Ts...>        in_params_tuple{ ins... };
148 
149     // Start logging the tuple elements, starting from 0 to tuple_size-1
150     logParamsImpl<0>(data_registry, in_params_tuple);
151     return data_registry;
152 }
153 
154 /** Inline function to parse the input parameters string passed from strignizing of the variadic macro input
155  *  #__VA_ARGS__.
156  *  It is Inline to avoid the redefinition of this function each time this header is included
157  *
158  * @param[in] in_params_str Constant reference to a string consists of the names of the input parameters provided
159  *                          as:ARM_COMPUTE_LOG_PARAMS(src0, src1) the params_names = "src0, src1"
160  *
161  * @return  Vector of strings containing all the names of the input parameters
162  */
getParamsNames(const std::string & in_params_str)163 inline const std::vector<std::string> getParamsNames(const std::string &in_params_str)
164 {
165     std::stringstream ss(in_params_str);
166 
167     // Vector containing all the names of the input parameters
168     std::vector<std::string> names;
169     std::string              temp;
170 
171     // Usually the input parameters string would be name of parameters separated
172     // by ',' e.g. "src0, src1, policy"
173     while(std::getline(ss, temp, ','))
174     {
175         names.push_back(temp);
176     }
177     for(auto &name : names)
178     {
179         // Totally get rid of white space characters
180         name.erase(std::remove(name.begin(), name.end(), ' '), name.end());
181     }
182     return names;
183 }
184 
185 /** It constructs the log message to be displayed by the logger by writing each parameter name and its
186  *  corresponding data info string.
187  *
188  * @param[in] params_names  Constant reference to a string consists of the the input parameters' names
189  *                          provided e.g.: ARM_COMPUTE_LOG_PARAMS(src0, src1) then params_names = "src0, src1"
190  * @param[in] data_registry Constant reference to a registry of all parameters' data in string format,
191  *                          stringnized by arm_compute::to_string()
192  *
193  * @return  Log message string to be displayed
194  */
constructDataLog(const std::vector<std::string> & params_names,const std::vector<std::string> & data_registry)195 inline const std::string constructDataLog(const std::vector<std::string> &params_names,
196                                           const std::vector<std::string> &data_registry)
197 {
198     std::string dataLog = "\n ";
199     ARM_COMPUTE_ERROR_ON(params_names.size() != data_registry.size());
200     for(uint8_t i = 0; i < params_names.size(); ++i)
201     {
202         dataLog += params_names[i] + ": " + data_registry.at(i) + "\n ";
203     }
204 
205     return dataLog;
206 }
207 
208 /** Macro for logging input Parameters from any function.
209  *  It detects the input parameters names, and their corresponding values before stringizing them using
210  *  the overloaded arm_compute::to_string() type printer. Finally, displayed using the printer configured
211  *  in the logger.
212  *
213  * @param[in] ... Input parameters
214  */
215 #define ARM_COMPUTE_LOG_PARAMS(...)                                                           \
216     do                                                                                        \
217     {                                                                                         \
218         ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(constructDataLog(getParamsNames(#__VA_ARGS__), \
219                                                                 logParams(__VA_ARGS__)));     \
220     } while(false)
221 #endif /* SRC_COMMON_LOG_H */
222