• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 #include <cstdlib>      // for abort
3 #include <memory>       // for shared_ptr
4 #include <ostream>      // for ostream
5 #include <string_view>  // for string_view
6 
7 #include "aemu/base/logging/LogSeverity.h"  // for EMULATOR_LOG_INFO, EMULATOR_...
8 
9 #ifdef ANDROID_EMULATOR_BUILD
10 #include "android/utils/debug.h" // for __emu_log_print, VERBOSE_CHECK
11 #else
12 #include "aemu/base/logging/CLog.h"
13 #endif
14 
15 extern "C" void __blue_write_to_file(LogSeverity prio,
16                                      const char* file,
17                                      int line,
18                                      const char* fmt,
19                                      ...);
20 
21 namespace android::bluetooth {
22 // Gets a log stream that can be used to write logging information.
23 // The id can be used to uniquely identify the stream. If the id has
24 // already been used it will be prefixed by %d_ and a number.
25 std::shared_ptr<std::ostream> getLogstream(std::string_view id);
26 }  // namespace android::bluetooth
27 
28 // Note that we log both to a file as well as the emulator log system.
29 #ifdef ANDROID_EMULATOR_BUILD
30 #define LOGWRAPPER(level, fmt, args...)                                      \
31     do {                                                                     \
32         if (VERBOSE_CHECK(bluetooth)) {                                      \
33             __blue_write_to_file(level, __FILE__, __LINE__, fmt "", ##args); \
34             __emu_log_print(level, __FILE__, __LINE__, fmt "", ##args);      \
35         }                                                                    \
36     } while (false)
37 #else
38 #define LOGWRAPPER(level, fmt, args...)                                  \
39     do {                                                                 \
40         __blue_write_to_file(level, __FILE__, __LINE__, fmt "", ##args); \
41         __emu_log_print(level, __FILE__, __LINE__, fmt "", ##args);      \
42     } while (false)
43 #endif
44 
45 #define LOG_VERBOSE(fmt, args...) LOGWRAPPER(EMULATOR_LOG_VERBOSE, fmt, ##args);
46 #define LOG_DEBUG(fmt, args...) LOGWRAPPER(EMULATOR_LOG_DEBUG, fmt, ##args);
47 #define LOG_INFO(...) LOGWRAPPER(EMULATOR_LOG_INFO, __VA_ARGS__)
48 #define LOG_WARN(...) LOGWRAPPER(EMULATOR_LOG_WARNING, __VA_ARGS__)
49 #define LOG_ERROR(...) LOGWRAPPER(EMULATOR_LOG_ERROR, __VA_ARGS__)
50 
51 #ifndef LOG_ALWAYS_FATAL
52 #define LOG_ALWAYS_FATAL(...)                                                 \
53     do {                                                                      \
54         __emu_log_print(EMULATOR_LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__); \
55         std::abort();                                                         \
56     } while (false)
57 #endif
58 
59 #define ASSERT(condition)                                          \
60     do {                                                           \
61         if (!(condition)) {                                        \
62             LOG_ALWAYS_FATAL("assertion '" #condition "' failed"); \
63         }                                                          \
64     } while (false)
65 
66 #define ASSERT_LOG(condition, fmt, args...)                              \
67     do {                                                                 \
68         if (!(condition)) {                                              \
69             LOG_ALWAYS_FATAL("assertion '" #condition "' failed - " fmt, \
70                              ##args);                                    \
71         }                                                                \
72     } while (false)
73 
74 #ifndef CASE_RETURN_TEXT
75 #define CASE_RETURN_TEXT(code) \
76     case code:                 \
77         return #code
78 #endif
79