xref: /aosp_15_r20/external/ComputeLibrary/src/common/utils/Log.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #ifndef SRC_COMMON_LOG_H
25*c217d954SCole Faust #define SRC_COMMON_LOG_H
26*c217d954SCole Faust 
27*c217d954SCole Faust #include "arm_compute/core/Error.h"
28*c217d954SCole Faust #include "arm_compute/core/utils/logging/Macros.h"
29*c217d954SCole Faust #include "utils/TypePrinter.h"
30*c217d954SCole Faust 
31*c217d954SCole Faust #ifdef ARM_COMPUTE_LOGGING_ENABLED
32*c217d954SCole Faust /** Create a logger
33*c217d954SCole Faust  *
34*c217d954SCole Faust  * @note It will eventually create all default loggers in don't exist
35*c217d954SCole Faust  */
36*c217d954SCole Faust #define ARM_COMPUTE_CREATE_ACL_LOGGER()                                                                                        \
37*c217d954SCole Faust     do                                                                                                                         \
38*c217d954SCole Faust     {                                                                                                                          \
39*c217d954SCole Faust         if(arm_compute::logging::LoggerRegistry::get().logger("ComputeLibrary") == nullptr)                                    \
40*c217d954SCole Faust         {                                                                                                                      \
41*c217d954SCole Faust             arm_compute::logging::LoggerRegistry::get().create_logger("ComputeLibrary", arm_compute::logging::LogLevel::INFO); \
42*c217d954SCole Faust         }                                                                                                                      \
43*c217d954SCole Faust     } while(false)
44*c217d954SCole Faust #else /* ARM_COMPUTE_LOGGING_ENABLED */
45*c217d954SCole Faust #define ARM_COMPUTE_CREATE_ACL_LOGGER()
46*c217d954SCole Faust #endif /* ARM_COMPUTE_LOGGING_ENABLED */
47*c217d954SCole Faust 
48*c217d954SCole Faust /** Log a message to the logger
49*c217d954SCole Faust  *
50*c217d954SCole Faust  * @param[in] log_level Logging level
51*c217d954SCole Faust  * @param[in] msg       Message to log
52*c217d954SCole Faust  */
53*c217d954SCole Faust #define ARM_COMPUTE_LOG_MSG_ACL(log_level, msg)                \
54*c217d954SCole Faust     do                                                         \
55*c217d954SCole Faust     {                                                          \
56*c217d954SCole Faust         ARM_COMPUTE_CREATE_ACL_LOGGER();                       \
57*c217d954SCole Faust         ARM_COMPUTE_LOG_MSG("ComputeLibrary", log_level, msg); \
58*c217d954SCole Faust     } while(false)
59*c217d954SCole Faust 
60*c217d954SCole Faust /** Log a message with format to the logger
61*c217d954SCole Faust  *
62*c217d954SCole Faust  * @param[in] log_level Logging level
63*c217d954SCole Faust  * @param[in] fmt       String format (printf style)
64*c217d954SCole Faust  * @param[in] ...       Message arguments
65*c217d954SCole Faust  */
66*c217d954SCole Faust #define ARM_COMPUTE_LOG_MSG_WITH_FORMAT_ACL(log_level, fmt, ...)                        \
67*c217d954SCole Faust     do                                                                                  \
68*c217d954SCole Faust     {                                                                                   \
69*c217d954SCole Faust         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                \
70*c217d954SCole Faust         ARM_COMPUTE_LOG_MSG_WITH_FORMAT("ComputeLibrary", log_level, fmt, __VA_ARGS__); \
71*c217d954SCole Faust     } while(false)
72*c217d954SCole Faust 
73*c217d954SCole Faust /** Log an error message to the logger
74*c217d954SCole Faust  *
75*c217d954SCole Faust  * @param[in] msg Message to log
76*c217d954SCole Faust  */
77*c217d954SCole Faust #define ARM_COMPUTE_LOG_ERROR_ACL(msg)                                                     \
78*c217d954SCole Faust     do                                                                                     \
79*c217d954SCole Faust     {                                                                                      \
80*c217d954SCole Faust         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                   \
81*c217d954SCole Faust         ARM_COMPUTE_LOG_MSG("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \
82*c217d954SCole Faust     } while(false)
83*c217d954SCole Faust 
84*c217d954SCole Faust /** Log an error message to the logger with function name before the message
85*c217d954SCole Faust  *
86*c217d954SCole Faust  * @param[in] msg Message to log
87*c217d954SCole Faust  */
88*c217d954SCole Faust #define ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL(msg)                                                     \
89*c217d954SCole Faust     do                                                                                                   \
90*c217d954SCole Faust     {                                                                                                    \
91*c217d954SCole Faust         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                                 \
92*c217d954SCole Faust         ARM_COMPUTE_LOG_MSG_WITH_FUNCNAME("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \
93*c217d954SCole Faust     } while(false)
94*c217d954SCole Faust 
95*c217d954SCole Faust /** Log an information message to the logger with function name before the message
96*c217d954SCole Faust  *
97*c217d954SCole Faust  * @param[in] msg Message to log
98*c217d954SCole Faust  */
99*c217d954SCole Faust #define ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(msg)                                                     \
100*c217d954SCole Faust     do                                                                                                  \
101*c217d954SCole Faust     {                                                                                                   \
102*c217d954SCole Faust         ARM_COMPUTE_CREATE_ACL_LOGGER();                                                                \
103*c217d954SCole Faust         ARM_COMPUTE_LOG_MSG_WITH_FUNCNAME("ComputeLibrary", arm_compute::logging::LogLevel::INFO, msg); \
104*c217d954SCole Faust     } while(false)
105*c217d954SCole Faust 
106*c217d954SCole Faust /** Function template specialization for the out of bound element at index = tuple_size
107*c217d954SCole Faust  *
108*c217d954SCole Faust  * @param[in,out] data_registry   Reference to the input parameters data in a string format
109*c217d954SCole Faust  * @param[in]     in_params_tuple Tuple of different input data types
110*c217d954SCole Faust  */
111*c217d954SCole Faust template <std::size_t Index, typename... Tp>
112*c217d954SCole Faust 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*c217d954SCole Faust logParamsImpl(std::vector<std::string> &data_registry, const std::tuple<Tp...> &in_params_tuple)
114*c217d954SCole Faust {
115*c217d954SCole Faust     // Because it is out of bound index so do nothing
116*c217d954SCole Faust     ARM_COMPUTE_UNUSED(data_registry);
117*c217d954SCole Faust     ARM_COMPUTE_UNUSED(in_params_tuple);
118*c217d954SCole Faust }
119*c217d954SCole Faust 
120*c217d954SCole Faust /** Function template to iterate over all input parameters tuple at compile time:
121*c217d954SCole Faust  *
122*c217d954SCole Faust  * @param[in,out] data_registry   Reference to a vector of input parameters data in a string format
123*c217d954SCole Faust  * @param[in]     in_params_tuple Constant reference to a tuple of different input data types
124*c217d954SCole Faust  */
125*c217d954SCole Faust template <std::size_t Index, typename... Tp>
126*c217d954SCole Faust inline typename std::enable_if < Index<sizeof...(Tp), void>::type
127*c217d954SCole Faust logParamsImpl(std::vector<std::string> &data_registry, const std::tuple<Tp...> &in_params_tuple)
128*c217d954SCole Faust {
129*c217d954SCole Faust     data_registry.push_back(arm_compute::to_string(std::get<Index>(in_params_tuple)));
130*c217d954SCole Faust     // Unfold the next tuple element
131*c217d954SCole Faust     logParamsImpl < Index + 1, Tp... > (data_registry, in_params_tuple);
132*c217d954SCole Faust }
133*c217d954SCole Faust 
134*c217d954SCole Faust /** Function Template with variable number of inputs to collect all the passed parameters from
135*c217d954SCole Faust  *  the logging macro ARM_COMPUTE_LOG_PARAMS(...)
136*c217d954SCole Faust  *
137*c217d954SCole Faust  * @param[in] ...ins The input parameters in the variadic template, taken by universal references Ts.. &&, (not by value)
138*c217d954SCole Faust  *                   to avoid detecting T as an abstract data type when passing any of these parameters as an L-value
139*c217d954SCole Faust  *                   reference to an abstract type.
140*c217d954SCole Faust  *
141*c217d954SCole Faust  * @return  Vector of the parameters' data in a string format
142*c217d954SCole Faust  */
143*c217d954SCole Faust template <typename... Ts>
logParams(Ts &&...ins)144*c217d954SCole Faust const std::vector<std::string> logParams(Ts &&... ins)
145*c217d954SCole Faust {
146*c217d954SCole Faust     std::vector<std::string> data_registry{};
147*c217d954SCole Faust     std::tuple<Ts...>        in_params_tuple{ ins... };
148*c217d954SCole Faust 
149*c217d954SCole Faust     // Start logging the tuple elements, starting from 0 to tuple_size-1
150*c217d954SCole Faust     logParamsImpl<0>(data_registry, in_params_tuple);
151*c217d954SCole Faust     return data_registry;
152*c217d954SCole Faust }
153*c217d954SCole Faust 
154*c217d954SCole Faust /** Inline function to parse the input parameters string passed from strignizing of the variadic macro input
155*c217d954SCole Faust  *  #__VA_ARGS__.
156*c217d954SCole Faust  *  It is Inline to avoid the redefinition of this function each time this header is included
157*c217d954SCole Faust  *
158*c217d954SCole Faust  * @param[in] in_params_str Constant reference to a string consists of the names of the input parameters provided
159*c217d954SCole Faust  *                          as:ARM_COMPUTE_LOG_PARAMS(src0, src1) the params_names = "src0, src1"
160*c217d954SCole Faust  *
161*c217d954SCole Faust  * @return  Vector of strings containing all the names of the input parameters
162*c217d954SCole Faust  */
getParamsNames(const std::string & in_params_str)163*c217d954SCole Faust inline const std::vector<std::string> getParamsNames(const std::string &in_params_str)
164*c217d954SCole Faust {
165*c217d954SCole Faust     std::stringstream ss(in_params_str);
166*c217d954SCole Faust 
167*c217d954SCole Faust     // Vector containing all the names of the input parameters
168*c217d954SCole Faust     std::vector<std::string> names;
169*c217d954SCole Faust     std::string              temp;
170*c217d954SCole Faust 
171*c217d954SCole Faust     // Usually the input parameters string would be name of parameters separated
172*c217d954SCole Faust     // by ',' e.g. "src0, src1, policy"
173*c217d954SCole Faust     while(std::getline(ss, temp, ','))
174*c217d954SCole Faust     {
175*c217d954SCole Faust         names.push_back(temp);
176*c217d954SCole Faust     }
177*c217d954SCole Faust     for(auto &name : names)
178*c217d954SCole Faust     {
179*c217d954SCole Faust         // Totally get rid of white space characters
180*c217d954SCole Faust         name.erase(std::remove(name.begin(), name.end(), ' '), name.end());
181*c217d954SCole Faust     }
182*c217d954SCole Faust     return names;
183*c217d954SCole Faust }
184*c217d954SCole Faust 
185*c217d954SCole Faust /** It constructs the log message to be displayed by the logger by writing each parameter name and its
186*c217d954SCole Faust  *  corresponding data info string.
187*c217d954SCole Faust  *
188*c217d954SCole Faust  * @param[in] params_names  Constant reference to a string consists of the the input parameters' names
189*c217d954SCole Faust  *                          provided e.g.: ARM_COMPUTE_LOG_PARAMS(src0, src1) then params_names = "src0, src1"
190*c217d954SCole Faust  * @param[in] data_registry Constant reference to a registry of all parameters' data in string format,
191*c217d954SCole Faust  *                          stringnized by arm_compute::to_string()
192*c217d954SCole Faust  *
193*c217d954SCole Faust  * @return  Log message string to be displayed
194*c217d954SCole Faust  */
constructDataLog(const std::vector<std::string> & params_names,const std::vector<std::string> & data_registry)195*c217d954SCole Faust inline const std::string constructDataLog(const std::vector<std::string> &params_names,
196*c217d954SCole Faust                                           const std::vector<std::string> &data_registry)
197*c217d954SCole Faust {
198*c217d954SCole Faust     std::string dataLog = "\n ";
199*c217d954SCole Faust     ARM_COMPUTE_ERROR_ON(params_names.size() != data_registry.size());
200*c217d954SCole Faust     for(uint8_t i = 0; i < params_names.size(); ++i)
201*c217d954SCole Faust     {
202*c217d954SCole Faust         dataLog += params_names[i] + ": " + data_registry.at(i) + "\n ";
203*c217d954SCole Faust     }
204*c217d954SCole Faust 
205*c217d954SCole Faust     return dataLog;
206*c217d954SCole Faust }
207*c217d954SCole Faust 
208*c217d954SCole Faust /** Macro for logging input Parameters from any function.
209*c217d954SCole Faust  *  It detects the input parameters names, and their corresponding values before stringizing them using
210*c217d954SCole Faust  *  the overloaded arm_compute::to_string() type printer. Finally, displayed using the printer configured
211*c217d954SCole Faust  *  in the logger.
212*c217d954SCole Faust  *
213*c217d954SCole Faust  * @param[in] ... Input parameters
214*c217d954SCole Faust  */
215*c217d954SCole Faust #define ARM_COMPUTE_LOG_PARAMS(...)                                                           \
216*c217d954SCole Faust     do                                                                                        \
217*c217d954SCole Faust     {                                                                                         \
218*c217d954SCole Faust         ARM_COMPUTE_LOG_INFO_WITH_FUNCNAME_ACL(constructDataLog(getParamsNames(#__VA_ARGS__), \
219*c217d954SCole Faust                                                                 logParams(__VA_ARGS__)));     \
220*c217d954SCole Faust     } while(false)
221*c217d954SCole Faust #endif /* SRC_COMMON_LOG_H */
222