Bifrost SDK
Bifrost SDK documentation
RuntimeServices.h
Go to the documentation of this file.
1//-
2// =============================================================================
3// Copyright 2025 Autodesk, Inc. All rights reserved.
4//
5// Use of this software is subject to the terms of the Autodesk license
6// agreement provided at the time of installation or download, or which
7// otherwise accompanies this software in either electronic or hard copy form.
8// =============================================================================
9
10//+
11
14
15#ifndef AMINO_CORE_RUNTIME_SERVICES_H
16#define AMINO_CORE_RUNTIME_SERVICES_H
17
18#include "CoreExport.h"
19
22
23#include "internal/ConfigMacros.h"
24#include "internal/Storage.h"
25
26#include <type_traits>
27
28//==============================================================================
29// NAMESPACE Amino
30//==============================================================================
31
32namespace Amino {
33
34// Forward declarations
35class RuntimeMessage;
36class RuntimeServices;
37struct SdkStorage;
38
39//==============================================================================
40// CLASS RuntimeServices
41//==============================================================================
42
104private:
105 template <typename T, typename R = void>
106 using enable_if_functor =
107 std::enable_if_t<!std::is_convertible<T const&, StringView>::value, R>;
108
109public:
110 /*----- types -----*/
111
112 using LogCallback = void (*)(
113 RuntimeMessageCategory category,
114 Amino::StringView const& message,
115 void* clientData);
116
117 class ProfilerGuard;
118
119 /*----- static member functions -----*/
120
128 static AMINO_CORE_SHARED_DECL RuntimeServices const& getDisabled();
129
130 /*----- member functions -----*/
131
135 template <typename OStream>
136 explicit AMINO_INTERNAL_FORCEINLINE RuntimeServices(OStream* os)
138
139 template <typename OStream>
140 AMINO_INTERNAL_FORCEINLINE RuntimeServices(
141 OStream* os, RuntimeMessageCategory logLevel)
142 : RuntimeServices(&callback<OStream>, os, logLevel) {}
144
147 AMINO_CORE_SHARED_DECL RuntimeServices(
148 LogCallback cb,
149 void* clientData,
151 bool allowConcurrentCalls = false);
152
155 RuntimeServices(const RuntimeServices& rhs) = delete;
156 RuntimeServices(RuntimeServices&& rhs) noexcept = delete;
158 RuntimeServices& operator=(RuntimeServices&& rhs) noexcept = delete;
160
162 AMINO_CORE_SHARED_DECL ~RuntimeServices();
163
165 AMINO_CORE_SHARED_DECL bool isLogEnabled(RuntimeMessageCategory cat) const;
166
168 bool isLogInfoEnabled() const {
170 }
171
173 bool isLogWarningEnabled() const {
175 }
176
178 bool isLogErrorEnabled() const {
180 }
181
191 template <typename MsgFunc>
192 AMINO_INTERNAL_FORCEINLINE enable_if_functor<MsgFunc> //
193 log(RuntimeMessageCategory category, MsgFunc const& msgFunc) const {
194 using StringLike = std::remove_reference_t<decltype(msgFunc())>;
195 static_assert(
196 (std::is_constructible<StringView, StringLike const&>::value ||
197 std::is_convertible<StringLike const&, StringView>::value),
198 "msgFunc must return a type convertible to StringView");
199 if (isLogEnabled(category)) {
200 internal_log(true, category, msgFunc());
201 }
202 }
203 void log(RuntimeMessageCategory category, StringView const& message) const {
204 bool const isEnabled = isLogEnabled(category);
205 assert(isEnabled && "Logging is disabled!");
206 internal_log(isEnabled, category, message);
207 }
209
211 template <typename Msg>
212 AMINO_INTERNAL_FORCEINLINE void logInfo(Msg const& msg) const {
214 }
215
217 template <typename Msg>
218 AMINO_INTERNAL_FORCEINLINE void logWarning(Msg const& msg) const {
220 }
221
223 template <typename Msg>
224 AMINO_INTERNAL_FORCEINLINE enable_if_functor<Msg> logError(
225 Msg const& msg) const {
227 }
228
238 AMINO_CORE_SHARED_DECL void logError(StringView msg) const;
239
245 AMINO_CORE_SHARED_DECL int profilerEventBegin() const;
246
252 AMINO_CORE_SHARED_DECL int profilerEventBegin(const char* eventName) const;
253
256 AMINO_CORE_SHARED_DECL void profilerEventEnd(int startEventID) const;
257
258private:
259 struct Private {};
260 AMINO_CORE_SHARED_DECL explicit RuntimeServices(Private);
261
263 template <typename OStream>
264 static void callback(
265 RuntimeMessageCategory category,
266 Amino::StringView const& message,
267 void* clientData) {
268 auto& os = *static_cast<OStream*>(clientData);
269 auto prefix = getCategoryPrefix(category);
270 os.write(prefix.data(), prefix.size());
271 os.write(message.data(), message.size());
272 os << '\n';
273 }
274
279 AMINO_CORE_SHARED_DECL static StringView getCategoryPrefix(
280 RuntimeMessageCategory category);
281
284 AMINO_CORE_SHARED_DECL
285 void internal_log(
286 bool enabled, RuntimeMessageCategory cat, StringView message) const;
287
288 template <typename StringLike>
289 AMINO_INTERNAL_FORCEINLINE
290 std::enable_if_t<!std::is_convertible<StringLike, StringView>::value>
291 internal_log(
292 bool enabled,
294 StringLike&& message) const {
295 return internal_log(enabled, cat, StringView{message});
296 }
298
299 friend SdkStorage;
300 Internal::Storage_t<5> m_storage{};
301};
302
303//==============================================================================
304// CLASS RuntimeServices::ProfilerGuard
305//==============================================================================
306
331public:
336 ProfilerGuard(RuntimeServices const& rs, const char* eventName)
337 : m_rs(rs), m_id(rs.profilerEventBegin(eventName)) {}
338 explicit ProfilerGuard(RuntimeServices const& rs)
339 : m_rs(rs), m_id(rs.profilerEventBegin()) {}
341
345 ~ProfilerGuard() { m_rs.profilerEventEnd(m_id); } // LCOV_EXCL_BR_LINE
346
347private:
348 RuntimeServices const& m_rs;
349 int m_id;
350};
351} // namespace Amino
352
353#endif // AMINO_CORE_RUNTIME_SERVICES_H
Definition of macros for symbol visibility.
String view class (similar to std::string_view)
Definition: HostData.h:33
RuntimeMessageCategory
Category of the message logged at runtime (when executing a graph).
@ kInfo
The message provides information to the user.
Runtime service that is used by operators.
AMINO_CORE_SHARED_DECL bool isLogEnabled(RuntimeMessageCategory cat) const
Check if logging is enabled for a specific category.
AMINO_CORE_SHARED_DECL void logError(StringView msg) const
Log the given error message if logging of errors is enabled.
bool isLogErrorEnabled() const
Short-hand for isLogEnabled(RuntimeMessageCategory::kError)
void log(RuntimeMessageCategory category, StringView const &message) const
Log a message with a specific category.
RuntimeServices(RuntimeServices &&rhs) noexcept=delete
Copying and moving RuntimeServices is prohibited.
AMINO_CORE_SHARED_DECL RuntimeServices(LogCallback cb, void *clientData, RuntimeMessageCategory logLevel=RuntimeMessageCategory::kInfo, bool allowConcurrentCalls=false)
Constructs a RuntimeServices that calls the given callback when messages are logged.
AMINO_INTERNAL_FORCEINLINE enable_if_functor< Msg > logError(Msg const &msg) const
Short-hand for log(RuntimeMessageCategory::kError, msg)
RuntimeServices(const RuntimeServices &rhs)=delete
Copying and moving RuntimeServices is prohibited.
AMINO_CORE_SHARED_DECL ~RuntimeServices()
Destructor.
AMINO_INTERNAL_FORCEINLINE void logInfo(Msg const &msg) const
Short-hand for log(RuntimeMessageCategory::kInfo, msg)
AMINO_CORE_SHARED_DECL int profilerEventBegin(const char *eventName) const
Notify profiler about beginning of a new named event.
AMINO_INTERNAL_FORCEINLINE void logWarning(Msg const &msg) const
Short-hand for log(RuntimeMessageCategory::kWarning, msg)
void(*)(RuntimeMessageCategory category, Amino::StringView const &message, void *clientData) LogCallback
AMINO_INTERNAL_FORCEINLINE RuntimeServices(OStream *os)
Constructs a RuntimeServices that logs messages to the given output stream when messages are logged.
AMINO_INTERNAL_FORCEINLINE RuntimeServices(OStream *os, RuntimeMessageCategory logLevel)
Constructs a RuntimeServices that logs messages to the given output stream when messages are logged.
bool isLogWarningEnabled() const
Short-hand for isLogEnabled(RuntimeMessageCategory::kWarning)
bool isLogInfoEnabled() const
Short-hand for isLogEnabled(RuntimeMessageCategory::kInfo)
AMINO_CORE_SHARED_DECL int profilerEventBegin() const
Notify profiler about beginning of a new named event.
AMINO_CORE_SHARED_DECL void profilerEventEnd(int startEventID) const
Notify profiler about end of an event.
RuntimeServices & operator=(const RuntimeServices &rhs)=delete
Copying and moving RuntimeServices is prohibited.
static AMINO_CORE_SHARED_DECL RuntimeServices const & getDisabled()
Get reference to a "disabled" runtime services.
AMINO_INTERNAL_FORCEINLINE enable_if_functor< MsgFunc > log(RuntimeMessageCategory category, MsgFunc const &msgFunc) const
Log a message with a specific category.
RuntimeServices & operator=(RuntimeServices &&rhs) noexcept=delete
Copying and moving RuntimeServices is prohibited.
Helper class to ensure all profiler events have matching begin/end calls.
ProfilerGuard(RuntimeServices const &rs, const char *eventName)
Constructor.
ProfilerGuard(RuntimeServices const &rs)
Constructor.
String view class (similar to std::string_view).
Definition: StringView.h:42
constexpr char const * data() const noexcept
Get the string view data.
Definition: StringView.h:109
constexpr size_t size() const noexcept
Get the size of the string view.
Definition: StringView.h:114