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 #define PRINT_SENSITIVE_BYTE(tag, byte, len) \ 41 do { \ 42 if ((len) < DESENSITIZATION_LEN) { \ 43 LOGW("[" tag "]: sensitive str is too short."); \ 44 } else { \ 45 LOGI("[" tag "]: %" LOG_PUB ".2x%" LOG_PUB ".2x****%" LOG_PUB ".2x%" LOG_PUB ".2x", (byte)[DEV_AUTH_ZERO], \ 46 (byte)[DEV_AUTH_ONE], (byte)[(len) - DEV_AUTH_TWO], (byte)[(len) - DEV_AUTH_ONE]); \ 47 } \ 48 } while (0) 49 50 #ifdef HILOG_ENABLE 51 52 #include <stdint.h> 53 #include <inttypes.h> 54 55 typedef enum { 56 DEV_AUTH_LOG_LEVEL_DEBUG = 0, 57 DEV_AUTH_LOG_LEVEL_INFO, 58 DEV_AUTH_LOG_LEVEL_WARN, 59 DEV_AUTH_LOG_LEVEL_ERROR 60 } DevAuthLogLevel; 61 62 #ifndef LOG_DOMAIN 63 #define LOG_DOMAIN 0xD002F03 /* Security subsystem's domain id */ 64 #endif 65 66 #ifndef LOG_TAG 67 #define LOG_TAG "[DEVAUTH]" 68 #endif 69 70 #ifdef DEV_AUTH_DEBUG_PRINTF 71 72 #include <stdio.h> 73 #include <stdlib.h> 74 75 #define LOG_PUB "" 76 77 #define LOGD(fmt, ...) printf("[D][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 78 #define LOGI(fmt, ...) printf("[I][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 79 #define LOGW(fmt, ...) printf("[W][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 80 #define LOGE(fmt, ...) printf("[E][DEVAUTH]: %s" fmt "\n", __FUNCTION__, ##__VA_ARGS__) 81 82 #else 83 84 #include "hilog/log.h" 85 86 #define LOG_PUB "{public}" 87 88 #define LOGD(fmt, ...) HILOG_DEBUG(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 89 #define LOGI(fmt, ...) HILOG_INFO(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 90 #define LOGW(fmt, ...) HILOG_WARN(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 91 #define LOGE(fmt, ...) HILOG_ERROR(LOG_CORE, "%" LOG_PUB "s: " fmt, __FUNCTION__, ##__VA_ARGS__) 92 93 #endif 94 95 #define SET_LOG_MODE(mode) SetLogMode(mode) 96 #define SET_TRACE_ID(traceId) SetTraceId(traceId) 97 98 #ifdef __cplusplus 99 extern "C" { 100 #endif 101 102 void DevAuthLogPrint(DevAuthLogLevel level, const char *funName, const char *fmt, ...); 103 void SetLogMode(LogMode mode); 104 void SetTraceId(int64_t traceId); 105 106 #ifdef __cplusplus 107 } 108 #endif 109 110 #else 111 112 #include <stdio.h> 113 #include <stdlib.h> 114 #include <inttypes.h> 115 116 #define LOG_PUB "" 117 118 #define LOGD(fmt, ...) printf("[D][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 119 #define LOGI(fmt, ...) printf("[I][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 120 #define LOGW(fmt, ...) printf("[W][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 121 #define LOGE(fmt, ...) printf("[E][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__) 122 123 #define SET_LOG_MODE(mode) 124 #define SET_TRACE_ID(traceId) 125 126 #endif 127 128 #endif 129