@yuneta/gobj-js 7.13.5 → 7.13.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -589,6 +589,10 @@ set_log_callback(fn) // fn(level, message)
589
589
 
590
590
  // Gate the DIRECT browser-console writes of all the functions above
591
591
  set_console_log_enabled(enabled) // default: true (unchanged behaviour)
592
+
593
+ // ...and a per-LINE say over them, on top of that switch (7.13.6)
594
+ set_console_log_filter(fn) // fn(level, msg) -> true to print
595
+ set_console_log_filter(null) // default: everything prints
592
596
  ```
593
597
 
594
598
  **Three independent output paths.** `set_console_log_enabled(false)` silences
@@ -598,6 +602,15 @@ a GUI route framework output — **including the automata/FSM trace, which arriv
598
602
  as `debug`** — into its own monitor window while keeping the browser console
599
603
  clean (gobj-ui's dev-window "Output" selector does exactly this).
600
604
 
605
+ **Why a filter and not just the switch.** The console write happens **before**
606
+ the sink is called, so nothing downstream can un-print a line: a consumer that
607
+ wants one CLASS of lines kept off the console — the dev monitor hiding the
608
+ `machine` trace's timer traffic, two `EV_TIMEOUT` lines a second for ever — can
609
+ only say so here. The filter decides the **console alone**: the sink still
610
+ receives every line, so a monitor keeps a complete record and decides
611
+ separately what to show. The master switch still wins, and a filter that
612
+ **throws** is ignored — a broken one must never be able to silence the log.
613
+
601
614
  ### Tracing: the `machine` trace, and the levels around it
602
615
 
603
616
  The FSM is where things happen, so the trace of the FSM **is** the execution
@@ -3016,6 +3016,19 @@ var __console_output_enabled__ = true;
3016
3016
  function set_console_log_enabled(enabled) {
3017
3017
  __console_output_enabled__ = enabled ? true : false;
3018
3018
  }
3019
+ var __console_log_filter__ = null;
3020
+ function set_console_log_filter(fn) {
3021
+ __console_log_filter__ = typeof fn === "function" ? fn : null;
3022
+ }
3023
+ function console_wants(level, msg) {
3024
+ if (!__console_output_enabled__) return false;
3025
+ if (!__console_log_filter__) return true;
3026
+ try {
3027
+ return __console_log_filter__(level, msg) ? true : false;
3028
+ } catch (e) {
3029
+ return true;
3030
+ }
3031
+ }
3019
3032
  var __in_log_callback__ = false;
3020
3033
  function emit_log_callback(level, msg, hora) {
3021
3034
  if (__log_callback__ && !__in_log_callback__) {
@@ -3063,9 +3076,9 @@ function log_error(format) {
3063
3076
  let hora = current_timestamp();
3064
3077
  if (f_error) {
3065
3078
  if (f_error === _console.error) {
3066
- if (__console_output_enabled__) f_error("%c" + hora + " ERROR: " + String(msg), "color:red");
3079
+ if (console_wants("error", msg)) f_error("%c" + hora + " ERROR: " + String(msg), "color:red");
3067
3080
  } else {
3068
- if (__console_output_enabled__) _console.error("%c" + hora + " ERROR: " + String(msg), "color:red");
3081
+ if (console_wants("error", msg)) _console.error("%c" + hora + " ERROR: " + String(msg), "color:red");
3069
3082
  f_error(`${hora} ERROR: ${String(msg)}`);
3070
3083
  }
3071
3084
  }
@@ -3076,9 +3089,9 @@ function log_warning(format) {
3076
3089
  let hora = current_timestamp();
3077
3090
  if (f_warning) {
3078
3091
  if (f_warning === _console.warn) {
3079
- if (__console_output_enabled__) f_warning("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3092
+ if (console_wants("warning", msg)) f_warning("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3080
3093
  } else {
3081
- if (__console_output_enabled__) _console.warn("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3094
+ if (console_wants("warning", msg)) _console.warn("%c" + hora + " WARNING: " + String(msg), "color:yellow");
3082
3095
  f_warning(`${hora} WARNING: ${String(msg)}`);
3083
3096
  }
3084
3097
  }
@@ -3088,7 +3101,7 @@ function log_info(format) {
3088
3101
  let msg = format_log(format);
3089
3102
  let hora = current_timestamp();
3090
3103
  if (f_info) {
3091
- if (__console_output_enabled__) f_info("%c" + hora + " INFO: " + String(msg), "color:cyan");
3104
+ if (console_wants("info", msg)) f_info("%c" + hora + " INFO: " + String(msg), "color:cyan");
3092
3105
  }
3093
3106
  emit_log_callback("info", msg, hora);
3094
3107
  }
@@ -3096,7 +3109,7 @@ function log_debug(format) {
3096
3109
  let msg = format_log(format);
3097
3110
  let hora = current_timestamp();
3098
3111
  if (f_debug) {
3099
- if (__console_output_enabled__) f_debug("%c" + hora + " DEBUG: " + String(msg), "color:silver");
3112
+ if (console_wants("debug", msg)) f_debug("%c" + hora + " DEBUG: " + String(msg), "color:silver");
3100
3113
  }
3101
3114
  emit_log_callback("debug", msg, hora);
3102
3115
  }
@@ -3104,12 +3117,12 @@ function trace_msg(format) {
3104
3117
  let msg = format_log(format);
3105
3118
  let hora = current_timestamp();
3106
3119
  if (f_debug) {
3107
- if (__console_output_enabled__) f_debug("%c" + hora + " MSG: " + String(msg), "color:cyan");
3120
+ if (console_wants("msg", msg)) f_debug("%c" + hora + " MSG: " + String(msg), "color:cyan");
3108
3121
  }
3109
3122
  emit_log_callback("msg", msg, hora);
3110
3123
  }
3111
3124
  function trace_json(jn, msg) {
3112
- if (__console_output_enabled__) {
3125
+ if (console_wants("json", msg)) {
3113
3126
  if (msg) window.console.warn("=====> " + msg);
3114
3127
  window.console.dir(jn);
3115
3128
  }
@@ -6861,6 +6874,7 @@ exports.safeSrc = safeSrc;
6861
6874
  exports.sdata_flag_t = sdata_flag_t;
6862
6875
  exports.send_http_json_post = send_http_json_post;
6863
6876
  exports.set_console_log_enabled = set_console_log_enabled;
6877
+ exports.set_console_log_filter = set_console_log_filter;
6864
6878
  exports.set_log_callback = set_log_callback;
6865
6879
  exports.set_remote_log_functions = set_remote_log_functions;
6866
6880
  exports.set_timeout = set_timeout;