Skip to content

Commit

Permalink
Support log to electron console
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Apr 18, 2024
1 parent 54f91ea commit 79e508e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion js/node/src/inference_session_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

Napi::FunctionReference InferenceSessionWrap::constructor;

const std::string LogLevelMap[] = {"V", "I", "W", "E", "F"};

void ConsoleLog(void *param, OrtLoggingLevel severity, const char *category, const char *logid,
const char *code_location, const char *message) {
auto consoleLog = reinterpret_cast<Napi::FunctionReference *>(param);
std::string logMessage = "[" + LogLevelMap[severity] + ":" + category + ", " + code_location + "] " + message;
consoleLog->Call({Napi::String::New(consoleLog->Env(), logMessage)});
}

Napi::Object InferenceSessionWrap::Init(Napi::Env env, Napi::Object exports) {
#if defined(USE_DML) && defined(_WIN32)
LoadDirectMLDll(env);
Expand All @@ -23,7 +32,9 @@ Napi::Object InferenceSessionWrap::Init(Napi::Env env, Napi::Object exports) {
Ort::Global<void>::api_ == nullptr, env,
"Failed to initialize ONNX Runtime API. It could happen when this nodejs binding was built with a higher version "
"ONNX Runtime but now runs with a lower version ONNX Runtime DLL(or shared library).");
auto ortEnv = new Ort::Env{ORT_LOGGING_LEVEL_WARNING, "onnxruntime-node"};
auto consoleLog = new Napi::FunctionReference(
Napi::Persistent(env.Global().Get("console").As<Napi::Object>().Get("log").As<Napi::Function>()));
auto ortEnv = new Ort::Env{ORT_LOGGING_LEVEL_WARNING, "onnxruntime-node", ConsoleLog, consoleLog};
env.SetInstanceData(ortEnv);
// initialize binding
Napi::HandleScope scope(env);
Expand Down

0 comments on commit 79e508e

Please sign in to comment.