Kompute
Logger.hpp
1 #pragma once
2 
3 #define KOMPUTE_LOG_LEVEL_TRACE 0
4 #define KOMPUTE_LOG_LEVEL_DEBUG 1
5 #define KOMPUTE_LOG_LEVEL_INFO 2
6 #define KOMPUTE_LOG_LEVEL_WARN 3
7 #define KOMPUTE_LOG_LEVEL_ERROR 4
8 #define KOMPUTE_LOG_LEVEL_CRITICAL 5
9 #define KOMPUTE_LOG_LEVEL_OFF 6
10 
11 // Logging is disabled entirely.
12 #if KOMPUTE_OPT_LOG_LEVEL_DISABLED
13 #define KP_LOG_TRACE(...)
14 #define KP_LOG_DEBUG(...)
15 #define KP_LOG_INFO(...)
16 #define KP_LOG_WARN(...)
17 #define KP_LOG_ERROR(...)
18 #else
19 
20 #if !KOMPUTE_OPT_USE_SPDLOG
21 #if VK_USE_PLATFORM_ANDROID_KHR
22 #include <android/log.h>
23 #include <fmt/core.h>
24 static const char* KOMPUTE_LOG_TAG = "KomputeLog";
25 #else
26 #if KOMPUTE_BUILD_PYTHON
27 #include <pybind11/pybind11.h>
28 namespace py = pybind11;
29 // from python/src/main.cpp
30 extern py::object kp_trace, kp_debug, kp_info, kp_warning, kp_error;
31 #else
32 #include <fmt/core.h>
33 #endif // KOMPUTE_BUILD_PYTHON
34 #endif // VK_USE_PLATFORM_ANDROID_KHR
35 #else
36 #include <spdlog/spdlog.h>
37 #endif // !KOMPUTE_OPT_USE_SPDLOG
38 #include <set>
39 #include <string>
40 #include <vector>
41 namespace logger {
42 // Setup the logger, note the loglevel can not be set below the CMake log level
43 // (To change this use -DKOMPUTE_OPT_LOG_LEVEL=...)
44 void
45 setupLogger();
46 
47 // Logging is enabled, but we do not use Spdlog. So we use fmt in case nothing
48 // else is defined, overriding logging.
49 #if !KOMPUTE_OPT_USE_SPDLOG
50 
51 #ifndef KP_LOG_TRACE
52 #if KOMPUTE_OPT_ACTIVE_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_TRACE
53 #if VK_USE_PLATFORM_ANDROID_KHR
54 #define KP_LOG_TRACE(...) \
55  ((void)__android_log_write( \
56  ANDROID_LOG_VERBOSE, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
57 #else
58 #if KOMPUTE_BUILD_PYTHON
59 #define KP_LOG_DEBUG(...) kp_trace(fmt::format(__VA_ARGS__))
60 #else
61 #define KP_LOG_TRACE(...) \
62  fmt::print("[{} {}] [trace] [{}:{}] {}\n", \
63  __DATE__, \
64  __TIME__, \
65  __FILE__, \
66  __LINE__, \
67  fmt::format(__VA_ARGS__))
68 #endif // KOMPUTE_BUILD_PYTHON
69 #endif // VK_USE_PLATFORM_ANDROID_KHR
70 #else
71 #define KP_LOG_TRACE(...)
72 #endif
73 #endif // !KP_LOG_TRACE
74 
75 #ifndef KP_LOG_DEBUG
76 #if KOMPUTE_OPT_ACTIVE_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_DEBUG
77 #if VK_USE_PLATFORM_ANDROID_KHR
78 #define KP_LOG_DEBUG(...) \
79  ((void)__android_log_write( \
80  ANDROID_LOG_DEBUG, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
81 #else
82 #if KOMPUTE_BUILD_PYTHON
83 #define KP_LOG_DEBUG(...) kp_debug(fmt::format(__VA_ARGS__))
84 #else
85 #ifdef __FILE_NAME__ // gcc 12 provides only file name without path
86 #define KP_LOG_DEBUG(...) \
87  fmt::print("[{} {}] [debug] [{}:{}] {}\n", \
88  __DATE__, \
89  __TIME__, \
90  __FILE_NAME__, \
91  __LINE__, \
92  fmt::format(__VA_ARGS__))
93 #else
94 #define KP_LOG_DEBUG(...) \
95  fmt::print("[{} {}] [debug] [{}:{}] {}\n", \
96  __DATE__, \
97  __TIME__, \
98  __FILE__, \
99  __LINE__, \
100  fmt::format(__VA_ARGS__))
101 #endif // __FILE__NAME__
102 #endif // KOMPUTE_BUILD_PYTHON
103 #endif // VK_USE_PLATFORM_ANDROID_KHR
104 #else
105 #define KP_LOG_DEBUG(...)
106 #endif
107 #endif // !KP_LOG_DEBUG
108 
109 #ifndef KP_LOG_INFO
110 #if KOMPUTE_OPT_ACTIVE_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_INFO
111 #if VK_USE_PLATFORM_ANDROID_KHR
112 #define KP_LOG_INFO(...) \
113  ((void)__android_log_write( \
114  ANDROID_LOG_INFO, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
115 #else
116 #if KOMPUTE_BUILD_PYTHON
117 #define KP_LOG_DEBUG(...) kp_info(fmt::format(__VA_ARGS__))
118 #else
119 #define KP_LOG_INFO(...) \
120  fmt::print("[{} {}] [info] [{}:{}] {}\n", \
121  __DATE__, \
122  __TIME__, \
123  __FILE__, \
124  __LINE__, \
125  fmt::format(__VA_ARGS__))
126 #endif // KOMPUTE_BUILD_PYTHON
127 #endif // VK_USE_PLATFORM_ANDROID_KHR
128 #else
129 #define KP_LOG_INFO(...)
130 #endif
131 #endif // !KP_LOG_INFO
132 
133 #ifndef KP_LOG_WARN
134 #if KOMPUTE_OPT_ACTIVE_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_WARN
135 #if VK_USE_PLATFORM_ANDROID_KHR
136 #define KP_LOG_WARN(...) \
137  ((void)__android_log_write( \
138  ANDROID_LOG_WARN, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
139 #else
140 #if KOMPUTE_BUILD_PYTHON
141 #define KP_LOG_DEBUG(...) kp_warning(fmt::format(__VA_ARGS__))
142 #else
143 #define KP_LOG_WARN(...) \
144  fmt::print("[{} {}] [warn] [{}:{}] {}\n", \
145  __DATE__, \
146  __TIME__, \
147  __FILE__, \
148  __LINE__, \
149  fmt::format(__VA_ARGS__))
150 #endif // KOMPUTE_BUILD_PYTHON
151 #endif // VK_USE_PLATFORM_ANDROID_KHR
152 #else
153 #define KP_LOG_WARN(...)
154 #endif
155 #endif // !KP_LOG_WARN
156 
157 #ifndef KP_LOG_ERROR
158 #if KOMPUTE_OPT_ACTIVE_LOG_LEVEL <= KOMPUTE_LOG_LEVEL_ERROR
159 #if VK_USE_PLATFORM_ANDROID_KHR
160 #define KP_LOG_ERROR(...) \
161  ((void)__android_log_write( \
162  ANDROID_LOG_ERROR, KOMPUTE_LOG_TAG, fmt::format(__VA_ARGS__).c_str()))
163 #else
164 #if KOMPUTE_BUILD_PYTHON
165 #define KP_LOG_DEBUG(...) kp_error(fmt::format(__VA_ARGS__))
166 #else
167 #define KP_LOG_ERROR(...) \
168  fmt::print("[{} {}] [error] [{}:{}] {}\n", \
169  __DATE__, \
170  __TIME__, \
171  __FILE__, \
172  __LINE__, \
173  fmt::format(__VA_ARGS__))
174 #endif // KOMPUTE_BUILD_PYTHON
175 #endif // VK_USE_PLATFORM_ANDROID_KHR
176 #else
177 #define KP_LOG_ERROR(...)
178 #endif
179 #endif // !KP_LOG_ERROR
180 #else
181 
182 #define KP_LOG_TRACE(...) SPDLOG_TRACE(__VA_ARGS__)
183 #define KP_LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__)
184 #define KP_LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__)
185 #define KP_LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__)
186 #define KP_LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__)
187 
188 void
189 setLogLevel(spdlog::level::level_enum level);
190 
191 spdlog::level::level_enum
192 getLogLevel();
193 
194 #endif // !KOMPUTE_OPT_USE_SPDLOG
195 } // namespace logger
196 
197 #endif // KOMPUTE_OPT_LOG_LEVEL_DISABLED