1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"), 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 /** 17 * @addtogroup TeeTrusted 18 * @{ 19 * 20 * @brief TEE(Trusted Excution Environment) API. 21 * Provides security capability APIs such as trusted storage, encryption and decryption, 22 * and trusted time for trusted application development. 23 * 24 * @since 20 25 */ 26 27 /** 28 * @file tee_log.h 29 * 30 * @brief Provides TEE log APIs. 31 * 32 * Reference of TEE log APIs and internal definitions. 33 * 34 * @library NA 35 * @kit TEEKit 36 * @syscap SystemCapability.Tee.TeeClient 37 * @since 20 38 */ 39 40 #ifndef __TEE_LOG_H 41 #define __TEE_LOG_H 42 43 #include "tee_defines.h" 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 /** 50 * @brief Defines the ERROR level of the TA log. 51 * 52 * @since 20 53 */ 54 #define TA_LOG_LEVEL_ERROR 0 55 56 /** 57 * @brief Defines the WARNING level of the TA log. 58 * 59 * @since 20 60 */ 61 #define TA_LOG_LEVEL_WARNING 1 62 63 /** 64 * @brief Defines the INFO level of the TA log. 65 * 66 * @since 20 67 */ 68 #define TA_LOG_LEVEL_INFO 2 69 70 /** 71 * @brief Defines the DEBUG level of the TA log. 72 * 73 * @since 20 74 */ 75 #define TA_LOG_LEVEL_DEBUG 3 76 77 /** 78 * @brief Defines the VERBO level of the TA log. 79 * 80 * @since 20 81 */ 82 #define TA_LOG_LEVEL_VERBO 4 83 84 /** 85 * @brief Defines the default level of the TA log. 86 * 87 * @since 20 88 */ 89 #define TA_LOG_LEVEL_DEFAULT TA_LOG_LEVEL_INFO 90 91 #ifndef TA_LOG_LEVEL 92 /** 93 * @brief Defines the default level of the TA log. 94 * {@code TA_LOG_LEVEL} can be redefined by TA developers 95 * 96 * @since 20 97 */ 98 #define TA_LOG_LEVEL TA_LOG_LEVEL_DEFAULT 99 #endif 100 101 /** 102 * @brief Defines the tag of the VERBO level TA log. 103 * 104 * @since 20 105 */ 106 #define TAG_VERB "[verb]" 107 108 /** 109 * @brief Defines the tag of the DEBUG level TA log. 110 * 111 * @since 20 112 */ 113 #define TAG_DEBUG "[debug]" 114 115 /** 116 * @brief Defines the tag of the INFO level TA log. 117 * 118 * @since 20 119 */ 120 #define TAG_INFO "[info]" 121 122 /** 123 * @brief Defines the tag of the WARNING level TA log. 124 * 125 * @since 20 126 */ 127 #define TAG_WARN "[warn]" 128 129 /** 130 * @brief Defines the tag of the ERROR level TA log. 131 * 132 * @since 20 133 */ 134 #define TAG_ERROR "[error]" 135 136 /** 137 * @brief Enumerates the levels of the log. 138 * 139 * @since 20 140 */ 141 typedef enum { 142 /** Error level log. */ 143 LOG_LEVEL_ERROR = 0, 144 /** Warning level log. */ 145 LOG_LEVEL_WARN = 1, 146 /** Information level log. */ 147 LOG_LEVEL_INFO = 2, 148 /** Debug level log. */ 149 LOG_LEVEL_DEBUG = 3, 150 /** Verbose level log. */ 151 LOG_LEVEL_VERBO = 4, 152 /** On level log. */ 153 LOG_LEVEL_ON = 5, 154 } LOG_LEVEL; 155 156 /** 157 * @brief Provides to print UART logs. 158 * 159 * @param fmt [IN] The log information. 160 * 161 * @since 20 162 */ 163 void uart_cprintf(const char *fmt, ...); 164 165 /** 166 * @brief Provides to print UART logs. 167 * 168 * @param fmt [IN] The log information. 169 * 170 * @since 20 171 */ 172 void uart_printf_func(const char *fmt, ...); 173 174 /** 175 * @brief Provides to print TEE logs. 176 * 177 * @param log_level [IN] The level of the log. 178 * @param fmt [IN] The log information. 179 * 180 * @since 20 181 */ 182 void tee_print(LOG_LEVEL log_level, const char *fmt, ...); 183 184 /** 185 * @brief Provides to print TEE driver logs. 186 * 187 * @param log_level [IN] The level of the log. 188 * @param log_tag [IN] The tag of the log. 189 * @param fmt [IN] The log information. 190 * 191 * @since 20 192 */ 193 void tee_print_driver(LOG_LEVEL log_level, const char *log_tag, const char *fmt, ...); 194 195 /** 196 * @brief Defines the debug prefix string. 197 * 198 * @since 20 199 */ 200 extern const char *g_debug_prefix; 201 202 #if (TA_LOG_LEVEL >= TA_LOG_LEVEL_VERBO) 203 #ifdef DRIVER_LOG_TAG 204 /** 205 * @brief Defines the API to print TEE log at the VERBO level. 206 * 207 * @since 20 208 */ 209 #define tlogv(fmt, args...) \ 210 tee_print_driver(LOG_LEVEL_VERBO, DRIVER_LOG_TAG, "%s %d:" fmt "", TAG_VERB, __LINE__, ##args) 211 #else 212 /** 213 * @brief Defines the API to print TEE log at the VERBO level. 214 * 215 * @since 20 216 */ 217 #define tlogv(fmt, args...) tee_print(LOG_LEVEL_VERBO, "%s %d:" fmt "", TAG_VERB, __LINE__, ##args) 218 #endif /* DRIVER_LOG_TAG */ 219 #else 220 /** 221 * @brief Defines the API to print TEE log at the VERBO level. 222 * 223 * @since 20 224 */ 225 #define tlogv(fmt, args...) \ 226 do { \ 227 } while (0) 228 #endif /* TA_LOG_LEVEL >= TA_LOG_LEVEL_VERBO */ 229 230 #if (TA_LOG_LEVEL >= TA_LOG_LEVEL_DEBUG) 231 #ifdef DRIVER_LOG_TAG 232 /** 233 * @brief Defines the API to print TEE log at the DEBUG level. 234 * 235 * @since 20 236 */ 237 #define tlogd(fmt, args...) \ 238 tee_print_driver(LOG_LEVEL_DEBUG, DRIVER_LOG_TAG, "%s %d:" fmt "", TAG_DEBUG, __LINE__, ##args) 239 #else 240 /** 241 * @brief Defines the API to print TEE log at the DEBUG level. 242 * 243 * @since 20 244 */ 245 #define tlogd(fmt, args...) tee_print(LOG_LEVEL_DEBUG, "%s %d:" fmt "", TAG_DEBUG, __LINE__, ##args) 246 #endif /* DRIVER_LOG_TAG */ 247 #else 248 /** 249 * @brief Defines the API to print TEE log at the DEBUG level. 250 * 251 * @since 20 252 */ 253 #define tlogd(fmt, args...) \ 254 do { \ 255 } while (0) 256 #endif /* TA_LOG_LEVEL >= TA_LOG_LEVEL_DEBUG */ 257 258 #if (TA_LOG_LEVEL >= TA_LOG_LEVEL_INFO) 259 #ifdef DRIVER_LOG_TAG 260 /** 261 * @brief Defines the API to print TEE log at the INFO level. 262 * 263 * @since 20 264 */ 265 #define tlogi(fmt, args...) \ 266 tee_print_driver(LOG_LEVEL_INFO, DRIVER_LOG_TAG, "%s %d:" fmt "", TAG_INFO, __LINE__, ##args) 267 #else 268 /** 269 * @brief Defines the API to print TEE log at the INFO level. 270 * 271 * @since 20 272 */ 273 #define tlogi(fmt, args...) tee_print(LOG_LEVEL_INFO, "%s %d:" fmt "", TAG_INFO, __LINE__, ##args) 274 #endif /* DRIVER_LOG_TAG */ 275 #else 276 /** 277 * @brief Defines the API to print TEE log at the INFO level. 278 * 279 * @since 20 280 */ 281 #define tlogi(fmt, args...) \ 282 do { \ 283 } while (0) 284 #endif /* TA_LOG_LEVEL >= TA_LOG_LEVEL_INFO */ 285 286 #if (TA_LOG_LEVEL >= TA_LOG_LEVEL_WARNING) 287 #ifdef DRIVER_LOG_TAG 288 /** 289 * @brief Defines the API to print TEE log at the WARNING level. 290 * 291 * @since 20 292 */ 293 #define tlogw(fmt, args...) \ 294 tee_print_driver(LOG_LEVEL_WARN, DRIVER_LOG_TAG, "%s %d:" fmt "", TAG_WARN, __LINE__, ##args) 295 #else 296 /** 297 * @brief Defines the API to print TEE log at the WARNING level. 298 * 299 * @since 20 300 */ 301 #define tlogw(fmt, args...) tee_print(LOG_LEVEL_WARN, "%s %d:" fmt "", TAG_WARN, __LINE__, ##args) 302 #endif /* DRIVER_LOG_TAG */ 303 #else 304 /** 305 * @brief Defines the API to print TEE log at the WARNING level. 306 * 307 * @since 20 308 */ 309 #define tlogw(fmt, args...) \ 310 do { \ 311 } while (0) 312 #endif /* TA_LOG_LEVEL >= TA_LOG_LEVEL_WARNING */ 313 314 #if (TA_LOG_LEVEL >= TA_LOG_LEVEL_ERROR) /* Always meet this condition. */ 315 #ifndef TLOGE_NO_TIMESTAMP 316 #ifdef DRIVER_LOG_TAG 317 /** 318 * @brief Defines the API to print TEE log at the ERROR level. 319 * 320 * @since 20 321 */ 322 #define tloge(fmt, args...) \ 323 tee_print_driver(LOG_LEVEL_ERROR, DRIVER_LOG_TAG, "%s %d:" fmt " ", TAG_ERROR, __LINE__, ##args) 324 #else 325 /** 326 * @brief Defines the API to print TEE log at the ERROR level. 327 * 328 * @since 20 329 */ 330 #define tloge(fmt, args...) tee_print(LOG_LEVEL_ERROR, "%s %d:" fmt " ", TAG_ERROR, __LINE__, ##args) 331 #endif /* DRIVER_LOG_TAG */ 332 #else 333 /** 334 * @brief Defines the API to print TEE log at the ERROR level. 335 * 336 * @since 20 337 */ 338 #define tloge(fmt, args...) printf("[%s] %s %d:" fmt " ", g_debug_prefix, TAG_ERROR, __LINE__, ##args) 339 #endif /* TLOGE_NO_TIMESTAMP */ 340 #else 341 /** 342 * @brief Defines the API to print TEE log at the ERROR level. 343 * 344 * @since 20 345 */ 346 #define tloge(fmt, args...) \ 347 do { \ 348 } while (0) 349 #endif /* TA_LOG_LEVEL >= TA_LOG_LEVEL_ERROR */ 350 351 /** 352 * @brief Defines an enum for system events. 353 * 354 * @since 20 355 */ 356 typedef enum { 357 /** Fault event. */ 358 FAULT = 1, 359 /** Statistics event. */ 360 STATISTIC = 2, 361 /** Security event. */ 362 SECURITY = 3, 363 /** Behavior event. */ 364 BEHAVIOR = 4, 365 } HISYSEVENT_TYPE; 366 367 /** 368 * @brief Reports DFX messages to HiViewOcean. 369 * 370 * @param domain Domain name of a message. 371 * @param event Event name of a message. 372 * @param event_type Event type of a message. 373 * @param fmt Format of a logged message. 374 * 375 * @since 20 376 */ 377 378 void tee_report(const char *domain, const char *event, HISYSEVENT_TYPE event_type, const char *fmt, ...); 379 380 381 #ifdef __cplusplus 382 } 383 #endif 384 385 #endif /* __TEE_LOG_H */ 386 /** @} */