1 /*
2  * Copyright (c) 2021-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 ACCESSTOKEN_LOG_H
17 #define ACCESSTOKEN_LOG_H
18 
19 #ifdef HILOG_ENABLE
20 
21 #include "hilog/log.h"
22 
23 /* define LOG_TAG as "security_*" at your submodule, * means your submodule name such as "security_dac" */
24 #undef LOG_TAG
25 #undef LOG_DOMAIN
26 
27 static constexpr unsigned int SECURITY_DOMAIN_ACCESSTOKEN = 0xD005A01;
28 static constexpr unsigned int SECURITY_DOMAIN_PRIVACY = 0xD005A02;
29 
30 #define ACCESSTOKEN_LOG_FATAL(label, fmt, ...)            \
31     ((void)HILOG_IMPL(label.type, LOG_FATAL, label.domain, label.tag, \
32     "[%{public}s:%{public}d]" fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__))
33 #define ACCESSTOKEN_LOG_ERROR(label, fmt, ...)            \
34     ((void)HILOG_IMPL(label.type, LOG_ERROR, label.domain, label.tag, \
35     "[%{public}s:%{public}d]" fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__))
36 #define ACCESSTOKEN_LOG_WARN(label, fmt, ...)            \
37     ((void)HILOG_IMPL(label.type, LOG_WARN, label.domain, label.tag, \
38     "[%{public}s:%{public}d]" fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__))
39 #define ACCESSTOKEN_LOG_INFO(label, fmt, ...)            \
40     ((void)HILOG_IMPL(label.type, LOG_INFO, label.domain, label.tag, \
41     "[%{public}s:%{public}d]" fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__))
42 #define ACCESSTOKEN_LOG_DEBUG(label, fmt, ...)            \
43     ((void)HILOG_IMPL(label.type, LOG_DEBUG, label.domain, label.tag, \
44     "[%{public}s:%{public}d]" fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__))
45 
46 #define IF_FALSE_PRINT_LOG(label, cond, fmt, ...) \
47     do { \
48         if (!(cond)) { \
49             ACCESSTOKEN_LOG_ERROR(label, fmt, ##__VA_ARGS__); \
50         } \
51     } while (0)
52 
53 #define IF_FALSE_RETURN_LOG(label, cond, fmt, ...) \
54     do { \
55         if (!(cond)) { \
56             ACCESSTOKEN_LOG_ERROR(label, fmt, ##__VA_ARGS__); \
57             return; \
58         } \
59     } while (0)
60 #else
61 
62 #include <stdarg.h>
63 #include <stdio.h>
64 
65 /* define LOG_TAG as "security_*" at your submodule, * means your submodule name such as "security_dac" */
66 #undef LOG_TAG
67 
68 #define ACCESSTOKEN_LOG_DEBUG(fmt, ...) printf("[%s] debug: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__)
69 #define ACCESSTOKEN_LOG_INFO(fmt, ...) printf("[%s] info: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__)
70 #define ACCESSTOKEN_LOG_WARN(fmt, ...) printf("[%s] warn: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__)
71 #define ACCESSTOKEN_LOG_ERROR(fmt, ...) printf("[%s] error: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__)
72 #define ACCESSTOKEN_LOG_FATAL(fmt, ...) printf("[%s] fatal: %s: " fmt "\n", LOG_TAG, __func__, ##__VA_ARGS__)
73 
74 #define IF_FALSE_PRINT_LOG(cond, fmt, ...) \
75     do { \
76         if (!(cond)) { \
77             ACCESSTOKEN_LOG_ERROR(fmt, ##__VA_ARGS__); \
78         } \
79     } while (0)
80 
81 #define IF_FALSE_RETURN_LOG(cond, fmt, ...) \
82     do { \
83         if (!(cond)) { \
84             ACCESSTOKEN_LOG_ERROR(fmt, ##__VA_ARGS__); \
85             return; \
86         } \
87     } while (0)
88 #endif // HILOG_ENABLE
89 
90 #endif // ACCESSTOKEN_LOG_H
91