• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DEV_AUTH_LOG_DOMAIN
53 #define DEV_AUTH_LOG_DOMAIN 0xD002F00 /* Security subsystem's domain id */
54 #endif
55 
56 #define LOGD(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_DEBUG, __FUNCTION__, fmt, ##__VA_ARGS__))
57 #define LOGI(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_INFO, __FUNCTION__, fmt, ##__VA_ARGS__))
58 #define LOGW(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_WARN, __FUNCTION__, fmt, ##__VA_ARGS__))
59 #define LOGE(fmt, ...) (DevAuthLogPrint(DEV_AUTH_LOG_LEVEL_ERROR, __FUNCTION__, fmt, ##__VA_ARGS__))
60 
61 #define SET_LOG_MODE(mode) SetLogMode(mode)
62 #define SET_TRACE_ID(traceId) SetTraceId(traceId)
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 void DevAuthLogPrint(DevAuthLogLevel level, const char *funName, const char *fmt, ...);
69 void SetLogMode(LogMode mode);
70 void SetTraceId(int64_t traceId);
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
76 #else
77 
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <inttypes.h>
81 
82 #define LOGD(fmt, ...) printf("[D][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
83 #define LOGI(fmt, ...) printf("[I][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
84 #define LOGW(fmt, ...) printf("[W][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
85 #define LOGE(fmt, ...) printf("[E][DEVAUTH]%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
86 
87 #define SET_LOG_MODE(mode)
88 #define SET_TRACE_ID(traceId)
89 
90 #endif
91 
92 #endif
93