xref: /aosp_15_r20/external/cronet/third_party/jni_zero/logging.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef JNI_ZERO_LOGGING_H_
6 #define JNI_ZERO_LOGGING_H_
7 
8 #include "third_party/jni_zero/jni_export.h"
9 
10 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
11 #define JNI_ZERO_DCHECK_IS_ON() false
12 #else
13 #define JNI_ZERO_DCHECK_IS_ON() true
14 #endif
15 
16 // Simplified version of Google's logging. Adapted from perfetto's
17 // implementation.
18 namespace jni_zero {
19 
20 // Constexpr functions to extract basename(__FILE__), e.g.: ../foo/f.c -> f.c .
StrEnd(const char * s)21 constexpr const char* StrEnd(const char* s) {
22   return *s ? StrEnd(s + 1) : s;
23 }
24 
BasenameRecursive(const char * s,const char * begin,const char * end)25 constexpr const char* BasenameRecursive(const char* s,
26                                         const char* begin,
27                                         const char* end) {
28   return (*s == '/' && s < end)
29              ? (s + 1)
30              : ((s > begin) ? BasenameRecursive(s - 1, begin, end) : s);
31 }
32 
Basename(const char * str)33 constexpr const char* Basename(const char* str) {
34   return BasenameRecursive(StrEnd(str), str, StrEnd(str));
35 }
36 
37 enum LogLev { kLogInfo = 0, kLogError, kLogFatal };
38 
39 struct LogMessageCallbackArgs {
40   LogLev level;
41   int line;
42   const char* filename;
43   const char* message;
44 };
45 
46 using LogMessageCallback = void (*)(LogMessageCallbackArgs);
47 
48 // This is not thread safe and must be called before using tracing from other
49 // threads.
50 JNI_ZERO_COMPONENT_BUILD_EXPORT void SetLogMessageCallback(
51     LogMessageCallback callback);
52 
53 JNI_ZERO_COMPONENT_BUILD_EXPORT void LogMessage(LogLev,
54                                                 const char* fname,
55                                                 int line,
56                                                 const char* fmt,
57                                                 ...)
58     __attribute__((__format__(__printf__, 4, 5)));
59 
60 #define JNI_ZERO_IMMEDIATE_CRASH() \
61   do {                             \
62     __builtin_trap();              \
63     __builtin_unreachable();       \
64   } while (0)
65 #define JNI_ZERO_XLOG(level, fmt, ...)                                         \
66   ::jni_zero::LogMessage(level, ::jni_zero::Basename(__FILE__), __LINE__, fmt, \
67                          ##__VA_ARGS__)
68 #define JNI_ZERO_ILOG(fmt, ...) \
69   JNI_ZERO_XLOG(::jni_zero::kLogInfo, fmt, ##__VA_ARGS__)
70 #define JNI_ZERO_ELOG(fmt, ...) \
71   JNI_ZERO_XLOG(::jni_zero::kLogError, fmt, ##__VA_ARGS__)
72 #define JNI_ZERO_FLOG(fmt, ...) \
73   JNI_ZERO_XLOG(::jni_zero::kLogFatal, fmt, ##__VA_ARGS__)
74 
75 #define JNI_ZERO_CHECK(x)                            \
76   do {                                               \
77     if (__builtin_expect(!(x), 0)) {                 \
78       JNI_ZERO_FLOG("%s", "JNI_ZERO_CHECK(" #x ")"); \
79     }                                                \
80   } while (0)
81 #if JNI_ZERO_DCHECK_IS_ON()
82 #define JNI_ZERO_DCHECK(x) JNI_ZERO_CHECK(x)
83 #else
84 #define JNI_ZERO_DCHECK(x) \
85   do {                     \
86   } while (false && (x))
87 #endif
88 }  // namespace jni_zero
89 
90 #endif  // JNI_ZERO_LOGGING_H_
91