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