File indexing completed on 2026-05-27 07:24:05
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011
0012 #define DETRAY_EMPTY_LOG() \
0013 do { \
0014 } while (0)
0015
0016 #if DETRAY_LOG_LVL >= 0
0017
0018
0019 #include "detray/definitions/detail/qualifiers.hpp"
0020 #include "detray/utils/type_list.hpp"
0021
0022
0023 #include <cstring>
0024 #include <iomanip>
0025 #include <iostream>
0026 #include <regex>
0027 #include <source_location>
0028 #include <string>
0029
0030 namespace detray::log::detail {
0031
0032
0033
0034
0035 constexpr const char *source_file_name(const char *path) {
0036 const char *file = path;
0037 while (*path != 0) {
0038 if (*path++ == '/') {
0039 file = path;
0040 }
0041 }
0042 return file;
0043 }
0044
0045
0046 template <typename T>
0047 inline std::string_view process_typename() {
0048 static const std::string type_name = [] {
0049 std::string s{""};
0050 try {
0051 s = detray::types::demangle_type_name<T>();
0052 } catch (...) {
0053 return std::string{"unknown"};
0054 }
0055
0056 if (s.empty()) {
0057 return std::string{"unknown"};
0058 }
0059
0060 std::regex re{"detray::"};
0061 s = std::regex_replace(s, re, "");
0062
0063
0064
0065 std::regex re_list{R"(^types::list<(.*)>$)"};
0066
0067 if (std::smatch match; std::regex_match(s, match, re_list)) {
0068 s = "[" + std::string{match[1]} + "]";
0069 }
0070
0071 return s;
0072 }();
0073
0074 return type_name;
0075 }
0076
0077 }
0078
0079
0080 #if defined(__CUDACC__) || defined(__HIP__) || \
0081 defined(CL_SYCL_LANGUAGE_VERSION) || defined(SYCL_LANGUAGE_VERSION)
0082 #define __DEVICE_LOGGING__
0083 #endif
0084
0085
0086 #if defined(__CUDACC__)
0087 #define __BACKEND__ "CUDA"
0088 #elif defined(__HIP__)
0089 #define __BACKEND__ "HIP"
0090 #elif defined(CL_SYCL_LANGUAGE_VERSION) || defined(SYCL_LANGUAGE_VERSION)
0091 #define __BACKEND__ "SYCL"
0092 #else
0093 #define __BACKEND__ "HOST"
0094 #endif
0095
0096
0097 #ifdef __DEVICE_LOGGING__
0098 #define DETRAY_TYPENAME(type) "unknown type"
0099 #else
0100 #define DETRAY_TYPENAME(type) detray::log::detail::process_typename<type>()
0101 #endif
0102
0103
0104 #ifdef __DEVICE_LOGGING__
0105 #define __FILENAME__ detray::log::detail::source_file_name(__FILE__)
0106 #else
0107 #define __FILENAME__ \
0108 (std::strrchr(__FILE__, '/') ? std::strrchr(__FILE__, '/') + 1 : __FILE__)
0109 #endif
0110
0111
0112 #ifdef __DEVICE_LOGGING__
0113 #define DETRAY_LOG_VECTOR(x) ""
0114 #else
0115 #define DETRAY_LOG_VECTOR(x) \
0116 [&]() { \
0117 std::stringstream _vec_os; \
0118 std::size_t _vec_i = 0; \
0119 for (const auto &_vec_elem : x) { \
0120 if (_vec_i > 0) { \
0121 _vec_os << ", "; \
0122 } \
0123 _vec_os << _vec_elem; \
0124 _vec_i++; \
0125 } \
0126 return _vec_os.str(); \
0127 }()
0128 #endif
0129
0130
0131 #ifndef __DEVICE_LOGGING__
0132
0133 #define DETRAY_LOG_STREAM(stream, lib, lvl, x) \
0134 std::stream << lib << " " << std::left << std::setw(7) << lvl << std::left \
0135 << std::setw(9) << " (HOST):" << std::left << std::setw(29) \
0136 << __FILENAME__ << "l." << std::left << std::setw(5) \
0137 << std::source_location::current().line() << x << std::endl
0138
0139
0140 #define DETRAY_LOG_STREAM_STDOUT(lib, lvl, x) \
0141 DETRAY_LOG_STREAM(cout, lib, lvl, x)
0142
0143
0144 #define DETRAY_LOG_STREAM_STDERR(lib, lvl, x) \
0145 DETRAY_LOG_STREAM(clog, lib, lvl, x)
0146
0147 #else
0148 #define DETRAY_LOG_STREAM_STDOUT(lib, lvl, x) DETRAY_EMPTY_LOG()
0149 #define DETRAY_LOG_STREAM_STDERR(lib, lvl, x) DETRAY_EMPTY_LOG()
0150 #endif
0151
0152
0153
0154
0155
0156 #if !defined(CL_SYCL_LANGUAGE_VERSION) && !defined(SYCL_LANGUAGE_VERSION)
0157
0158 #ifdef __DEVICE_LOGGING__
0159
0160 #define DETRAY_LOG_PRINTF(lib, lvl, x, ...) \
0161 printf("%s %-7s (%s): %-29sl.%-5d" x "\n", lib, lvl, __BACKEND__, \
0162 __FILENAME__, __LINE__ __VA_OPT__(, ) __VA_ARGS__)
0163
0164 #define DETRAY_LOG_PRINTF_STDOUT(lib, lvl, x, ...) \
0165 DETRAY_LOG_PRINTF(lib, lvl, x, __VA_ARGS__)
0166
0167 #define DETRAY_LOG_PRINTF_STDERR(lib, lvl, x, ...) \
0168 DETRAY_LOG_PRINTF(lib, lvl, x, __VA_ARGS__)
0169
0170 #else
0171
0172 #define DETRAY_LOG_PRINTF(stream, lib, lvl, x, ...) \
0173 fprintf(stream, "%s %-7s (%s): %-29sl.%-5d" x "\n", lib, lvl, __BACKEND__, \
0174 __FILENAME__, __LINE__ __VA_OPT__(, ) __VA_ARGS__)
0175
0176 #define DETRAY_LOG_PRINTF_STDOUT(lib, lvl, x, ...) \
0177 DETRAY_LOG_PRINTF(stdout, lib, lvl, x, __VA_ARGS__)
0178
0179 #define DETRAY_LOG_PRINTF_STDERR(lib, lvl, x, ...) \
0180 DETRAY_LOG_PRINTF(stderr, lib, lvl, x, __VA_ARGS__)
0181
0182 #endif
0183
0184 #else
0185 #define DETRAY_LOG_PRINTF_STDOUT(lib, lvl, x, ...) DETRAY_EMPTY_LOG()
0186 #define DETRAY_LOG_PRINTF_STDERR(lib, lvl, x, ...) DETRAY_EMPTY_LOG()
0187 #endif
0188
0189 #else
0190 #define DETRAY_LOG_STREAM_STDOUT(lib, lvl, x) DETRAY_EMPTY_LOG()
0191 #define DETRAY_LOG_STREAM_STDERR(lib, lvl, x) DETRAY_EMPTY_LOG()
0192 #define DETRAY_LOG_PRINTF_STDOUT(lib, lvl, x, ...) DETRAY_EMPTY_LOG()
0193 #define DETRAY_LOG_PRINTF_STDERR(lib, lvl, x, ...) DETRAY_EMPTY_LOG()
0194 #define DETRAY_TYPENAME(type) DETRAY_EMPTY_LOG()
0195 #endif
0196
0197
0198
0199
0200
0201
0202 #define DETRAY_FATAL_STREAM(lib, x) DETRAY_LOG_STREAM_STDERR(lib, "FATAL", x)
0203 #define DETRAY_ERROR_STREAM(lib, x) DETRAY_LOG_STREAM_STDERR(lib, "ERROR", x)
0204 #define DETRAY_WARN_STREAM(lib, x) DETRAY_LOG_STREAM_STDERR(lib, "WARNING", x)
0205
0206 #define DETRAY_FATAL_PRINTF(lib, x, ...) \
0207 DETRAY_LOG_PRINTF_STDERR(lib, "FATAL", x, __VA_ARGS__)
0208 #define DETRAY_ERROR_PRINTF(lib, x, ...) \
0209 DETRAY_LOG_PRINTF_STDERR(lib, "ERROR", x, __VA_ARGS__)
0210 #define DETRAY_WARN_PRINTF(lib, x, ...) \
0211 DETRAY_LOG_PRINTF_STDERR(lib, "WARNING", x, __VA_ARGS__)
0212
0213
0214 #if DETRAY_LOG_LVL > 0
0215 #define DETRAY_INFO_STREAM(lib, x) DETRAY_LOG_STREAM_STDOUT(lib, "INFO", x)
0216 #define DETRAY_INFO_PRINTF(lib, x, ...) \
0217 DETRAY_LOG_PRINTF_STDOUT(lib, "INFO", x, __VA_ARGS__)
0218 #else
0219 #define DETRAY_INFO_STREAM(lib, x) DETRAY_EMPTY_LOG()
0220 #define DETRAY_INFO_PRINTF(lib, x, ...) DETRAY_EMPTY_LOG()
0221 #endif
0222
0223
0224 #if DETRAY_LOG_LVL > 1
0225 #define DETRAY_VERBOSE_STREAM(lib, x) \
0226 DETRAY_LOG_STREAM_STDERR(lib, "VERBOSE", x)
0227 #define DETRAY_VERBOSE_PRINTF(lib, x, ...) \
0228 DETRAY_LOG_PRINTF_STDERR(lib, "VERBOSE", x, __VA_ARGS__)
0229 #else
0230 #define DETRAY_VERBOSE_STREAM(lib, x) DETRAY_EMPTY_LOG()
0231 #define DETRAY_VERBOSE_PRINTF(lib, x, ...) DETRAY_EMPTY_LOG()
0232 #endif
0233
0234
0235 #if DETRAY_LOG_LVL > 2
0236 #define DETRAY_DEBUG_STREAM(lib, x) DETRAY_LOG_STREAM_STDERR(lib, "DEBUG", x)
0237 #define DETRAY_DEBUG_PRINTF(lib, x, ...) \
0238 DETRAY_LOG_PRINTF_STDERR(lib, "DEBUG", x, __VA_ARGS__)
0239 #else
0240 #define DETRAY_DEBUG_STREAM(lib, x) DETRAY_EMPTY_LOG()
0241 #define DETRAY_DEBUG_PRINTF(lib, x, ...) DETRAY_EMPTY_LOG()
0242 #endif