Skip to content

Commit

Permalink
Implement console.trace()
Browse files Browse the repository at this point in the history
console.trace() is meant for logging very low-level details, so the
output is hidden by default in SSJ.  `ssj --trace` can be used to show
it.
  • Loading branch information
fatcerberus committed May 22, 2016
1 parent fb8f700 commit 1fe185b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 19 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ minisphere Changelog
v3.2.0 - TBD
------------

* The module system has been overhauled to be more Node.js-like, including
support for `require.cache`, `module.loaded`, and `module.require`.
* `require()` can now be used to load JS modules from anywhere in a game's file
system by prefixing the module ID with a SphereFS alias (`@/`, `~/`, or
`#/`).
* The module system has been overhauled to work more like Node.js, and now has
support for `package.json` files, parsing JSON files as objects, and useful
APIs such as `require.cache`, `module.loaded`, and `module.require`.
* `require()` can now load JS modules from anywhere in a game's file system by
prefixing the module ID with a SphereFS alias (`@/`, `~/`, or `#/`).
* Adds support for `console.log()` and friends. `stdout` is reserved for
under-the-hood logging, so `console` output will only be visible when SSJ is
attached.
Expand Down
7 changes: 6 additions & 1 deletion src/debugger/inferior.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct inferior
bool have_debug_info;
int line_no;
uint8_t ptr_size;
bool show_trace;
socket_t* socket;
struct source* sources;
};
Expand All @@ -52,7 +53,7 @@ inferiors_deinit(void)
}

inferior_t*
inferior_new(const char* hostname, int port)
inferior_new(const char* hostname, int port, bool show_trace)
{
inferior_t* obj;
message_t* req;
Expand Down Expand Up @@ -81,6 +82,7 @@ inferior_new(const char* hostname, int port)
printf(" author: %s\n", obj->author);

obj->id_no = s_next_id_no++;
obj->show_trace = show_trace;
return obj;

on_error:
Expand Down Expand Up @@ -494,8 +496,11 @@ handle_notify(inferior_t* obj, const message_t* msg)
: print_op == PRINT_DEBUG ? "debug"
: print_op == PRINT_ERROR ? "ERROR"
: print_op == PRINT_INFO ? "info"
: print_op == PRINT_TRACE ? "trace"
: print_op == PRINT_WARN ? "warn"
: "log";
if (print_op == PRINT_TRACE && !obj->show_trace)
break;
if (print_op == PRINT_ASSERT || print_op == PRINT_ERROR)
printf("\33[31;1m");
else if (print_op == PRINT_WARN)
Expand Down
2 changes: 1 addition & 1 deletion src/debugger/inferior.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ enum resume_op

void inferiors_init (void);
void inferiors_deinit (void);
inferior_t* inferior_new (const char* hostname, int port);
inferior_t* inferior_new (const char* hostname, int port, bool show_trace);
void inferior_free (inferior_t* obj);
bool inferior_update (inferior_t* obj);
bool inferior_attached (const inferior_t* obj);
Expand Down
9 changes: 8 additions & 1 deletion src/debugger/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct cmdline
{
path_t* path;
bool run_now;
bool show_trace;
};

static struct cmdline* parse_cmdline (int argc, char* argv[], int *out_retval);
Expand Down Expand Up @@ -35,7 +36,7 @@ main(int argc, char* argv[])

inferiors_init();

if (!(inferior = inferior_new("127.0.0.1", 1208)))
if (!(inferior = inferior_new("127.0.0.1", 1208, cmdline->show_trace)))
return EXIT_FAILURE;
printf("\n");
session = session_new(inferior);
Expand Down Expand Up @@ -176,6 +177,8 @@ parse_cmdline(int argc, char* argv[], int *out_retval)
have_target = true;
else if (strcmp(argv[i], "--run") == 0)
cmdline->run_now = true;
else if (strcmp(argv[i], "--trace") == 0)
cmdline->show_trace = true;
else {
printf("ssj: error: unknown option `%s`\n", argv[i]);
goto on_output_only;
Expand All @@ -191,6 +194,9 @@ parse_cmdline(int argc, char* argv[], int *out_retval)
case 'r':
cmdline->run_now = true;
break;
case 't':
cmdline->show_trace = true;
break;
default:
printf("ssj: error: unknown option '-%c'\n", short_args[i_arg]);
*out_retval = EXIT_FAILURE;
Expand Down Expand Up @@ -278,6 +284,7 @@ print_usage(void)
printf(" connection can be made within 30 seconds, SSJ will exit. \n");
printf(" -r, --run Prevent SSJ from pausing execution on attach. When starting\n");
printf(" a new instance, begin execution immediately. \n");
printf(" -t, --trace Show output from console.trace(). \n");
printf(" --version Print the version number of SSJ and its dependencies. \n");
printf(" --help Print this help text. \n");
}
1 change: 1 addition & 0 deletions src/debugger/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum print_op
PRINT_DEBUG,
PRINT_ERROR,
PRINT_INFO,
PRINT_TRACE,
PRINT_WARN,
};

