Skip to content

Commit c4e64a0

Browse files
committed
Add logs for node port in debug mode.
1 parent aea5252 commit c4e64a0

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

source/ports/node_port/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ Module.prototype.require = function (id) {
8383
}
8484
};
8585

86+
/* Debug logs */
87+
if (process.env['NODE_ENV'] === 'debug')
88+
{
89+
addon.metacall_logs();
90+
}
91+
8692
/* Export the API */
8793
module.exports = {
8894
metacall: (name, ...args) => {
@@ -116,4 +122,9 @@ module.exports = {
116122
return json;
117123
}
118124
},
125+
126+
/* TODO: Remove this from user or provide better ways of configuring logs */
127+
metacall_logs: () => {
128+
addon.metacall_logs();
129+
},
119130
};

source/ports/node_port/source/node_port.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ napi_value metacall_node(napi_env env, napi_callback_info info)
319319
metacall_node_value_to_napi(env, ptr, &js_object);
320320
return js_object;
321321
}
322+
322323
// this function is the handler of the "metacall_load_from_file"
323324
napi_value metacall_node_load_from_file(napi_env env, napi_callback_info info)
324325
{
@@ -384,21 +385,39 @@ napi_value metacall_node_inspect(napi_env env, napi_callback_info)
384385
return result;
385386
}
386387

388+
/* TODO: Add documentation */
389+
napi_value metacall_node_logs(napi_env env, napi_callback_info)
390+
{
391+
struct metacall_log_stdio_type log_stdio = { stdout };
392+
393+
if (metacall_log(METACALL_LOG_STDIO, (void *)&log_stdio) != 0)
394+
{
395+
napi_throw_error(env, NULL, "MetaCall failed to initialize debug logs");
396+
}
397+
398+
return NULL;
399+
}
400+
387401
/* TODO: Review documentation */
388402
// This functions sets the necessary js functions that could be called in NodeJs
389403
void metacall_node_exports(napi_env env, napi_value exports)
390404
{
391405
const char function_metacall_str[] = "metacall";
392406
const char function_load_from_file_str[] = "metacall_load_from_file";
393407
const char function_inspect_str[] = "metacall_inspect";
394-
napi_value function_metacall, function_load_from_file, function_inspect;
408+
const char function_logs_str[] = "metacall_logs";
409+
410+
napi_value function_metacall, function_load_from_file, function_inspect, function_logs;
395411

396412
napi_create_function(env, function_metacall_str, sizeof(function_metacall_str) - 1, metacall_node, NULL, &function_metacall);
397413
napi_create_function(env, function_load_from_file_str, sizeof(function_load_from_file_str) - 1, metacall_node_load_from_file, NULL, &function_load_from_file);
398414
napi_create_function(env, function_inspect_str, sizeof(function_inspect_str) - 1, metacall_node_inspect, NULL, &function_inspect);
415+
napi_create_function(env, function_logs_str, sizeof(function_logs_str) - 1, metacall_node_logs, NULL, &function_logs);
416+
399417
napi_set_named_property(env, exports, function_metacall_str, function_metacall);
400418
napi_set_named_property(env, exports, function_load_from_file_str, function_load_from_file);
401419
napi_set_named_property(env, exports, function_inspect_str, function_inspect);
420+
napi_set_named_property(env, exports, function_logs_str, function_logs);
402421
}
403422

404423
/* TODO: Review documentation */

source/ports/node_port/test/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
const assert = require('assert');
44

5-
const { metacall, metacall_load_from_file, metacall_inspect } = require('../index.js');
5+
const { metacall, metacall_load_from_file, metacall_inspect, metacall_logs } = require('../index.js');
66

77
describe('metacall', () => {
88
describe('require', () => {
99
it('functions metacall and metacall_load_from_file must be defined', () => {
1010
assert.notStrictEqual(metacall, undefined);
1111
assert.notStrictEqual(metacall_load_from_file, undefined);
1212
assert.notStrictEqual(metacall_inspect, undefined);
13+
assert.notStrictEqual(metacall_logs, undefined);
14+
});
15+
});
16+
17+
describe('logs', () => {
18+
it('metacall_logs', () => {
19+
assert.strictEqual(metacall_logs(), undefined);
1320
});
1421
});
1522

0 commit comments

Comments
 (0)