• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 INCLUDE_PERFETTO_BASE_LOGGING_H_
18 #define INCLUDE_PERFETTO_BASE_LOGGING_H_
19 
20 #include <errno.h>
21 #include <string.h>  // For strerror.
22 
23 #include "perfetto/base/build_config.h"
24 #include "perfetto/base/compiler.h"
25 #include "perfetto/base/export.h"
26 
27 // Ignore GCC warning about a missing argument for a variadic macro parameter.
28 #pragma GCC system_header
29 
30 // TODO(primiano): move this to base/build_config.h, turn into
31 // PERFETTO_BUILDFLAG(DCHECK_IS_ON) and update call sites to use that instead.
32 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
33 #define PERFETTO_DCHECK_IS_ON() 0
34 #else
35 #define PERFETTO_DCHECK_IS_ON() 1
36 #endif
37 
38 #if PERFETTO_BUILDFLAG(PERFETTO_FORCE_DLOG_ON)
39 #define PERFETTO_DLOG_IS_ON() 1
40 #elif PERFETTO_BUILDFLAG(PERFETTO_FORCE_DLOG_OFF)
41 #define PERFETTO_DLOG_IS_ON() 0
42 #else
43 #define PERFETTO_DLOG_IS_ON() PERFETTO_DCHECK_IS_ON()
44 #endif
45 
46 #if defined(PERFETTO_ANDROID_ASYNC_SAFE_LOG)
47 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
48     !PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
49 #error "Async-safe logging is limited to Android tree builds"
50 #endif
51 // For binaries which need a very lightweight logging implementation.
52 // Note that this header is incompatible with android/log.h.
53 #include <async_safe/log.h>
54 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
55 // Normal android logging.
56 #include <android/log.h>
57 #endif
58 
59 namespace perfetto {
60 namespace base {
61 
62 // Constexpr functions to extract basename(__FILE__), e.g.: ../foo/f.c -> f.c .
StrEnd(const char * s)63 constexpr const char* StrEnd(const char* s) {
64   return *s ? StrEnd(s + 1) : s;
65 }
66 
BasenameRecursive(const char * s,const char * begin,const char * end)67 constexpr const char* BasenameRecursive(const char* s,
68                                         const char* begin,
69                                         const char* end) {
70   return (*s == '/' && s < end)
71              ? (s + 1)
72              : ((s > begin) ? BasenameRecursive(s - 1, begin, end) : s);
73 }
74 
Basename(const char * str)75 constexpr const char* Basename(const char* str) {
76   return BasenameRecursive(StrEnd(str), str, StrEnd(str));
77 }
78 
79 enum LogLev { kLogDebug = 0, kLogInfo, kLogImportant, kLogError };
80 
81 PERFETTO_EXPORT void LogMessage(LogLev,
82                                 const char* fname,
83                                 int line,
84                                 const char* fmt,
85                                 ...) PERFETTO_PRINTF_FORMAT(4, 5);
86 
87 #if defined(PERFETTO_ANDROID_ASYNC_SAFE_LOG)
88 #define PERFETTO_XLOG(level, fmt, ...)                                        \
89   do {                                                                        \
90     async_safe_format_log((ANDROID_LOG_DEBUG + level), "perfetto",            \
91                           "%s:%d " fmt, ::perfetto::base::Basename(__FILE__), \
92                           __LINE__, ##__VA_ARGS__);                           \
93   } while (0)
94 #elif defined(PERFETTO_DISABLE_LOG)
95 #define PERFETTO_XLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__)
96 #else
97 #define PERFETTO_XLOG(level, fmt, ...)                                      \
98   ::perfetto::base::LogMessage(level, ::perfetto::base::Basename(__FILE__), \
99                                __LINE__, fmt, ##__VA_ARGS__)
100 #endif
101 
102 #define PERFETTO_IMMEDIATE_CRASH() \
103   do {                             \
104     __builtin_trap();              \
105     __builtin_unreachable();       \
106   } while (0)
107 
108 #if PERFETTO_BUILDFLAG(PERFETTO_VERBOSE_LOGS)
109 #define PERFETTO_LOG(fmt, ...) \
110   PERFETTO_XLOG(::perfetto::base::kLogInfo, fmt, ##__VA_ARGS__)
111 #else  // PERFETTO_BUILDFLAG(PERFETTO_VERBOSE_LOGS)
112 #define PERFETTO_LOG(...) ::perfetto::base::ignore_result(__VA_ARGS__)
113 #endif  // PERFETTO_BUILDFLAG(PERFETTO_VERBOSE_LOGS)
114 
115 #define PERFETTO_ILOG(fmt, ...) \
116   PERFETTO_XLOG(::perfetto::base::kLogImportant, fmt, ##__VA_ARGS__)
117 #define PERFETTO_ELOG(fmt, ...) \
118   PERFETTO_XLOG(::perfetto::base::kLogError, fmt, ##__VA_ARGS__)
119 #define PERFETTO_FATAL(fmt, ...)       \
120   do {                                 \
121     PERFETTO_PLOG(fmt, ##__VA_ARGS__); \
122     PERFETTO_IMMEDIATE_CRASH();        \
123   } while (0)
124 
125 #define PERFETTO_PLOG(x, ...) \
126   PERFETTO_ELOG(x " (errno: %d, %s)", ##__VA_ARGS__, errno, strerror(errno))
127 
128 #define PERFETTO_CHECK(x)                            \
129   do {                                               \
130     if (PERFETTO_UNLIKELY(!(x))) {                   \
131       PERFETTO_PLOG("%s", "PERFETTO_CHECK(" #x ")"); \
132       PERFETTO_IMMEDIATE_CRASH();                    \
133     }                                                \
134   } while (0)
135 
136 #if PERFETTO_DLOG_IS_ON()
137 
138 #define PERFETTO_DLOG(fmt, ...) \
139   PERFETTO_XLOG(::perfetto::base::kLogDebug, fmt, ##__VA_ARGS__)
140 
141 #define PERFETTO_DPLOG(x, ...) \
142   PERFETTO_DLOG(x " (errno: %d, %s)", ##__VA_ARGS__, errno, strerror(errno))
143 
144 #else  // PERFETTO_DLOG_IS_ON()
145 
146 #define PERFETTO_DLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__)
147 #define PERFETTO_DPLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__)
148 
149 #endif  // PERFETTO_DLOG_IS_ON()
150 
151 #if PERFETTO_DCHECK_IS_ON()
152 
153 #define PERFETTO_DCHECK(x) PERFETTO_CHECK(x)
154 #define PERFETTO_DFATAL(...) PERFETTO_FATAL(__VA_ARGS__)
155 #define PERFETTO_DFATAL_OR_ELOG(...) PERFETTO_DFATAL(__VA_ARGS__)
156 
157 #else  // PERFETTO_DCHECK_IS_ON()
158 
159 #define PERFETTO_DCHECK(x) \
160   do {                     \
161   } while (false && (x))
162 
163 #define PERFETTO_DFATAL(...) ::perfetto::base::ignore_result(__VA_ARGS__)
164 #define PERFETTO_DFATAL_OR_ELOG(...) PERFETTO_ELOG(__VA_ARGS__)
165 
166 #endif  // PERFETTO_DCHECK_IS_ON()
167 
168 }  // namespace base
169 }  // namespace perfetto
170 
171 #endif  // INCLUDE_PERFETTO_BASE_LOGGING_H_
172