Expand Down
39 changes: 29 additions & 10 deletions src/engine/console.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// this needs cleanup at some point. there is a ton of duplicate code here,
// it would be better to refactor it so that all the console methods piggybacked on the
// console.log() implementation.

#include "minisphere.h"
#include "console.h"

Expand All @@ -9,6 +13,7 @@ static duk_ret_t js_console_debug (duk_context* ctx);
static duk_ret_t js_console_error (duk_context* ctx);
static duk_ret_t js_console_info (duk_context* ctx);
static duk_ret_t js_console_log (duk_context* ctx);
static duk_ret_t js_console_trace (duk_context* ctx);
static duk_ret_t js_console_warn (duk_context* ctx);

static int s_verbosity = 1;
Expand Down Expand Up @@ -46,6 +51,7 @@ init_console_api(void)
api_register_function(g_duk, "console", "error", js_console_error);
api_register_function(g_duk, "console", "info", js_console_info);
api_register_function(g_duk, "console", "log", js_console_log);
api_register_function(g_duk, "console", "trace", js_console_trace);
api_register_function(g_duk, "console", "warn", js_console_warn);

// `console` is a Proxy so that unimplemented methods do not throw
Expand Down Expand Up @@ -81,8 +87,7 @@ js_console_debug(duk_context* ctx)
num_items = duk_get_top(ctx);
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_push_string(ctx, "\n");
duk_join(ctx, num_items + 1);
duk_join(ctx, num_items);

debug_print(duk_get_string(ctx, -1), PRINT_DEBUG);
return 0;
Expand All @@ -97,8 +102,7 @@ js_console_error(duk_context* ctx)
num_items = duk_get_top(ctx);
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_push_string(ctx, "\n");
duk_join(ctx, num_items + 1);
duk_join(ctx, num_items);

debug_print(duk_get_string(ctx, -1), PRINT_ERROR);
return 0;
Expand All @@ -113,8 +117,7 @@ js_console_info(duk_context* ctx)
num_items = duk_get_top(ctx);
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_push_string(ctx, "\n");
duk_join(ctx, num_items + 1);
duk_join(ctx, num_items);

debug_print(duk_get_string(ctx, -1), PRINT_INFO);
return 0;
Expand All @@ -132,13 +135,30 @@ js_console_log(duk_context* ctx)
num_items = duk_get_top(ctx);
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_push_string(ctx, "\n");
duk_join(ctx, num_items + 1);
duk_join(ctx, num_items);

debug_print(duk_get_string(ctx, -1), PRINT_NORMAL);
return 0;
}

static duk_ret_t
js_console_trace(duk_context* ctx)
{
// note: console.log() does not currently support format specifiers.
// this may change in a future implementation.

int num_items;

// join the passed-in arguments separated with spaces
num_items = duk_get_top(ctx);
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_join(ctx, num_items);

debug_print(duk_get_string(ctx, -1), PRINT_TRACE);
return 0;
}

static duk_ret_t
js_console_warn(duk_context* ctx)
{
Expand All @@ -148,8 +168,7 @@ js_console_warn(duk_context* ctx)
num_items = duk_get_top(ctx);
duk_push_string(ctx, " ");
duk_insert(ctx, 0);
duk_push_string(ctx, "\n");
duk_join(ctx, num_items + 1);
duk_join(ctx, num_items);

debug_print(duk_get_string(ctx, -1), PRINT_WARN);
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/engine/debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum print_op
PRINT_DEBUG,
PRINT_ERROR,
PRINT_INFO,
PRINT_TRACE,
PRINT_WARN,
} print_op_t;

Expand Down
3 changes: 2 additions & 1 deletion src/plugin/Debugger/DMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ enum PrintType
Debug = 0x02,
Error = 0x03,
Info = 0x04,
Warn = 0x05,
Trace = 0x05,
Warn = 0x06,
}

class DMessage
Expand Down
2 changes: 2 additions & 0 deletions src/plugin/Debugger/Inferior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ private void ProcessMessages()
{
case AppNotify.DebugPrint:
PrintType type = (PrintType)(int)message[3];
if (type == PrintType.Trace)
break;
string debugText = (string)message[4];
string prefix = type == PrintType.Assert ? "ASSERT"
: type == PrintType.Debug ? "debug"
Expand Down

0 comments on commit 1fe185b

Please sign in to comment.