Last active
February 11, 2022 19:07
-
-
Save steenhansen/2b23a0e14016bb755d08df8582de8952 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myConsole = (function(){ | |
let the_log_buffer=[[]], the_count=0, the_single_line=false; | |
const THE_CONSOLE=console, LINE_DIVIDER=' ~ ', ONE_LINE='ONE_LINE', | |
PARAMETER_SEPARATOR= ', ', NEW_LINE = Symbol(); | |
const start = (line_type='NOT_ONE_LINE') => { | |
the_log_buffer=[[]]; | |
the_count=0; | |
the_single_line = line_type == ONE_LINE; | |
console = myConsole; | |
} | |
const stop = () => { | |
isNewline(); | |
console = THE_CONSOLE; | |
}; | |
const isNewline = a_param => { | |
if (the_single_line && a_param==NEW_LINE) return; | |
const buffer_parts = the_log_buffer.map(one_set=> one_set.join(PARAMETER_SEPARATOR)) | |
const buffer_line = buffer_parts.join(LINE_DIVIDER); | |
if (the_single_line) { | |
THE_CONSOLE.clear(); | |
} | |
THE_CONSOLE.log( buffer_line ); | |
the_log_buffer = [[]]; | |
the_count=0; | |
} | |
const anObject = an_object => { | |
if (an_object instanceof Error){ | |
const error_props = [...Object.getOwnPropertyNames(an_object)]; | |
error_props.map( error_key => an_object['_' + error_key] = an_object[error_key] ); | |
} | |
the_log_buffer[the_count].push(JSON.stringify(an_object)); | |
} | |
const aScalar = a_scalar => { | |
if (typeof a_scalar === 'string' && !isNaN(a_scalar)) { | |
the_log_buffer[the_count].push("'" + a_scalar + "'"); | |
} else { | |
the_log_buffer[the_count].push(a_scalar); | |
} | |
} | |
const notNewline = a_param => typeof a_param === 'object' ? anObject(a_param) : aScalar(a_param); | |
const checkNewline = a_param => a_param == NEW_LINE ? isNewline(a_param) : notNewline(a_param); | |
const log = (...parameters_list) => { | |
the_log_buffer[the_count]=[]; | |
parameters_list.map( checkNewline ); | |
if (the_single_line){ | |
isNewline(undefined); | |
}else{ | |
const last_log = parameters_list.pop(); | |
if (last_log !== NEW_LINE){ | |
the_count++; | |
} | |
} | |
} | |
return Object.assign({}, console, {start, stop, log, ONE_LINE, NEW_LINE}); | |
})(); | |
function showConcatLog(){ | |
myConsole.stop(); | |
myConsole.start(); | |
console.log('a'); | |
console.log('bb'); | |
console.dir({i:'not', j:'affected', k:'but not in step'}) | |
console.log('ccc'); | |
console.log([1,2,3,4,5,'6'], {x:8, y:'9'}); | |
console.log("dddd", 1, '2', 3, myConsole.NEW_LINE); | |
console.log("z", myConsole.NEW_LINE, 8, '7'); | |
console.log(new Error("error test")); | |
myConsole.stop(); | |
} | |
myConsole.start(myConsole.ONE_LINE); | |
var stop_callback = 5; | |
function myCallback(){ | |
console.log(stop_callback, 'Date.now()', myConsole.NEW_LINE, Date.now()); | |
stop_callback--; | |
if (stop_callback>0){ | |
window.setTimeout(myCallback, 1000); | |
}else{ | |
showConcatLog(); | |
} | |
} | |
window.setTimeout(myCallback, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment