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/util/log_common.h" 31 #include "chre_api/chre.h" 32 33 #ifndef NANOAPP_MINIMUM_LOG_LEVEL 34 #error "NANOAPP_MINIMUM_LOG_LEVEL must be defined" 35 #endif // NANOAPP_MINIMUM_LOG_LEVEL 36 37 /* 38 * Supply a stub implementation of the LOGx macros when the build is 39 * configured with a minimum logging level that is above the requested level. 40 * Use CHRE with pigweed tokenized logging if enabled, otherwise just map into 41 * the chreLog function with the appropriate level. 42 */ 43 44 #ifdef CHRE_NANOAPP_TOKENIZED_LOGGING_ENABLED 45 #include "chre/platform/shared/nanoapp/tokenized_log.h" 46 #include "pw_tokenizer/encode_args.h" 47 #include "pw_tokenizer/tokenize.h" 48 49 #define CHRE_LOG_TAG(level, tag, fmt, ...) \ 50 do { \ 51 CHRE_LOG_PREAMBLE \ 52 PW_TOKENIZE_FORMAT_STRING(PW_TOKENIZER_DEFAULT_DOMAIN, UINT32_MAX, \ 53 tag " " fmt, __VA_ARGS__); \ 54 platform_chrePwTokenizedLog(level, _pw_tokenizer_token, \ 55 PW_TOKENIZER_ARG_TYPES(__VA_ARGS__) \ 56 PW_COMMA_ARGS(__VA_ARGS__)); \ 57 CHRE_LOG_EPILOGUE \ 58 } while (0) 59 60 #else 61 62 #define CHRE_LOG_TAG(level, tag, fmt, ...) \ 63 do { \ 64 CHRE_LOG_PREAMBLE \ 65 chreLog(level, "%s " fmt, tag, ##__VA_ARGS__); \ 66 CHRE_LOG_EPILOGUE \ 67 } while (0) 68 69 #endif // CHRE_NANOAPP_TOKENIZED_LOGGING_ENABLED 70 71 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_ERROR 72 #define LOGE_TAG(tag, fmt, ...) \ 73 CHRE_LOG_TAG(CHRE_LOG_ERROR, tag, fmt, ##__VA_ARGS__) 74 #else 75 #define LOGE_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 76 #endif 77 #define LOGE(fmt, ...) LOGE_TAG(LOG_TAG, fmt, ##__VA_ARGS__) 78 79 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_WARN 80 #define LOGW_TAG(tag, fmt, ...) \ 81 CHRE_LOG_TAG(CHRE_LOG_WARN, tag, fmt, ##__VA_ARGS__) 82 #else 83 #define LOGW_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 84 #endif 85 #define LOGW(fmt, ...) LOGW_TAG(LOG_TAG, fmt, ##__VA_ARGS__) 86 87 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_INFO 88 #define LOGI_TAG(tag, fmt, ...) \ 89 CHRE_LOG_TAG(CHRE_LOG_INFO, tag, fmt, ##__VA_ARGS__) 90 #else 91 #define LOGI_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 92 #endif 93 #define LOGI(fmt, ...) LOGI_TAG(LOG_TAG, fmt, ##__VA_ARGS__) 94 95 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_DEBUG 96 #define LOGD_TAG(tag, fmt, ...) \ 97 CHRE_LOG_TAG(CHRE_LOG_DEBUG, tag, fmt, ##__VA_ARGS__) 98 #else 99 #define LOGD_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 100 #endif 101 #define LOGD(fmt, ...) LOGD_TAG(LOG_TAG, fmt, ##__VA_ARGS__) 102 103 // Map LOGV to LOGD as CHRE doesn't support it yet. 104 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_VERBOSE 105 #define LOGV_TAG(tag, fmt, ...) \ 106 CHRE_LOG_TAG(CHRE_LOG_DEBUG, tag, fmt, ##__VA_ARGS__) 107 #else 108 #define LOGV_TAG(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 109 #endif 110 #define LOGV(fmt, ...) LOGV_TAG(LOG_TAG, fmt, ##__VA_ARGS__) 111 112 #else 113 114 // For static nanoapps, reroute to the internal framework logging macro so that 115 // things are consistent across all the source code statically linked into the 116 // binary that contains the framework. 117 // This loses out on LOG_TAG prepending, and follows CHRE_MINIMUM_LOG_LEVEL 118 // rather than NANOAPP_MINIMUM_LOG_LEVEL, but means that anything using the 119 // container support library will have a consistent definition regardless of 120 // whether it's used in framework code or static nanoapp code. 121 #include "chre/platform/log.h" 122 123 #endif // CHRE_IS_NANOAPP_BUILD 124 125 // Use this macro when including privacy-sensitive information like the user's 126 // location. 127 #ifdef LOG_INCLUDE_SENSITIVE_INFO 128 #define LOGE_SENSITIVE_INFO LOGE 129 #define LOGE_TAG_SENSITIVE_INFO LOGE_TAG 130 #define LOGW_SENSITIVE_INFO LOGW 131 #define LOGW_TAG_SENSITIVE_INFO LOGW_TAG 132 #define LOGI_SENSITIVE_INFO LOGI 133 #define LOGI_TAG_SENSITIVE_INFO LOGI_TAG 134 #define LOGD_SENSITIVE_INFO LOGD 135 #define LOGD_TAG_SENSITIVE_INFO LOGD_TAG 136 #define LOGV_SENSITIVE_INFO LOGV 137 #define LOGV_TAG_SENSITIVE_INFO LOGV_TAG 138 #else 139 #define LOGE_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 140 #define LOGE_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 141 #define LOGW_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 142 #define LOGW_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 143 #define LOGI_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 144 #define LOGI_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 145 #define LOGD_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 146 #define LOGD_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 147 #define LOGV_SENSITIVE_INFO(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 148 #define LOGV_TAG_SENSITIVE_INFO(tag, fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__) 149 #endif 150 151 // Convenience macro that helps with suppressing double promotion warnings when 152 // passing a float to chreDebugDumpLog(). 153 #define CHRE_DEBUG_DUMP_LOG(fmt, ...) \ 154 do { \ 155 CHRE_LOG_PREAMBLE \ 156 chreDebugDumpLog(fmt, ##__VA_ARGS__); \ 157 CHRE_LOG_EPILOGUE \ 158 } while (0) 159 160 #endif // CHRE_UTIL_NANOAPP_LOG_H_ 161