1 /* 2 * Copyright (C) 2022 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 #ifndef HC_LOG_H 17 #define HC_LOG_H 18 19 typedef enum { 20 NORMAL_MODE = 0, 21 TRACE_MODE = 1, 22 } LogMode; 23 24 #define DESENSITIZATION_LEN 12 25 #define DEV_AUTH_ZERO 0 26 #define DEV_AUTH_ONE 1 27 #define DEV_AUTH_TWO 2 28 #define DEV_AUTH_THREE 3 29 30 #define PRINT_SENSITIVE_DATA(tag, str) \ 31 do { \ 32 if (HcStrlen((str)) < DESENSITIZATION_LEN) { \ 33 LOGW("[" tag "]: sensitive str is too short."); \ 34 } else { \ 35 LOGI("[" tag "]: %" LOG_PUB "c%" LOG_PUB "c%" LOG_PUB "c%" LOG_PUB "c****", (str)[DEV_AUTH_ZERO], \ 36 (str)[DEV_AUTH_ONE], (str)[DEV_AUTH_TWO], (str)[DEV_AUTH_THREE]); \ 37 } \ 38 } while (0) 39 40 #ifdef HILOG_ENABLE 41 42 #include <stdint.h> 43 #include <inttypes.h> 44 45 typedef enum { 46 DEV_AUTH_LOG_LEVEL_DEBUG = 0, 47 DEV_AUTH_LOG_LEVEL_INFO, 48 DEV_AUTH_LOG_LEVEL_WARN, 49 DEV_AUTH_LOG_LEVEL_ERROR 50 } DevAuthLogLevel; 51 52 #ifndef LOG_DOMAIN 53 #define LOG_DOMAIN 0xD002F03 /* Security subsystem's domain id */ 54 #endif 55 56 #ifndef LOG_TAG 57 #define LOG_TAG "[DEVAUTH]" 58 #endif 59 60 #ifdef DEV_AUTH_DEBUG_PRINTF 61 62 #include <stdio.h> 63 #include <stdlib.h> 64 65 #define LOG_PUB "" 66 67 #define LOGD(fmt, ...) printf("[D][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 68 #define LOGI(fmt, ...) printf("[I][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 69 #define LOGW(fmt, ...) printf("[W][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 70 #define LOGE(fmt, ...) printf("[E][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 71 72 #else 73 74 #include "hilog/log.h" 75 76 #define LOG_PUB "{public}" 77 78 #define LOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 79 #define LOGI(fmt, ...) HILOG_INFO(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 80 #define LOGW(fmt, ...) HILOG_WARN(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 81 #define LOGE(fmt, ...) HILOG_ERROR(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 82 83 #endif 84 85 #define SET_LOG_MODE(mode) SetLogMode(mode) 86 #define SET_TRACE_ID(traceId) SetTraceId(traceId) 87 88 #ifdef __cplusplus 89 extern "C" { 90 #endif 91 92 void DevAuthLogPrint(DevAuthLogLevel level, const char *funName, const char *fmt, ...); 93 void SetLogMode(LogMode mode); 94 void SetTraceId(int64_t traceId); 95 96 #ifdef __cplusplus 97 } 98 #endif 99 100 #else 101 102 #include <stdio.h> 103 #include <stdlib.h> 104 #include <inttypes.h> 105 106 #define LOG_PUB "" 107 108 #define LOGD(fmt, ...) printf("[D][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 109 #define LOGI(fmt, ...) printf("[I][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 110 #define LOGW(fmt, ...) printf("[W][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 111 #define LOGE(fmt, ...) printf("[E][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 112 113 #define SET_LOG_MODE(mode) 114 #define SET_TRACE_ID(traceId) 115 116 #endif 117 118 #endif 119