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 "]: %c%c%c%c****", (str)[DEV_AUTH_ZERO], (str)[DEV_AUTH_ONE], \
36                 (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 #define LOGD(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_DEBUG, __FUNCTION__, fmt, ##__VA_ARGS__))
61 #define LOGI(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_INFO, __FUNCTION__, fmt, ##__VA_ARGS__))
62 #define LOGW(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_WARN, __FUNCTION__, fmt, ##__VA_ARGS__))
63 #define LOGE(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_ERROR, __FUNCTION__, fmt, ##__VA_ARGS__))
64 
65 #define SET_LOG_MODE(mode) SetLogMode(mode)
66 #define SET_TRACE_ID(traceId) SetTraceId(traceId)
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
72 void DevAuthLogPrint(DevAuthLogLevel level, const char *funName, const char *fmt, ...);
73 void SetLogMode(LogMode mode);
74 void SetTraceId(int64_t traceId);
75 
76 #ifdef __cplusplus
77 }
78 #endif
79 
80 #else
81 
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <inttypes.h>
85 
86 #define LOGD(fmt, ...) printf("[D][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
87 #define LOGI(fmt, ...) printf("[I][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
88 #define LOGW(fmt, ...) printf("[W][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
89 #define LOGE(fmt, ...) printf("[E][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
90 
91 #define SET_LOG_MODE(mode)
92 #define SET_TRACE_ID(traceId)
93 
94 #endif
95 
96 #endif
97