Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions include/triton/core/tritonserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2139,6 +2139,42 @@ TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
TRITONSERVER_ServerOptionsSetLogVerbose(
struct TRITONSERVER_ServerOptions* options, int level);

/// Type for log callback function. When registered, this callback will be
/// invoked for each log message, allowing custom log handling (e.g.,
/// integration with external logging systems).
///
/// \param level The log level of the message.
/// \param filename The source file where the log message originated.
/// \param line The line number in the source file.
/// \param msg The log message content.
/// \param userp User-provided pointer passed to
/// TRITONSERVER_ServerOptionsSetLogCallback.
typedef void (*TRITONSERVER_LogCallbackFn_t)(
TRITONSERVER_LogLevel level, const char* filename, int line,
const char* msg, void* userp);

/// Set a callback function to receive log messages. This allows applications
/// to integrate Triton logging with their own logging infrastructure.
///
/// When a callback is set with 'replace_default_logger' as true, the callback
/// completely replaces the default logging behavior (writing to
/// stdout/stderr/file). When false, the callback is invoked in addition to
/// the default logging.
///
/// \param options The server options object.
/// \param callback_fn The callback function to invoke for each log message,
/// or nullptr to disable callback logging.
/// \param userp User-provided pointer that will be passed to the callback.
/// \param replace_default_logger If true, the callback replaces the default
/// logging behavior. If false, callback is called in addition to default
/// logging.
/// \return a TRITONSERVER_Error indicating success or failure.
TRITONSERVER_DECLSPEC struct TRITONSERVER_Error*
TRITONSERVER_ServerOptionsSetLogCallback(
struct TRITONSERVER_ServerOptions* options,
TRITONSERVER_LogCallbackFn_t callback_fn, void* userp,
bool replace_default_logger);

/// Enable or disable metrics collection in a server options.
///
/// \param options The server options object.
Expand Down
71 changes: 71 additions & 0 deletions src/tritonserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,77 @@ TRITONSERVER_ServerOptionsSetLogVerbose(
return nullptr; // Success
}

#ifdef TRITON_ENABLE_LOGGING
namespace {

// Context for bridging C callback to internal Logger callback
struct LogCallbackContext {
TRITONSERVER_LogCallbackFn_t user_callback;
void* userp;
};

// Global context (logging is global in Triton)
static LogCallbackContext* g_log_callback_context = nullptr;

// Internal callback wrapper that converts Logger::Level to TRITONSERVER_LogLevel
void LogCallbackWrapper(
triton::common::Logger::Level level, const char* filename, int line,
const char* msg, void* userp)
{
auto* ctx = static_cast<LogCallbackContext*>(userp);
if (ctx == nullptr || ctx->user_callback == nullptr) {
return;
}

// Convert internal log level to C API log level
TRITONSERVER_LogLevel api_level;
switch (level) {
case triton::common::Logger::Level::kERROR:
api_level = TRITONSERVER_LOG_ERROR;
break;
case triton::common::Logger::Level::kWARNING:
api_level = TRITONSERVER_LOG_WARN;
break;
case triton::common::Logger::Level::kINFO:
default:
api_level = TRITONSERVER_LOG_INFO;
break;
}

ctx->user_callback(api_level, filename, line, msg, ctx->userp);
}

} // namespace
#endif // TRITON_ENABLE_LOGGING

// Set a callback function to receive log messages.
TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_ServerOptionsSetLogCallback(
TRITONSERVER_ServerOptions* options, TRITONSERVER_LogCallbackFn_t callback_fn,
void* userp, bool replace_default_logger)
{
#ifdef TRITON_ENABLE_LOGGING
// Clean up existing context if any
if (g_log_callback_context != nullptr) {
delete g_log_callback_context;
g_log_callback_context = nullptr;
}

if (callback_fn != nullptr) {
// Create new context for the callback
g_log_callback_context = new LogCallbackContext{callback_fn, userp};
LOG_SET_CALLBACK(LogCallbackWrapper, g_log_callback_context, replace_default_logger);
} else {
// Disable callback logging
LOG_SET_CALLBACK(nullptr, nullptr, false);
}
return nullptr; // Success
#else
return TRITONSERVER_ErrorNew(
TRITONSERVER_ERROR_UNSUPPORTED, "logging not supported");
#endif // TRITON_ENABLE_LOGGING
}

TRITONAPI_DECLSPEC TRITONSERVER_Error*
TRITONSERVER_ServerOptionsSetLogFormat(
TRITONSERVER_ServerOptions* options, const TRITONSERVER_LogFormat format)
Expand Down
4 changes: 4 additions & 0 deletions src/tritonserver_stub.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ TRITONSERVER_ServerOptionsSetLogVerbose()
{
}
TRITONAPI_DECLSPEC void
TRITONSERVER_ServerOptionsSetLogCallback()
{
}
TRITONAPI_DECLSPEC void
TRITONSERVER_ServerOptionsSetLogFormat()
{
}
Expand Down