alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1/* Copyright 2025 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
9
10#include <mutex>
11#include <source_location>
12
14{
15 /** Log the entry and exit of a scope
16 *
17 * @attention It is suggested to use the logger macro ALPAKA_LOG_FUNCTION to speedup the compile time.
18 * For cases where logging is disabled the compiler does not need to register the C++ function signature.
19 *
20 * The time spend within the scope is added to the output as additional information, in milliseconds.
21 *
22 * @param logLvl log level or a sum of log levels
23 */
24 inline auto scope(
25 concepts::Level auto logLvl,
26 std::source_location const& location = std::source_location::current())
27 {
28 alpaka::unused(logLvl, location);
29#if defined(ALPAKA_LOG_STATIC)
30 if constexpr(logLvl.mask() & ALPAKA_LOG_STATIC_LVL_MASK)
31 return internal::Scoped{logLvl, location};
32 else
33 return internal::Scoped{logLvl};
34#elif defined(ALPAKA_LOG_DYNAMIC)
35 static std::once_flag flag;
36 static size_t envLogMask = 0;
37
38 std::call_once(
39 flag,
40 []()
41 {
42 if(char const* envStr = std::getenv("ALPAKA_LOG_DYNAMIC_LVL"))
43 envLogMask = std::stoull(envStr);
44 });
45
46 if(logLvl.mask() & envLogMask)
47 return internal::Scoped{logLvl, location};
48 else
49 return internal::Scoped{logLvl};
50#endif
51 }
52
53 /** Write a meta data message to the output
54 *
55 * @attention It is suggested to use the logger macro ALPAKA_LOG_INFO to speedup the compile time.
56 * For cases where logging is disabled the compiler does not need to register the C++ function signature.
57 *
58 * @param logLvl log level or a sum of log levels
59 * @param callable callable without arguments which provides a string which should be written to the output
60 */
61 inline void info(
62 concepts::Level auto logLvl,
63 auto const& callable,
64 std::source_location const& location = std::source_location::current())
65 {
66 alpaka::unused(logLvl, callable, location);
67#if defined(ALPAKA_LOG_STATIC)
68 if constexpr(logLvl.mask() & ALPAKA_LOG_STATIC_LVL_MASK)
69 internal::Info{logLvl, callable, location};
70#elif defined(ALPAKA_LOG_DYNAMIC)
71 static std::once_flag flag;
72 static size_t envLogMask = 0;
73
74 std::call_once(
75 flag,
76 []()
77 {
78 if(char const* envStr = std::getenv("ALPAKA_LOG_DYNAMIC_LVL"))
79 envLogMask = std::stoull(envStr);
80 });
81 if(logLvl.mask() & envLogMask)
82 internal::Info{logLvl, callable, location};
83#endif
84 }
85} // namespace alpaka::onHost::logger
86
87/** Log the entry and exit of a scope
88 *
89 * @param logLvl log level or a sum of log levels
90 */
91#if defined(ALPAKA_ENABLE_LOG_FUNCTIONS)
92# define ALPAKA_LOG_FUNCTION(logLvl) \
93 [[maybe_unused]] auto const __alpaka_log_scope = ::alpaka::onHost::logger::scope(logLvl)
94#else
95# define ALPAKA_LOG_FUNCTION(logLvl) void()
96#endif
97
98/** Write a meta data message to the output
99 *
100 * @param logLvl log level or a sum of log levels
101 * @param callable callable without arguments which provides a string which should be written to the output
102 */
103#if defined(ALPAKA_ENABLE_LOG_INFO)
104# define ALPAKA_LOG_INFO(logLvl, callable) ::alpaka::onHost::logger::info(logLvl, callable)
105#else
106# define ALPAKA_LOG_INFO(logLvl, callable) void()
107#endif
Concept for log level types.
Definition lvl.hpp:51
void info(concepts::Level auto logLvl, auto const &callable, std::source_location const &location=std::source_location::current())
Write a meta data message to the output.
Definition logger.hpp:61
auto scope(concepts::Level auto logLvl, std::source_location const &location=std::source_location::current())
Log the entry and exit of a scope.
Definition logger.hpp:24
Write a meta data message to the output.
Definition logger.hpp:171
Log the entry and exit of a scope.
Definition logger.hpp:120