-
Notifications
You must be signed in to change notification settings - Fork 697
Expand file tree
/
Copy pathlogging.h
More file actions
106 lines (90 loc) · 3.63 KB
/
logging.h
File metadata and controls
106 lines (90 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#pragma once
#include <android/log.h>
#include <fmt/format.h>
#include <array>
/**
* @file logging.h
* @brief Provides a lightweight logging framework using fmt.
*
*/
/// The tag used for all log messages from this library.
#ifndef LOG_TAG
#define LOG_TAG "VectorNative"
#endif
/**
* @def LOGV(fmt, ...)
* @brief Logs a verbose message. Compiled out in release builds.
* Includes file, line, and function information.
*/
/**
* @def LOGD(fmt, ...)
* @brief Logs a debug message. Compiled out in release builds.
* Includes file, line, and function information.
*/
/**
* @def LOGI(fmt, ...)
* @brief Logs an informational message.
*/
/**
* @def LOGW(fmt, ...)
* @brief Logs a warning message.
*/
/**
* @def LOGE(fmt, ...)
* @brief Logs an error message.
*/
/**
* @def LOGF(fmt, ...)
* @brief Logs a fatal error message.
*/
/**
* @def PLOGE(fmt, ...)
* @brief Logs an error message and appends the string representation of the
* current `errno` value.
*/
#ifdef LOG_DISABLED
#define LOGV(...) ((void)0)
#define LOGD(...) ((void)0)
#define LOGI(...) ((void)0)
#define LOGW(...) ((void)0)
#define LOGE(...) ((void)0)
#define LOGF(...) ((void)0)
#define PLOGE(...) ((void)0)
#else
namespace vector::native::detail {
template <typename... T>
inline void LogToAndroid(int prio, const char *tag, fmt::format_string<T...> fmt, T &&...args) {
// Using a stack-allocated buffer for performance.
std::array<char, 1024> buf{};
// format_to_n is safe against buffer overflows.
auto result = fmt::format_to_n(buf.data(), buf.size() - 1, fmt, std::forward<T>(args)...);
buf[result.size] = '\0';
__android_log_write(prio, tag, buf.data());
}
} // namespace vector::native::detail
#ifndef NDEBUG
#define LOGD(fmt, ...) \
::vector::native::detail::LogToAndroid(ANDROID_LOG_DEBUG, LOG_TAG, "{}:{} ({}): " fmt, \
__FILE_NAME__, __LINE__, \
__PRETTY_FUNCTION__ __VA_OPT__(, ) __VA_ARGS__)
#define LOGV(fmt, ...) \
::vector::native::detail::LogToAndroid(ANDROID_LOG_VERBOSE, LOG_TAG, \
fmt __VA_OPT__(, ) __VA_ARGS__)
#else
#define LOGV(...) ((void)0)
#define LOGD(...) ((void)0)
#endif
#define LOGI(fmt, ...) \
::vector::native::detail::LogToAndroid(ANDROID_LOG_INFO, LOG_TAG, \
fmt __VA_OPT__(, ) __VA_ARGS__)
#define LOGW(fmt, ...) \
::vector::native::detail::LogToAndroid(ANDROID_LOG_WARN, LOG_TAG, \
fmt __VA_OPT__(, ) __VA_ARGS__)
#define LOGE(fmt, ...) \
::vector::native::detail::LogToAndroid(ANDROID_LOG_ERROR, LOG_TAG, \
fmt __VA_OPT__(, ) __VA_ARGS__)
#define LOGF(fmt, ...) \
::vector::native::detail::LogToAndroid(ANDROID_LOG_FATAL, LOG_TAG, \
fmt __VA_OPT__(, ) __VA_ARGS__)
#define PLOGE(fmt, ...) LOGE(fmt " failed with error {}: {}", ##__VA_ARGS__, errno, strerror(errno))
#endif // LOG_DISABLED