Home | History | Annotate | Download | only in nanoapp
      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 
     29 #include <chre/re.h>
     30 
     31 #include "chre/util/log_common.h"
     32 #include "chre/util/toolchain.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  * Supply a stub implementation of the LOGx macros when the build is
     40  * configured with a minimum logging level that is above the requested level.
     41  * Otherwise just map into the chreLog function with the appropriate level.
     42  */
     43 
     44 #define CHRE_LOG(level, fmt, ...) \
     45     do { \
     46       CHRE_LOG_PREAMBLE \
     47       chreLog(level, LOG_TAG " " fmt, ##__VA_ARGS__); \
     48       CHRE_LOG_EPILOGUE \
     49     } while (0)
     50 
     51 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_ERROR
     52 #define LOGE(fmt, ...) CHRE_LOG(CHRE_LOG_ERROR, fmt, ##__VA_ARGS__)
     53 #else
     54 #define LOGE(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
     55 #endif
     56 
     57 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_WARN
     58 #define LOGW(fmt, ...) CHRE_LOG(CHRE_LOG_WARN, fmt, ##__VA_ARGS__)
     59 #else
     60 #define LOGW(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
     61 #endif
     62 
     63 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_INFO
     64 #define LOGI(fmt, ...) CHRE_LOG(CHRE_LOG_INFO, fmt, ##__VA_ARGS__)
     65 #else
     66 #define LOGI(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
     67 #endif
     68 
     69 #if NANOAPP_MINIMUM_LOG_LEVEL >= CHRE_LOG_LEVEL_DEBUG
     70 #define LOGD(fmt, ...) CHRE_LOG(CHRE_LOG_DEBUG, fmt, ##__VA_ARGS__)
     71 #else
     72 #define LOGD(fmt, ...) CHRE_LOG_NULL(fmt, ##__VA_ARGS__)
     73 #endif
     74 
     75 // Apply printf-style compiler warnings to chreLog calls
     76 CHRE_PRINTF_ATTR(2, 3)
     77 void chreLog(enum chreLogLevel level, const char *formatStr, ...);
     78 
     79 #endif  // CHRE_UTIL_NANOAPP_LOG_H_
     80