Skip to content

Commit 3bac249

Browse files
authored
Add logging of ORT version and build info. (#1405)
It is useful to be able to see the onnxruntime version and build info when switching between multiple onnxruntime shared libraries.
1 parent d12a331 commit 3bac249

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/logging.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
namespace Generators {
1111

1212
LogItems g_log;
13-
static std::ostream* gp_stream{&std::cerr};
13+
14+
static std::ostream*& GlobalLogStreamPtr() {
15+
static std::ostream* stream = &std::cerr;
16+
return stream;
17+
}
18+
1419
static std::unique_ptr<std::ofstream> gp_logfile;
1520
static CallbackFn gp_callback{};
1621

@@ -33,11 +38,11 @@ struct CallbackStream : std::ostream {
3338

3439
void SetLogStream() {
3540
if (gp_callback)
36-
gp_stream = &gp_callback_stream;
41+
GlobalLogStreamPtr() = &gp_callback_stream;
3742
else if (gp_logfile)
38-
gp_stream = gp_logfile.get();
43+
GlobalLogStreamPtr() = gp_logfile.get();
3944
else
40-
gp_stream = &std::cerr;
45+
GlobalLogStreamPtr() = &std::cerr;
4146
}
4247

4348
void SetLogBool(std::string_view name, bool value) {
@@ -122,10 +127,10 @@ std::ostream& Log(std::string_view label, std::string_view string) {
122127
assert(g_log.enabled);
123128

124129
// Warnings will be yellow, all other labels will be blue
125-
*gp_stream << SGR::Bold << (label == "warning" ? SGR::Bg_Yellow : SGR::Bg_Blue) << " " << label << " " << SGR::Reset << ' ';
130+
*GlobalLogStreamPtr() << SGR::Bold << (label == "warning" ? SGR::Bg_Yellow : SGR::Bg_Blue) << " " << label << " " << SGR::Reset << ' ';
126131
if (!string.empty())
127-
*gp_stream << string << std::endl;
128-
return *gp_stream;
132+
*GlobalLogStreamPtr() << string << std::endl;
133+
return *GlobalLogStreamPtr();
129134
}
130135

131136
std::ostream& Log(std::string_view label, const char* fmt, ...) {

src/models/onnxruntime_api.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ inline void InitApi() {
206206
Generators::SetLogBool("ort_lib", true);
207207
}
208208

209+
OrtApiBaseFn ort_api_base_fn{};
210+
209211
#if defined(__linux__) || defined(MACOS_USE_DLOPEN)
210212
// If the GenAI library links against the onnxruntime library, it will have a dependency on a specific
211213
// version of OrtGetApiBase.
@@ -257,18 +259,21 @@ inline void InitApi() {
257259
throw std::runtime_error(std::string("Failed to load onnxruntime. Set ORTGENAI_LOG_ORT_LIB envvar to enable detailed logging."));
258260
}
259261

260-
OrtApiBaseFn ort_api_base_fn = (OrtApiBaseFn)dlsym(ort_lib_handle, "OrtGetApiBase");
262+
ort_api_base_fn = (OrtApiBaseFn)dlsym(ort_lib_handle, "OrtGetApiBase");
261263
if (ort_api_base_fn == nullptr) {
262264
char* err = dlerror();
263265
throw std::runtime_error(std::string("Failed to load symbol OrtGetApiBase: ") + (err != nullptr ? err : "Unknown"));
264266
}
265267

266268
InitApiWithDynamicFn(ort_api_base_fn);
267269
#else // defined(__linux__) || defined(MACOS_USE_DLOPEN)
268-
api = OrtGetApiBase()->GetApi(ORT_API_VERSION);
270+
ort_api_base_fn = &OrtGetApiBase;
271+
api = ort_api_base_fn()->GetApi(ORT_API_VERSION);
269272
if (!api)
270273
throw std::runtime_error("Onnxruntime is installed but is too old, please install a newer version");
271274
#endif // defined(__linux__) || defined(MACOS_USE_DLOPEN)
275+
276+
LOG_INFO("ORT Version: %s. %s", ort_api_base_fn()->GetVersionString(), api->GetBuildInfoString());
272277
}
273278

274279
/** \brief All C++ methods that can fail will throw an exception of this type

0 commit comments

Comments
 (0)