Browse Source

Grok DUK_CONSOLE_TO_STDOUT DUK_CONSOLE_TO_STDERR

Add these two constants, which can be passed as part of the flags to
duk_console_init().  Their meanings are:

- DUK_CONSOLE_TO_STDOUT: all console output goes to stdout
- DUK_CONSOLE_TO_STDERR: all console output goes to stderr
- default, no flags: output for warn and above goes to stderr, output
  for info and below goes to stdout

The constants are used both for the flags parameter to
duk_console_init(), as well as for the flags saved in magic and passed
to duk__console_log_helper(); that way we avoid defining additional,
similar constants for these two different usages.
pull/1890/head
Gonzalo Diethelm 7 years ago
parent
commit
c522541fb9
  1. 42
      extras/console/duk_console.c
  2. 7
      extras/console/duk_console.h

42
extras/console/duk_console.c

@ -13,10 +13,6 @@
/* XXX: Add some form of log level filtering. */
/* XXX: For now logs everything to stdout, V8/Node.js logs debug/info level
* to stdout, warn and above to stderr. Should this extra do the same?
*/
/* XXX: Should all output be written via e.g. console.write(formattedMsg)?
* This would make it easier for user code to redirect all console output
* to a custom backend.
@ -25,12 +21,10 @@
/* XXX: Init console object using duk_def_prop() when that call is available. */
static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_name) {
duk_idx_t i, n;
duk_uint_t flags;
flags = (duk_uint_t) duk_get_current_magic(ctx);
n = duk_get_top(ctx);
duk_uint_t flags = (duk_uint_t) duk_get_current_magic(ctx);
FILE *output = (flags & DUK_CONSOLE_TO_STDOUT) ? stdout : stderr;
duk_idx_t n = duk_get_top(ctx);
duk_idx_t i;
duk_get_global_string(ctx, "console");
duk_get_prop_string(ctx, -1, "format");
@ -59,9 +53,9 @@ static duk_ret_t duk__console_log_helper(duk_context *ctx, const char *error_nam
duk_get_prop_string(ctx, -1, "stack");
}
fprintf(stdout, "%s\n", duk_to_string(ctx, -1));
fprintf(output, "%s\n", duk_to_string(ctx, -1));
if (flags & DUK_CONSOLE_FLUSH) {
fflush(stdout);
fflush(output);
}
return 0;
}
@ -110,6 +104,17 @@ static void duk__console_reg_vararg_func(duk_context *ctx, duk_c_function func,
}
void duk_console_init(duk_context *ctx, duk_uint_t flags) {
duk_uint_t flags_orig;
/* If both DUK_CONSOLE_TO_STDOUT and DUK_CONSOLE_TO_STDERR where specified,
* just turn off DUK_CONSOLE_TO_STDOUT and keep DUK_CONSOLE_TO_STDERR. */
if ((flags & DUK_CONSOLE_TO_STDOUT) &&
(flags & DUK_CONSOLE_TO_STDERR)) {
flags &= ~DUK_CONSOLE_TO_STDOUT;
}
/* Remember the (possibly corrected) flags we received. */
flags_orig = flags;
duk_push_object(ctx);
/* Custom function to format objects; user can replace.
@ -128,11 +133,24 @@ void duk_console_init(duk_context *ctx, duk_uint_t flags) {
"})(Duktape.enc)");
duk_put_prop_string(ctx, -2, "format");
flags = flags_orig;
if (!(flags & DUK_CONSOLE_TO_STDOUT) &&
!(flags & DUK_CONSOLE_TO_STDERR)) {
/* No output indicators were specified; these levels go to stdout. */
flags |= DUK_CONSOLE_TO_STDOUT;
}
duk__console_reg_vararg_func(ctx, duk__console_assert, "assert", flags);
duk__console_reg_vararg_func(ctx, duk__console_log, "log", flags);
duk__console_reg_vararg_func(ctx, duk__console_log, "debug", flags); /* alias to console.log */
duk__console_reg_vararg_func(ctx, duk__console_trace, "trace", flags);
duk__console_reg_vararg_func(ctx, duk__console_info, "info", flags);
flags = flags_orig;
if (!(flags & DUK_CONSOLE_TO_STDOUT) &&
!(flags & DUK_CONSOLE_TO_STDERR)) {
/* No output indicators were specified; these levels go to stderr. */
flags |= DUK_CONSOLE_TO_STDERR;
}
duk__console_reg_vararg_func(ctx, duk__console_warn, "warn", flags);
duk__console_reg_vararg_func(ctx, duk__console_error, "error", flags);
duk__console_reg_vararg_func(ctx, duk__console_error, "exception", flags); /* alias to console.error */

7
extras/console/duk_console.h

@ -13,6 +13,13 @@ extern "C" {
/* Flush output after every call. */
#define DUK_CONSOLE_FLUSH (1 << 1)
/* Send output to stdout. */
#define DUK_CONSOLE_TO_STDOUT (1 << 2)
/* Send output to stderr. */
#define DUK_CONSOLE_TO_STDERR (1 << 3)
/* Initialize the console system */
extern void duk_console_init(duk_context *ctx, duk_uint_t flags);
#if defined(__cplusplus)

Loading…
Cancel
Save