-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathaddonLog.cpp
More file actions
143 lines (117 loc) · 3.96 KB
/
addonLog.cpp
File metadata and controls
143 lines (117 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <sstream>
#include "addonLog.h"
AddonThreadSafeLogCallbackFunction addonThreadSafeLoggerCallback;
bool addonJsLoggerCallbackSet = false;
int addonLoggerLogLevel = 5;
int addonLastLoggerLogLevel = 6;
static int addonGetGgmlLogLevelNumber(ggml_log_level level) {
switch (level) {
case GGML_LOG_LEVEL_ERROR: return 2;
case GGML_LOG_LEVEL_WARN: return 3;
case GGML_LOG_LEVEL_INFO: return 4;
case GGML_LOG_LEVEL_NONE: return 5;
case GGML_LOG_LEVEL_DEBUG: return 6;
case GGML_LOG_LEVEL_CONT: return addonLastLoggerLogLevel;
}
return 1;
}
void addonCallJsLogCallback(
Napi::Env env, Napi::Function callback, AddonThreadSafeLogCallbackFunctionContext* context, addon_logger_log* data
) {
bool called = false;
if (env != nullptr && callback != nullptr && addonJsLoggerCallbackSet) {
try {
callback.Call({
Napi::Number::New(env, data->logLevelNumber),
Napi::String::New(env, data->stringStream->str()),
});
called = true;
} catch (const Napi::Error& e) {
called = false;
}
}
if (!called && data != nullptr) {
if (data->logLevelNumber == 2) {
fputs(data->stringStream->str().c_str(), stderr);
fflush(stderr);
} else {
fputs(data->stringStream->str().c_str(), stdout);
fflush(stdout);
}
}
if (data != nullptr) {
delete data->stringStream;
delete data;
}
}
void addonLlamaCppLogCallback(ggml_log_level level, const char* text, void* user_data) {
int logLevelNumber = addonGetGgmlLogLevelNumber(level);
addonLastLoggerLogLevel = logLevelNumber;
if (logLevelNumber > addonLoggerLogLevel) {
return;
}
if (addonJsLoggerCallbackSet) {
std::stringstream* stringStream = new std::stringstream();
if (text != nullptr) {
*stringStream << text;
}
addon_logger_log* data = new addon_logger_log {
logLevelNumber,
stringStream,
};
auto status = addonThreadSafeLoggerCallback.NonBlockingCall(data);
if (status == napi_ok) {
return;
} else {
delete stringStream;
delete data;
}
}
if (text != nullptr) {
if (level == 2) {
fputs(text, stderr);
fflush(stderr);
} else {
fputs(text, stdout);
fflush(stdout);
}
}
}
Napi::Value setLogger(const Napi::CallbackInfo& info) {
if (addonJsLoggerCallbackSet) {
addonJsLoggerCallbackSet = false;
addonThreadSafeLoggerCallback.Release();
}
if (info.Length() < 1 || !info[0].IsFunction()) {
return info.Env().Undefined();
}
auto addonLoggerJSCallback = info[0].As<Napi::Function>();
AddonThreadSafeLogCallbackFunctionContext* context = new Napi::Reference<Napi::Value>(Napi::Persistent(info.This()));
addonThreadSafeLoggerCallback = AddonThreadSafeLogCallbackFunction::New(
info.Env(),
addonLoggerJSCallback,
"loggerCallback",
0,
1,
context,
[](Napi::Env, void*, AddonThreadSafeLogCallbackFunctionContext* ctx) {
addonJsLoggerCallbackSet = false;
delete ctx;
}
);
addonJsLoggerCallbackSet = true;
// prevent blocking the main node process from exiting due to active resources
addonThreadSafeLoggerCallback.Unref(info.Env());
return info.Env().Undefined();
}
Napi::Value setLoggerLogLevel(const Napi::CallbackInfo& info) {
if (info.Length() < 1 || !info[0].IsNumber()) {
addonLoggerLogLevel = 5;
return info.Env().Undefined();
}
addonLoggerLogLevel = info[0].As<Napi::Number>().Int32Value();
return info.Env().Undefined();
}
void addonLog(ggml_log_level level, const std::string text) {
addonLlamaCppLogCallback(level, std::string("[addon] " + text + "\n").c_str(), nullptr);
}