Skip to content

Commit

Permalink
Add logs for node port in debug mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Dec 3, 2019
1 parent aea5252 commit c4e64a0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
11 changes: 11 additions & 0 deletions source/ports/node_port/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ Module.prototype.require = function (id) {
}
};

/* Debug logs */
if (process.env['NODE_ENV'] === 'debug')
{
addon.metacall_logs();
}

/* Export the API */
module.exports = {
metacall: (name, ...args) => {
Expand Down Expand Up @@ -116,4 +122,9 @@ module.exports = {
return json;
}
},

/* TODO: Remove this from user or provide better ways of configuring logs */
metacall_logs: () => {
addon.metacall_logs();
},
};
21 changes: 20 additions & 1 deletion source/ports/node_port/source/node_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ napi_value metacall_node(napi_env env, napi_callback_info info)
metacall_node_value_to_napi(env, ptr, &js_object);
return js_object;
}

// this function is the handler of the "metacall_load_from_file"
napi_value metacall_node_load_from_file(napi_env env, napi_callback_info info)
{
Expand Down Expand Up @@ -384,21 +385,39 @@ napi_value metacall_node_inspect(napi_env env, napi_callback_info)
return result;
}

/* TODO: Add documentation */
napi_value metacall_node_logs(napi_env env, napi_callback_info)
{
struct metacall_log_stdio_type log_stdio = { stdout };

if (metacall_log(METACALL_LOG_STDIO, (void *)&log_stdio) != 0)
{
napi_throw_error(env, NULL, "MetaCall failed to initialize debug logs");
}

return NULL;
}

/* TODO: Review documentation */
// This functions sets the necessary js functions that could be called in NodeJs
void metacall_node_exports(napi_env env, napi_value exports)
{
const char function_metacall_str[] = "metacall";
const char function_load_from_file_str[] = "metacall_load_from_file";
const char function_inspect_str[] = "metacall_inspect";
napi_value function_metacall, function_load_from_file, function_inspect;
const char function_logs_str[] = "metacall_logs";

napi_value function_metacall, function_load_from_file, function_inspect, function_logs;

napi_create_function(env, function_metacall_str, sizeof(function_metacall_str) - 1, metacall_node, NULL, &function_metacall);
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);
napi_create_function(env, function_inspect_str, sizeof(function_inspect_str) - 1, metacall_node_inspect, NULL, &function_inspect);
napi_create_function(env, function_logs_str, sizeof(function_logs_str) - 1, metacall_node_logs, NULL, &function_logs);

napi_set_named_property(env, exports, function_metacall_str, function_metacall);
napi_set_named_property(env, exports, function_load_from_file_str, function_load_from_file);
napi_set_named_property(env, exports, function_inspect_str, function_inspect);
napi_set_named_property(env, exports, function_logs_str, function_logs);
}

/* TODO: Review documentation */
Expand Down
9 changes: 8 additions & 1 deletion source/ports/node_port/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

const assert = require('assert');

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

describe('metacall', () => {
describe('require', () => {
it('functions metacall and metacall_load_from_file must be defined', () => {
assert.notStrictEqual(metacall, undefined);
assert.notStrictEqual(metacall_load_from_file, undefined);
assert.notStrictEqual(metacall_inspect, undefined);
assert.notStrictEqual(metacall_logs, undefined);
});
});

describe('logs', () => {
it('metacall_logs', () => {
assert.strictEqual(metacall_logs(), undefined);
});
});

Expand Down

0 comments on commit c4e64a0

Please sign in to comment.