Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:05

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 // Empty log output, to be removed by the compiler
0012 #define DETRAY_EMPTY_LOG() \
0013   do {                     \
0014   } while (0)
0015 
0016 #if DETRAY_LOG_LVL >= 0
0017 
0018 // Project include(s)
0019 #include "detray/definitions/detail/qualifiers.hpp"
0020 #include "detray/utils/type_list.hpp"
0021 
0022 // System include(s)
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 /// @returns the name of the current source file without the full path
0033 /// @see
0034 /// https://stackoverflow.com/questions/31050113/how-to-extract-the-source-filename-without-path-and-suffix-at-compile-time
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 /// Print a type name for type @tparam T in the logs
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     // Special case type-list for readability
0064     // Only attempt if the full type is a list
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 }  // namespace detray::log::detail
0078 
0079 // Enable device-side logging
0080 #if defined(__CUDACC__) || defined(__HIP__) || \
0081     defined(CL_SYCL_LANGUAGE_VERSION) || defined(SYCL_LANGUAGE_VERSION)
0082 #define __DEVICE_LOGGING__
0083 #endif
0084 
0085 // String that represents the backend that emitted the log message
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 // Convert type to string
0097 #ifdef __DEVICE_LOGGING__
0098 #define DETRAY_TYPENAME(type) "unknown type"
0099 #else  // ifdef __DEVICE_LOGGING__
0100 #define DETRAY_TYPENAME(type) detray::log::detail::process_typename<type>()
0101 #endif  // ifdef __DEVICE_LOGGING__
0102 
0103 // Get the file name
0104 #ifdef __DEVICE_LOGGING__
0105 #define __FILENAME__ detray::log::detail::source_file_name(__FILE__)
0106 #else  // ifdef __DEVICE_LOGGING__
0107 #define __FILENAME__ \
0108   (std::strrchr(__FILE__, '/') ? std::strrchr(__FILE__, '/') + 1 : __FILE__)
0109 #endif  // ifdef __DEVICE_LOGGING__
0110 
0111 // Convert a vector to string
0112 #ifdef __DEVICE_LOGGING__
0113 #define DETRAY_LOG_VECTOR(x) ""
0114 #else  // ifdef __DEVICE_LOGGING__
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  // ifdef __DEVICE_LOGGING__
0129 
0130 // Print 'x' in the host logs only
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 // Log output to stdout
0140 #define DETRAY_LOG_STREAM_STDOUT(lib, lvl, x) \
0141   DETRAY_LOG_STREAM(cout, lib, lvl, x)
0142 
0143 // Log output to stderr
0144 #define DETRAY_LOG_STREAM_STDERR(lib, lvl, x) \
0145   DETRAY_LOG_STREAM(clog, lib, lvl, x)
0146 
0147 #else  // ifndef __DEVICE_LOGGING__
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  // ifndef __DEVICE_LOGGING__
0151 
0152 // Print 'x' in printf
0153 // @note 'x' is the format string and the variadic arguments the corresponding
0154 // values
0155 // @note logging is currently disabled for SYCL builds
0156 #if !defined(CL_SYCL_LANGUAGE_VERSION) && !defined(SYCL_LANGUAGE_VERSION)
0157 // TODO: Move to device specific header for e.g. rate limiting by thread idx
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  // ifdef __DEVICE_LOGGING__
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  // ifdef __DEVICE_LOGGING__
0183 
0184 #else  // ifndef SYCL
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  // ifndef SYCL
0188 
0189 #else  // DETRAY_LOG_LVL < 0
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  // if DETRAY_LOG_LVL >= 0
0196 
0197 //
0198 // Define log levels
0199 //
0200 
0201 // Errors and warnings
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 // Info
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  // DETRAY_LOG_LVL > 0
0219 #define DETRAY_INFO_STREAM(lib, x) DETRAY_EMPTY_LOG()
0220 #define DETRAY_INFO_PRINTF(lib, x, ...) DETRAY_EMPTY_LOG()
0221 #endif  // DETRAY_LOG_LVL > 0
0222 
0223 // Verbose
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  // DETRAY_LOG_LVL > 1
0230 #define DETRAY_VERBOSE_STREAM(lib, x) DETRAY_EMPTY_LOG()
0231 #define DETRAY_VERBOSE_PRINTF(lib, x, ...) DETRAY_EMPTY_LOG()
0232 #endif  // DETRAY_LOG_LVL > 1
0233 
0234 // Debug
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  // DETRAY_LOG_LVL > 2
0240 #define DETRAY_DEBUG_STREAM(lib, x) DETRAY_EMPTY_LOG()
0241 #define DETRAY_DEBUG_PRINTF(lib, x, ...) DETRAY_EMPTY_LOG()
0242 #endif  // DETRAY_LOG_LVL > 2