Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { checkLogLevel, generateConfig, generatePrefix, selectOutputStream, FakeStream } = require( './lib/utils.js' );
  2. let consoleStamp = ( con, options = {} ) => {
  3. if ( con.__patched ) {
  4. con.reset();
  5. }
  6. const isCustom = con !== console;
  7. const customConsoleStream = new FakeStream();
  8. const customConsole = new console.Console( customConsoleStream, customConsoleStream );
  9. // Fix the lack of debug alias in pre 8.0 node
  10. if ( typeof con.debug === "undefined" ) {
  11. con.debug = ( ...arg ) => con.org.log ? con.org.log( ...arg ) : con.log( ...arg );
  12. }
  13. const config = generateConfig( options );
  14. const include = config.include.filter( m => typeof con[m] === 'function' );
  15. const helperConsole = new console.Console( config.stdout, config.stderr );
  16. const org = {};
  17. Object.keys( con ).forEach( m => org[m] = con[m] );
  18. con.org = org;
  19. include.forEach( method => {
  20. const stream = selectOutputStream( method, config );
  21. const trg = con[method];
  22. con[method] = new Proxy( trg, {
  23. apply: ( target, context, arguments ) => {
  24. if ( checkLogLevel( config, method ) ) {
  25. customConsole.log.apply( context, arguments );
  26. stream.write( `${generatePrefix( method, config, customConsoleStream.last_msg )} ` );
  27. if ( config.preventDefaultMessage || /:msg\b/.test( config.format ) ) {
  28. stream.write('\n');
  29. }else if(method === 'table'){
  30. stream.write('\n');
  31. // Normaly table calls log to write to stream, so we need to prevent double prefix
  32. helperConsole.table.apply( context, arguments);
  33. }else if( !isCustom && options.stdout){
  34. stream.write(`${customConsoleStream.last_msg}\n`);
  35. } else {
  36. target.apply( context, arguments );
  37. }
  38. }
  39. }
  40. } );
  41. con.__patched = true
  42. } );
  43. if(!include.includes('table')) {
  44. // Normaly table calls log to write to stream, we need to prevent prefix when table is not included
  45. con.table = helperConsole.table;
  46. }
  47. con.reset = () => {
  48. Object.keys( con.org ).forEach( m => {
  49. con[m] = con.org[m];
  50. delete con.org[m];
  51. } );
  52. delete con.org;
  53. delete con.__patched;
  54. delete con.reset;
  55. customConsoleStream.end();
  56. };
  57. };
  58. module.exports = consoleStamp;
  59. module.exports.default = consoleStamp;