Skip to content

Commit 9418fad

Browse files
committed
Add new output sink to the logger which is a user configurable callback function.
1 parent 1baed0b commit 9418fad

File tree

17 files changed

+246
-4
lines changed

17 files changed

+246
-4
lines changed

include/ur_api.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ typedef enum ur_function_t {
226226
UR_FUNCTION_BINDLESS_IMAGES_IMPORT_EXTERNAL_MEMORY_EXP = 226, ///< Enumerator for ::urBindlessImagesImportExternalMemoryExp
227227
UR_FUNCTION_BINDLESS_IMAGES_IMPORT_EXTERNAL_SEMAPHORE_EXP = 227, ///< Enumerator for ::urBindlessImagesImportExternalSemaphoreExp
228228
UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP = 228, ///< Enumerator for ::urEnqueueNativeCommandExp
229+
UR_FUNCTION_LOADER_CONFIG_SET_LOGGER_CALLBACK = 229, ///< Enumerator for ::urLoaderConfigSetLoggerCallback
229230
/// @cond
230231
UR_FUNCTION_FORCE_UINT32 = 0x7fffffff
231232
/// @endcond
@@ -786,6 +787,30 @@ UR_APIEXPORT ur_result_t UR_APICALL
786787
urLoaderTearDown(
787788
void);
788789

790+
///////////////////////////////////////////////////////////////////////////////
791+
/// @brief Callback function to retrieve output from the logger.
792+
typedef void (*ur_logger_output_callback_t)(
793+
const char *pLoggerMsg, ///< [in][out] pointer to data to be passed to callback
794+
void *pUserData ///< [in][out] pointer to data to be passed to callback
795+
);
796+
797+
///////////////////////////////////////////////////////////////////////////////
798+
/// @brief Set a callback function for use by the logger to retrieve logging
799+
/// output.
800+
///
801+
/// @returns
802+
/// - ::UR_RESULT_SUCCESS
803+
/// - ::UR_RESULT_ERROR_UNINITIALIZED
804+
/// - ::UR_RESULT_ERROR_DEVICE_LOST
805+
/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC
806+
/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER
807+
/// + `NULL == pfnLoggerCallback`
808+
UR_APIEXPORT ur_result_t UR_APICALL
809+
urLoaderConfigSetLoggerCallback(
810+
ur_logger_output_callback_t pfnLoggerCallback, ///< [in] Function pointer to callback from the logger.
811+
void *pUserData ///< [in][out][optional] pointer to data to be passed to callback
812+
);
813+
789814
#if !defined(__GNUC__)
790815
#pragma endregion
791816
#endif
@@ -9642,6 +9667,15 @@ typedef struct ur_loader_config_set_code_location_callback_params_t {
96429667
void **ppUserData;
96439668
} ur_loader_config_set_code_location_callback_params_t;
96449669

9670+
///////////////////////////////////////////////////////////////////////////////
9671+
/// @brief Function parameters for urLoaderConfigSetLoggerCallback
9672+
/// @details Each entry is a pointer to the parameter passed to the function;
9673+
/// allowing the callback the ability to modify the parameter's value
9674+
typedef struct ur_loader_config_set_logger_callback_params_t {
9675+
ur_logger_output_callback_t *ppfnLoggerCallback;
9676+
void **ppUserData;
9677+
} ur_loader_config_set_logger_callback_params_t;
9678+
96459679
///////////////////////////////////////////////////////////////////////////////
96469680
/// @brief Function parameters for urPlatformGet
96479681
/// @details Each entry is a pointer to the parameter passed to the function;

include/ur_print.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urPrintLoaderConfigEnableLayerParams(const s
11061106
/// - `buff_size < out_size`
11071107
UR_APIEXPORT ur_result_t UR_APICALL urPrintLoaderConfigSetCodeLocationCallbackParams(const struct ur_loader_config_set_code_location_callback_params_t *params, char *buffer, const size_t buff_size, size_t *out_size);
11081108

1109+
///////////////////////////////////////////////////////////////////////////////
1110+
/// @brief Print ur_loader_config_set_logger_callback_params_t struct
1111+
/// @returns
1112+
/// - ::UR_RESULT_SUCCESS
1113+
/// - ::UR_RESULT_ERROR_INVALID_SIZE
1114+
/// - `buff_size < out_size`
1115+
UR_APIEXPORT ur_result_t UR_APICALL urPrintLoaderConfigSetLoggerCallbackParams(const struct ur_loader_config_set_logger_callback_params_t *params, char *buffer, const size_t buff_size, size_t *out_size);
1116+
11091117
///////////////////////////////////////////////////////////////////////////////
11101118
/// @brief Print ur_platform_get_params_t struct
11111119
/// @returns

include/ur_print.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,9 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_function_t value) {
942942
case UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP:
943943
os << "UR_FUNCTION_ENQUEUE_NATIVE_COMMAND_EXP";
944944
break;
945+
case UR_FUNCTION_LOADER_CONFIG_SET_LOGGER_CALLBACK:
946+
os << "UR_FUNCTION_LOADER_CONFIG_SET_LOGGER_CALLBACK";
947+
break;
945948
default:
946949
os << "unknown enumerator";
947950
break;
@@ -10266,6 +10269,26 @@ inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct
1026610269
return os;
1026710270
}
1026810271

10272+
///////////////////////////////////////////////////////////////////////////////
10273+
/// @brief Print operator for the ur_loader_config_set_logger_callback_params_t type
10274+
/// @returns
10275+
/// std::ostream &
10276+
inline std::ostream &operator<<(std::ostream &os, [[maybe_unused]] const struct ur_loader_config_set_logger_callback_params_t *params) {
10277+
10278+
os << ".pfnLoggerCallback = ";
10279+
10280+
os << reinterpret_cast<void *>(
10281+
*(params->ppfnLoggerCallback));
10282+
10283+
os << ", ";
10284+
os << ".pUserData = ";
10285+
10286+
ur::details::printPtr(os,
10287+
*(params->ppUserData));
10288+
10289+
return os;
10290+
}
10291+
1026910292
///////////////////////////////////////////////////////////////////////////////
1027010293
/// @brief Print operator for the ur_platform_get_params_t type
1027110294
/// @returns
@@ -17315,6 +17338,9 @@ inline ur_result_t UR_APICALL printFunctionParams(std::ostream &os, ur_function_
1731517338
case UR_FUNCTION_LOADER_CONFIG_SET_CODE_LOCATION_CALLBACK: {
1731617339
os << (const struct ur_loader_config_set_code_location_callback_params_t *)params;
1731717340
} break;
17341+
case UR_FUNCTION_LOADER_CONFIG_SET_LOGGER_CALLBACK: {
17342+
os << (const struct ur_loader_config_set_logger_callback_params_t *)params;
17343+
} break;
1731817344
case UR_FUNCTION_PLATFORM_GET: {
1731917345
os << (const struct ur_platform_get_params_t *)params;
1732017346
} break;

scripts/core/INTRO.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ By default, no messages are printed.
202202

203203
By default, there is a guarantee that *error* messages are flushed immediately. One can change this behavior to flush on lower-level messages.
204204

205-
Loggers redirect messages to *stdout*, *stderr*, or a file (default: *stderr*).
205+
Loggers redirect messages to *stdout*, *stderr*, a file or a user configurable callback function (default: *stderr*).
206206

207207
All of these logging options can be set with **UR_LOG_LOADER** and **UR_LOG_NULL** environment variables described in the **Environment Variables** section below.
208208
Both of these environment variables have the same syntax for setting logger options:
209209

210-
"[level:debug|info|warning|error];[flush:<debug|info|warning|error>];[output:stdout|stderr|file,<path>]"
210+
"[level:debug|info|warning|error];[flush:<debug|info|warning|error>];[output:stdout|stderr|file,<path>|callback]"
211211

212212
* level - a log level, meaning that only messages from this level and above are printed,
213213
possible values, from the lowest level to the highest one: *debug*, *info*, *warning*, *error*,

scripts/core/loader.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,29 @@ ordinal: "1"
225225
params: []
226226
returns:
227227
- $X_RESULT_ERROR_OUT_OF_HOST_MEMORY
228+
--- #--------------------------------------------------------------------------
229+
type: fptr_typedef
230+
desc: "Callback function to retrieve output from the logger."
231+
name: $x_logger_output_callback_t
232+
return: "void"
233+
params:
234+
- type: const char*
235+
name: pLoggerMsg
236+
desc: "[in][out] pointer to data to be passed to callback"
237+
- type: void*
238+
name: pUserData
239+
desc: "[in][out] pointer to data to be passed to callback"
240+
--- #--------------------------------------------------------------------------
241+
type: function
242+
desc: "Set a callback function for use by the logger to retrieve logging output."
243+
class: $xLoaderConfig
244+
loader_only: True
245+
name: SetLoggerCallback
246+
decl: static
247+
params:
248+
- type: $x_logger_output_callback_t
249+
name: pfnLoggerCallback
250+
desc: "[in] Function pointer to callback from the logger."
251+
- type: void*
252+
name: pUserData
253+
desc: "[in][out][optional] pointer to data to be passed to callback"

scripts/core/registry.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ etors:
592592
- name: ENQUEUE_NATIVE_COMMAND_EXP
593593
desc: Enumerator for $xEnqueueNativeCommandExp
594594
value: '228'
595+
- name: LOADER_CONFIG_SET_LOGGER_CALLBACK
596+
desc: Enumerator for $xLoaderConfigSetLoggerCallback
597+
value: '229'
595598
---
596599
type: enum
597600
desc: Defines structure types

source/common/logger/ur_logger_details.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,13 @@ class Logger {
101101
if (!sink) {
102102
return;
103103
}
104-
105104
if (isLegacySink) {
106105
sink->log(level, p.message, std::forward<Args>(args)...);
107106
return;
108107
}
109108
if (level < this->level) {
110109
return;
111110
}
112-
113111
sink->log(level, format, std::forward<Args>(args)...);
114112
}
115113

@@ -118,6 +116,22 @@ class Logger {
118116
this->sink = std::move(legacySink);
119117
}
120118

119+
void setCallbackSinkFunction(ur_logger_output_callback_t cb,
120+
void *pUserData) {
121+
logger::Sink *rawBasePtr = this->sink.release();
122+
logger::CallbackSink *derivedPtr =
123+
dynamic_cast<logger::CallbackSink *>(rawBasePtr);
124+
125+
if (derivedPtr) {
126+
derivedPtr->setCallback(cb, pUserData);
127+
128+
this->sink.reset(derivedPtr);
129+
} else {
130+
// output a failure here??
131+
this->sink.reset(rawBasePtr);
132+
}
133+
}
134+
121135
private:
122136
logger::Level level;
123137
std::unique_ptr<logger::Sink> sink;

source/common/logger/ur_sinks.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
#define UR_SINKS_HPP 1
88

99
#include <fstream>
10+
#include <functional>
1011
#include <iostream>
1112
#include <mutex>
1213
#include <sstream>
1314

15+
#include "ur_api.h"
1416
#include "ur_filesystem_resolved.hpp"
1517
#include "ur_level.hpp"
1618
#include "ur_print.hpp"
@@ -195,6 +197,37 @@ class FileSink : public Sink {
195197
std::ofstream ofstream;
196198
};
197199

200+
class CallbackSink : public Sink {
201+
public:
202+
CallbackSink(std::string logger_name, bool skip_prefix = false,
203+
bool skip_linebreak = false)
204+
: Sink(std::move(logger_name), skip_prefix, skip_linebreak) {}
205+
206+
CallbackSink(std::string logger_name, Level flush_lvl, bool skip_prefix,
207+
bool skip_linebreak)
208+
: CallbackSink(std::move(logger_name), skip_prefix, skip_linebreak) {
209+
this->flush_level = flush_lvl;
210+
}
211+
212+
~CallbackSink() = default;
213+
214+
void setCallback(ur_logger_output_callback_t cb, void *pUserData) {
215+
callback = cb;
216+
userData = pUserData;
217+
}
218+
219+
private:
220+
ur_logger_output_callback_t callback;
221+
void *userData;
222+
223+
virtual void print([[maybe_unused]] logger::Level level,
224+
const std::string &msg) {
225+
if (level >= flush_level) {
226+
callback(msg.c_str(), userData);
227+
}
228+
}
229+
};
230+
198231
inline std::unique_ptr<Sink> sink_from_str(std::string logger_name,
199232
std::string name,
200233
filesystem::path file_path = "",
@@ -209,6 +242,9 @@ inline std::unique_ptr<Sink> sink_from_str(std::string logger_name,
209242
} else if (name == "file" && !file_path.empty()) {
210243
return std::make_unique<logger::FileSink>(logger_name, file_path,
211244
skip_prefix, skip_linebreak);
245+
} else if (name == "callback" && !file_path.empty()) {
246+
return std::make_unique<logger::CallbackSink>(logger_name, skip_prefix,
247+
skip_linebreak);
212248
}
213249

214250
throw std::invalid_argument(

source/loader/loader.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ EXPORTS
142142
urLoaderConfigRelease
143143
urLoaderConfigRetain
144144
urLoaderConfigSetCodeLocationCallback
145+
urLoaderConfigSetLoggerCallback
145146
urLoaderInit
146147
urLoaderTearDown
147148
urMemBufferCreate
@@ -361,6 +362,7 @@ EXPORTS
361362
urPrintLoaderConfigReleaseParams
362363
urPrintLoaderConfigRetainParams
363364
urPrintLoaderConfigSetCodeLocationCallbackParams
365+
urPrintLoaderConfigSetLoggerCallbackParams
364366
urPrintLoaderInitParams
365367
urPrintLoaderTearDownParams
366368
urPrintMapFlags

source/loader/loader.map.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
urLoaderConfigRelease;
143143
urLoaderConfigRetain;
144144
urLoaderConfigSetCodeLocationCallback;
145+
urLoaderConfigSetLoggerCallback;
145146
urLoaderInit;
146147
urLoaderTearDown;
147148
urMemBufferCreate;
@@ -361,6 +362,7 @@
361362
urPrintLoaderConfigReleaseParams;
362363
urPrintLoaderConfigRetainParams;
363364
urPrintLoaderConfigSetCodeLocationCallbackParams;
365+
urPrintLoaderConfigSetLoggerCallbackParams;
364366
urPrintLoaderInitParams;
365367
urPrintLoaderTearDownParams;
366368
urPrintMapFlags;

0 commit comments

Comments
 (0)