• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <cstdio>
20 #include <cstdlib>
21 
22 #include <android/log.h>
23 
24 extern "C" {
25 
26 #ifndef ANDROID_LOG_STUB_MIN_PRIORITY
27 #define ANDROID_LOG_STUB_MIN_PRIORITY ANDROID_LOG_INFO
28 #endif
29 
30 #ifndef LOG_TAG
31 #define LOG_TAG ""
32 #endif
33 
__android_log_stub_is_loggable(android_LogPriority priority)34 constexpr bool __android_log_stub_is_loggable(android_LogPriority priority) {
35     return ANDROID_LOG_STUB_MIN_PRIORITY <= priority;
36 }
37 
38 #ifdef ANDROID_LOG_STUB_WEAK_PRINT
39 #define __ANDROID_LOG_STUB_IS_PRINT_PRESENT __android_log_print
40 #define __ANDROID_LOG_STUB_PRINT_ATTR __attribute__((weak))
41 #else
42 #define __ANDROID_LOG_STUB_IS_PRINT_PRESENT true
43 #define __ANDROID_LOG_STUB_PRINT_ATTR
44 #endif
45 
46 int __android_log_print(int prio, const char* tag, const char* fmt, ...)
47         __attribute__((format(printf, 3, 4))) __ANDROID_LOG_STUB_PRINT_ATTR;
48 
49 #define IF_ALOG(priority, tag) \
50     if (__android_log_stub_is_loggable(ANDROID_##priority) && __ANDROID_LOG_STUB_IS_PRINT_PRESENT)
51 #define IF_ALOGV() IF_ALOG(LOG_VERBOSE, LOG_TAG)
52 #define IF_ALOGD() IF_ALOG(LOG_DEBUG, LOG_TAG)
53 #define IF_ALOGI() IF_ALOG(LOG_INFO, LOG_TAG)
54 #define IF_ALOGW() IF_ALOG(LOG_WARN, LOG_TAG)
55 #define IF_ALOGE() IF_ALOG(LOG_ERROR, LOG_TAG)
56 
57 #define ALOG(priority, tag, fmt, ...)                                        \
58     do {                                                                     \
59         if (false)[[/*VERY*/ unlikely]] { /* ignore unused __VA_ARGS__ */    \
60             std::fprintf(stderr, fmt __VA_OPT__(, ) __VA_ARGS__);            \
61         }                                                                    \
62         IF_ALOG(priority, tag) {                                             \
63             __android_log_print(ANDROID_##priority, tag, "%s: " fmt "\n",    \
64                                 (tag)__VA_OPT__(, ) __VA_ARGS__);            \
65         }                                                                    \
66         if constexpr (ANDROID_##priority == ANDROID_LOG_FATAL) std::abort(); \
67     } while (false)
68 #define ALOGV(...) ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
69 #define ALOGD(...) ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__)
70 #define ALOGI(...) ALOG(LOG_INFO, LOG_TAG, __VA_ARGS__)
71 #define ALOGW(...) ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__)
72 #define ALOGE(...) ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__)
73 #define LOG_FATAL(...) ALOG(LOG_FATAL, LOG_TAG, __VA_ARGS__)
74 #define LOG_ALWAYS_FATAL LOG_FATAL
75 
76 #define ALOG_IF(cond, priority, tag, ...) \
77     if (cond) [[unlikely]]                \
78     ALOG(priority, tag, #cond ": " __VA_ARGS__)
79 #define ALOGV_IF(cond, ...) ALOG_IF(cond, LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
80 #define ALOGD_IF(cond, ...) ALOG_IF(cond, LOG_DEBUG, LOG_TAG, __VA_ARGS__)
81 #define ALOGI_IF(cond, ...) ALOG_IF(cond, LOG_INFO, LOG_TAG, __VA_ARGS__)
82 #define ALOGW_IF(cond, ...) ALOG_IF(cond, LOG_WARN, LOG_TAG, __VA_ARGS__)
83 #define ALOGE_IF(cond, ...) ALOG_IF(cond, LOG_ERROR, LOG_TAG, __VA_ARGS__)
84 #define LOG_FATAL_IF(cond, ...) ALOG_IF(cond, LOG_FATAL, LOG_TAG, __VA_ARGS__)
85 #define LOG_ALWAYS_FATAL_IF LOG_FATAL_IF
86 #define ALOG_ASSERT(cond, ...) LOG_FATAL_IF(!(cond), ##__VA_ARGS__)
87 
android_errorWriteLog(int tag,const char * subTag)88 inline int android_errorWriteLog(int tag, const char* subTag) {
89     ALOGE("android_errorWriteLog(%x, %s)", tag, subTag);
90     return 0;
91 }
92 
93 } // extern "C"
94