1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_UTIL_NANOAPP_LOG_H_
18 #define CHRE_UTIL_NANOAPP_LOG_H_
19 
20 /**
21  * @file
22  * Logging macros for nanoapps. These macros allow injecting a LOG_TAG and
23  * compiling nanoapps with a minimum logging level (that is different than CHRE
24  * itself).
25  *
26  * The typical format for the LOG_TAG macro is: "[AppName]"
27  */
28 #ifdef CHRE_IS_NANOAPP_BUILD
29 
30 #include <chre/re.h>
31 
32 #include "chre/util/log_common.h"
33 
34 #ifndef NANOAPP_MINIMUM_LOG_LEVEL
35 #error "NANOAPP_MINIMUM_LOG_LEVEL must be defined"
36 #endif  // NANOAPP_MINIMUM_LOG_LEVEL
37 
38 
39 /*
40  * Supply a stub implementation of the LOGx macros when the build is
41  * configured with a minimum logging level that is above the requested level.
42  * Otherwise just map into the chreLog function with the appropriate level.
43  */
44 
45 #define CHRE_LOG_TAG(level, tag, fmt, ...)         \
46   do {                                             \
47     CHRE_LOG_PREAMBLE                              \
48     chreLog(level, "%s " fmt, tag, ##__VA_ARGS__); \
49     CHRE_LOG_EPILOGUE                              \
50   } while (0)
51 
52 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_ERROR
53 #define LOGE_TAG(tag, fmt, ...) \
54   CHRE_LOG_TAG(CHRE_LOG_ERROR, tag, fmt, ##__VA_ARGS__)
55 #else
56 #define LOGE_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
57 #endif
58 #define LOGE(fmt, ...) LOGE_TAG(LOG_TAG, fmt, ##__VA_ARGS__)
59 
60 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_WARN
61 #define LOGW_TAG(tag, fmt, ...) \
62   CHRE_LOG_TAG(CHRE_LOG_WARN, tag, fmt, ##__VA_ARGS__)
63 #else
64 #define LOGW_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
65 #endif
66 #define LOGW(fmt, ...) LOGW_TAG(LOG_TAG, fmt, ##__VA_ARGS__)
67 
68 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_INFO
69 #define LOGI_TAG(tag, fmt, ...) \
70   CHRE_LOG_TAG(CHRE_LOG_INFO, tag, fmt, ##__VA_ARGS__)
71 #else
72 #define LOGI_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
73 #endif
74 #define LOGI(fmt, ...) LOGI_TAG(LOG_TAG, fmt, ##__VA_ARGS__)
75 
76 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_DEBUG
77 #define LOGD_TAG(tag, fmt, ...) \
78   CHRE_LOG_TAG(CHRE_LOG_DEBUG, tag, fmt, ##__VA_ARGS__)
79 #else
80 #define LOGD_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
81 #endif
82 #define LOGD(fmt, ...) LOGD_TAG(LOG_TAG, fmt, ##__VA_ARGS__)
83 
84 // Map LOGV to LOGD as CHRE doesn't support it yet.
85 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_VERBOSE
86 #define LOGV_TAG(tag, fmt, ...) \
87   CHRE_LOG_TAG(CHRE_LOG_DEBUG, tag, fmt, ##__VA_ARGS__)
88 #else
89 #define LOGV_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
90 #endif
91 #define LOGV(fmt, ...) LOGV_TAG(LOG_TAG, fmt, ##__VA_ARGS__)
92 
93 #else
94 
95 // For static nanoapps, reroute to the internal framework logging macro so that
96 // things are consistent across all the source code statically linked into the
97 // binary that contains the framework.
98 // This loses out on LOG_TAG prepending, and follows CHRE_MINIMUM_LOG_LEVEL
99 // rather than NANOAPP_MINIMUM_LOG_LEVEL, but means that anything using the
100 // container support library will have a consistent definition regardless of
101 // whether it's used in framework code or static nanoapp code.
102 #include "chre/platform/log.h"
103 
104 #endif  // CHRE_IS_NANOAPP_BUILD
105 
106 // Use this macro when including privacy-sensitive information like the user's
107 // location.
108 #ifdef LOG_INCLUDE_SENSITIVE_INFO
109 #define LOGE_SENSITIVE_INFO LOGE
110 #define LOGE_TAG_SENSITIVE_INFO LOGE_TAG
111 #define LOGW_SENSITIVE_INFO LOGW
112 #define LOGW_TAG_SENSITIVE_INFO LOGW_TAG
113 #define LOGI_SENSITIVE_INFO LOGI
114 #define LOGI_TAG_SENSITIVE_INFO LOGI_TAG
115 #define LOGD_SENSITIVE_INFO LOGD
116 #define LOGD_TAG_SENSITIVE_INFO LOGD_TAG
117 #define LOGV_SENSITIVE_INFO LOGV
118 #define LOGV_TAG_SENSITIVE_INFO LOGV_TAG
119 #else
120 #define LOGE_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
121 #define LOGE_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
122 #define LOGW_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
123 #define LOGW_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
124 #define LOGI_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
125 #define LOGI_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
126 #define LOGD_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
127 #define LOGD_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
128 #define LOGV_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
129 #define LOGV_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
130 #endif
131 
132 #endif  // CHRE_UTIL_NANOAPP_LOG_H_
133