1/*
2 * Copyright (C) 2005-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#pragma once
18
19#include <android/log.h>
20
21/*
22 * Normally we strip the effects of ALOGV (VERBOSE messages),
23 * LOG_FATAL and LOG_FATAL_IF (FATAL assert messages) from the
24 * release builds be defining NDEBUG.  You can modify this (for
25 * example with "#define LOG_NDEBUG 0" at the top of your source
26 * file) to change that behavior.
27 */
28
29#ifndef LOG_NDEBUG
30#ifdef NDEBUG
31#define LOG_NDEBUG 1
32#else
33#define LOG_NDEBUG 0
34#endif
35#endif
36
37#ifndef __predict_false
38#define __predict_false(exp) __builtin_expect((exp) != 0, 0)
39#endif
40
41/*
42 * Simplified macro to send a verbose system log message using current LOG_TAG.
43 */
44#ifndef SLOGV
45#define __SLOGV(...)                                                          \
46  ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, LOG_TAG, \
47                                 __VA_ARGS__))
48#if LOG_NDEBUG
49#define SLOGV(...)          \
50  do {                      \
51    if (0) {                \
52      __SLOGV(__VA_ARGS__); \
53    }                       \
54  } while (0)
55#else
56#define SLOGV(...) __SLOGV(__VA_ARGS__)
57#endif
58#endif
59
60#ifndef SLOGV_IF
61#if LOG_NDEBUG
62#define SLOGV_IF(cond, ...) ((void)0)
63#else
64#define SLOGV_IF(cond, ...)                                                 \
65  ((__predict_false(cond))                                                  \
66       ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_VERBOSE, \
67                                        LOG_TAG, __VA_ARGS__))              \
68       : (void)0)
69#endif
70#endif
71
72/*
73 * Simplified macro to send a debug system log message using current LOG_TAG.
74 */
75#ifndef SLOGD
76#define SLOGD(...)                                                          \
77  ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, LOG_TAG, \
78                                 __VA_ARGS__))
79#endif
80
81#ifndef SLOGD_IF
82#define SLOGD_IF(cond, ...)                                               \
83  ((__predict_false(cond))                                                \
84       ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_DEBUG, \
85                                        LOG_TAG, __VA_ARGS__))            \
86       : (void)0)
87#endif
88
89/*
90 * Simplified macro to send an info system log message using current LOG_TAG.
91 */
92#ifndef SLOGI
93#define SLOGI(...)                                                         \
94  ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, LOG_TAG, \
95                                 __VA_ARGS__))
96#endif
97
98#ifndef SLOGI_IF
99#define SLOGI_IF(cond, ...)                                              \
100  ((__predict_false(cond))                                               \
101       ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, \
102                                        LOG_TAG, __VA_ARGS__))           \
103       : (void)0)
104#endif
105
106/*
107 * Simplified macro to send a warning system log message using current LOG_TAG.
108 */
109#ifndef SLOGW
110#define SLOGW(...)                                                         \
111  ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, LOG_TAG, \
112                                 __VA_ARGS__))
113#endif
114
115#ifndef SLOGW_IF
116#define SLOGW_IF(cond, ...)                                              \
117  ((__predict_false(cond))                                               \
118       ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_WARN, \
119                                        LOG_TAG, __VA_ARGS__))           \
120       : (void)0)
121#endif
122
123/*
124 * Simplified macro to send an error system log message using current LOG_TAG.
125 */
126#ifndef SLOGE
127#define SLOGE(...)                                                          \
128  ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, LOG_TAG, \
129                                 __VA_ARGS__))
130#endif
131
132#ifndef SLOGE_IF
133#define SLOGE_IF(cond, ...)                                               \
134  ((__predict_false(cond))                                                \
135       ? ((void)__android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_ERROR, \
136                                        LOG_TAG, __VA_ARGS__))            \
137       : (void)0)
138#endif
139