blob: c15597a9f50c41a08873ce44f8ac5e0c883c68c8 [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +00001/*
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>
Primiano Tucci5aab7582017-12-07 12:22:03 +000021#include <stdio.h>
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000022#include <stdlib.h>
Hector Dearmanadc8c2b2018-03-12 13:19:02 +000023#include <string.h> // For strerror.
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000024#include <unistd.h>
25
26#if defined(NDEBUG)
27#define PERFETTO_DCHECK_IS_ON() 0
28#else
29#define PERFETTO_DCHECK_IS_ON() 1
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000030#endif
31
Primiano Tucci05889d52017-12-21 18:47:17 +010032#include "perfetto/base/build_config.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000033#include "perfetto/base/utils.h"
34
Oystein Eftevaagff729592018-02-12 14:24:06 -080035#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
Primiano Tucci05889d52017-12-21 18:47:17 +010036#include <android/log.h>
37#endif
38
Primiano Tucci5aab7582017-12-07 12:22:03 +000039namespace perfetto {
40namespace base {
41
42// Constexpr functions to extract basename(__FILE__), e.g.: ../foo/f.c -> f.c .
43constexpr const char* StrEnd(const char* s) {
44 return *s ? StrEnd(s + 1) : s;
45}
46
47constexpr const char* BasenameRecursive(const char* s,
48 const char* begin,
49 const char* end) {
50 return (*s == '/' && s < end)
51 ? (s + 1)
52 : ((s > begin) ? BasenameRecursive(s - 1, begin, end) : s);
53}
54
55constexpr const char* Basename(const char* str) {
56 return BasenameRecursive(StrEnd(str), str, StrEnd(str));
57}
58
59enum LogLev { kLogDebug = 0, kLogInfo, kLogImportant, kLogError };
60constexpr const char* kLogFmt[] = {"\x1b[2m", "\x1b[39m", "\x1b[32m\x1b[1m",
61 "\x1b[31m"};
62
63#define PERFETTO_LOG_LINE__(x) #x
64#define PERFETTO_LOG_LINE_(x) PERFETTO_LOG_LINE__(x)
65#define PERFETTO_LOG_LINE PERFETTO_LOG_LINE_(__LINE__)
66
Primiano Tucci05889d52017-12-21 18:47:17 +010067#define PERFETTO_XLOG_STDERR(level, fmt, ...) \
Primiano Tucci5aab7582017-12-07 12:22:03 +000068 fprintf(stderr, "\x1b[90m%-24.24s\x1b[0m %s" fmt "\x1b[0m\n", \
69 ::perfetto::base::Basename(__FILE__ ":" PERFETTO_LOG_LINE), \
70 ::perfetto::base::kLogFmt[::perfetto::base::LogLev::level], \
71 ##__VA_ARGS__)
72
Primiano Tucci05889d52017-12-21 18:47:17 +010073// Let android log to both stderr and logcat. When part of the Android tree
74// stderr points to /dev/null so logcat is the only way to get some logging.
Oystein Eftevaagff729592018-02-12 14:24:06 -080075#if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
Primiano Tucci05889d52017-12-21 18:47:17 +010076#define PERFETTO_XLOG(level, fmt, ...) \
77 do { \
78 __android_log_print( \
79 (ANDROID_LOG_DEBUG + ::perfetto::base::LogLev::level), "perfetto", \
80 "%s " fmt, ::perfetto::base::Basename(__FILE__ ":" PERFETTO_LOG_LINE), \
81 ##__VA_ARGS__); \
82 PERFETTO_XLOG_STDERR(level, fmt, ##__VA_ARGS__); \
83 } while (0)
84#else
85#define PERFETTO_XLOG PERFETTO_XLOG_STDERR
86#endif
87
Florian Mayer2ab0d772018-01-17 15:53:17 +000088#define PERFETTO_IMMEDIATE_CRASH() \
89 do { \
90 __builtin_trap(); \
91 __builtin_unreachable(); \
92 } while (0)
93
Primiano Tucci5aab7582017-12-07 12:22:03 +000094#define PERFETTO_LOG(fmt, ...) PERFETTO_XLOG(kLogInfo, fmt, ##__VA_ARGS__)
95#define PERFETTO_ILOG(fmt, ...) PERFETTO_XLOG(kLogImportant, fmt, ##__VA_ARGS__)
96#define PERFETTO_ELOG(fmt, ...) PERFETTO_XLOG(kLogError, fmt, ##__VA_ARGS__)
Florian Mayer2ab0d772018-01-17 15:53:17 +000097#define PERFETTO_FATAL(fmt, ...) \
98 do { \
99 PERFETTO_ELOG(fmt, ##__VA_ARGS__); \
100 PERFETTO_IMMEDIATE_CRASH(); \
101 } while (0)
Primiano Tucci5aab7582017-12-07 12:22:03 +0000102
Florian Mayer8ede4ac2018-03-27 13:13:56 +0100103#define PERFETTO_PLOG(x, ...) \
104 PERFETTO_ELOG(x " (errno: %d, %s)", ##__VA_ARGS__, errno, strerror(errno))
Sami Kyostila247110b2018-02-14 10:18:45 +0000105
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000106#if PERFETTO_DCHECK_IS_ON()
Primiano Tucci5aab7582017-12-07 12:22:03 +0000107
108#define PERFETTO_DLOG(fmt, ...) PERFETTO_XLOG(kLogDebug, fmt, ##__VA_ARGS__)
109
Florian Mayer8ede4ac2018-03-27 13:13:56 +0100110#define PERFETTO_DPLOG(x, ...) \
111 PERFETTO_DLOG(x " (errno: %d, %s)", ##__VA_ARGS__, errno, strerror(errno))
Primiano Tucci5aab7582017-12-07 12:22:03 +0000112
Florian Mayer8ede4ac2018-03-27 13:13:56 +0100113#define PERFETTO_DCHECK(x) \
114 do { \
115 if (PERFETTO_UNLIKELY(!(x))) { \
116 PERFETTO_DPLOG("%s", "PERFETTO_CHECK(" #x ")"); \
117 PERFETTO_IMMEDIATE_CRASH(); \
118 } \
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000119 } while (0)
Primiano Tucci5aab7582017-12-07 12:22:03 +0000120
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000121#else
Primiano Tucci5aab7582017-12-07 12:22:03 +0000122
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000123#define PERFETTO_DLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__)
124#define PERFETTO_DPLOG(...) ::perfetto::base::ignore_result(__VA_ARGS__)
125#define PERFETTO_DCHECK(x) ::perfetto::base::ignore_result(x)
Primiano Tucci5aab7582017-12-07 12:22:03 +0000126
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000127#endif // PERFETTO_DCHECK_IS_ON()
128
129#if PERFETTO_DCHECK_IS_ON()
130#define PERFETTO_CHECK(x) PERFETTO_DCHECK(x)
131#else
Primiano Tucci5aab7582017-12-07 12:22:03 +0000132#define PERFETTO_CHECK(x) \
133 do { \
Primiano Tucci19318a72018-02-22 12:33:02 +0000134 if (PERFETTO_UNLIKELY(!(x))) { \
Primiano Tucci5aab7582017-12-07 12:22:03 +0000135 PERFETTO_ELOG("%s", "PERFETTO_CHECK(" #x ")"); \
Florian Mayer2ab0d772018-01-17 15:53:17 +0000136 PERFETTO_IMMEDIATE_CRASH(); \
Primiano Tucci5aab7582017-12-07 12:22:03 +0000137 } \
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000138 } while (0)
Primiano Tucci5aab7582017-12-07 12:22:03 +0000139
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000140#endif // PERFETTO_DCHECK_IS_ON()
141
Primiano Tucci5aab7582017-12-07 12:22:03 +0000142} // namespace base
143} // namespace perfetto
144
Primiano Tucci4f9b6d72017-12-05 20:59:16 +0000145#endif // INCLUDE_PERFETTO_BASE_LOGGING_H_