GithubHelp home page GithubHelp logo

crunch / nodejs-hook-stdio Goto Github PK

View Code? Open in Web Editor NEW

This project forked from papercloud/nodejs-hook-stdio

0.0 2.0 0.0 125 KB

A handy way to hook into process.stderr and process.stdout.

Home Page: https://github.com/Papercloud/nodejs-hook-stdio

License: MIT License

JavaScript 100.00%

nodejs-hook-stdio's Introduction

#Intro

A simplified version of a great library: https://github.com/dpweb/loghooks-node. Allows you to grab the Node global process.stdout and process.stderr pipes and listen in.

Docs

The API is straightforward. Using either the stderr or the stdout function of the hook library, just pass in a handler function. The optional second argument is a true/false which controls whether or not the data is copied to the original destination or completely intercepted.

    //  Import the library.
var Hook = require('hook-stdio');

    //  Setup up a little container variable for stderr.
var errors = [];

    //  Intercept messages passed to the stderr pipe and add them to the array of errors.
Hook.stderr(errors.push, true);

process.stderr.write('Something catastrophic!');

//  Now errors == ['Something catastrophic!']

Or if you are running your javascript in the context of a node-webkit application:

    //  Import the library.
var Hook = require('hook-stdio');

    //  Intercept messages passed to the stderr and stdout pipes and display them graphically.
Hook.stdout(writeBlackTextToDOM, true);
Hook.stderr(writeRedTextToDOM, true);

function writeBlackTextToDOM (data) {
    document.getElementById('myconsole').innerHTML += '<p style="color:black;" >'+data+'</p>';
};

function writeRedTextToDOM (data) {
    document.getElementById('myconsole').innerHTML += '<p style="color:red;" >'+data+'</p>';
};

To restore the pipe to what it was, Hook.stdout and Hook.stderr return a function you can call:

    //  Import the library.
var Hook = require('hook-stdio');

var unhook = Hook.stderr(someFn, true);

	// Later, to restore the stream to what it was
unhook();

nodejs-hook-stdio's People

Contributors

callumacrae avatar jamesdavidson avatar karlgl avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.