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