• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create fancy log entries for errors and function call sites

callsite-record

Build Status

Create fancy log entries for errors and function call sites.

For Error:

'use strict';

const createCallsiteRecord = require('callsite-record');

function myFunc() {
    throw new Error('Yo!');
}

try {
    myFunc();
}
catch(err) {
    console.log(createCallsiteRecord({ forError: err }).renderSync());
}

⬇

example

For function call up in the stack:

'use strict';

const createCallsiteRecord = require('callsite-record');

function func2 () {
    (function func1 () {
        console.log(createCallsiteRecord({ byFunctionName: 'func2' }).renderSync());
    })();
}

func2();

⬇

example

Additional goodies:

  • Use renderers for different output formats, e.g. to produce output in HTML.
  • Use stack filter to produce clean and beautiful stacks, e.g. removing Node lib internal calls.

Install

npm install callsite-record

API

createCallsiteRecord( { forError, isCallsiteFrame, processFrameFn }) β†’ CallsiteRecord

You can generate a callsite for any stack frame, not only the topmost one. Use the isCallsiteFrame function to select a frame. This function is called for each frame starting from the top. Return true for the desired frame to generate the callsite.

Example:

const createCallsiteRecord = require('callsite-record');

try {
    throw new Error("We're doomed");
}
catch(err) {
    const record = createCallsiteRecord({ forError: err });
}

createCallsiteRecord({ byFunctionName, typeName, processFrameFn }) β†’ CallsiteRecord

Creates CallsiteRecord for the function up in the call stack specified by byFunctionName. You can optionally specify a typeName if the function is a method. If the function is a constructor set byFunctionName to constructor.

Example:

const createCallsiteRecord = require('callsite-record');

(function func1() {
    (function func2() {
        (function func3() {
            const record = createCallsiteRecord({ byFunctionName: 'func2' });
        })();
    })();
})();

You can specify processFrameFn function, which will process every frame in callstack. It's usefull when you need to enable frame processing like source-maps-support.

Example:

const createCallsiteRecord = require('callsite-record');
const wrapCallSite         = require('source-map-support').wrapCallSite;

try {
    throw new Error("We're doomed");
}
catch(err) {
    const record = createCallsiteRecord({ forError: err, processFrameFn: wrapCallSite });
}

(function func1() {
    (function func2() {
        (function func3() {
            const record = createCallsiteRecord({ byFunctionName: 'func2', processFrameFn: wrapCallSite });
        })();
    })();
})();

CallsiteRecord

CallsiteRecord.render([renderOptions]) β†’ Promise<String>

Renders call site record to the string.

Example:

record.render().then(str => console.log(str));

CallsiteRecord.renderSync([renderOptions]) β†’ String

Sync version of the CallsiteRecord.render.

renderOptions.frameSize

Specifies the number of lines rendered above and below the call site in the code frame. Default: 5.

Example:

console.log(record.renderSync({ frameSize: 0 }));
// > 12 |    func1();
// ...

console.log(record.renderSync({ frameSize: 1 }));
//   11 |(function func2() {
// > 12 |    func1();
//   13 |})();
// ...
renderOptions.codeFrame

Specifies if code frame should be rendered. If disabled only stack will be rendered. Default: true.

renderOptions.stack

Specifies if stack trace should be rendered in addition to the code frame. Default: true.

renderOptions.stackFilter

Function that will be used to filter stack frames. Function accepts 2 arguments:

  • stackFrame - stack entry.
  • idx - index of the frame.
  • isV8StackFrame - if true then stackFrame is a V8 CallSite object. Otherwise it's a StackFrame object.

Default: null.

Example:

const sep = require('path').sep;

// Remove node core lib calls from the stack trace
record.renderSync({ stackFilter: frame => frame.getFileName().indexOf(sep) > -1 });
renderOptions.renderer

Specifies the output format of the rendering. Default: renderers.default. You can pass your own renderer object (example implementations) or use one of the built-in renderers:

renderers.default

Provides ANSI-colored output as shown above.

Usage:

const defaultRenderer = require('callsite-record').renderers.default;

record.renderSync({ renderer: defaultRenderer });
renderers.noColor

Same as default renderer but without colors.

Usage:

const noColorRenderer = require('callsite-record').renderers.noColor;

record.renderSync({ renderer: noColorRenderer });
renderers.html

Outputs HTML that can be later decorated with the CSS and embeded into the web page. Example output.

Usage:

const htmlRenderer = require('callsite-record').renderers.html;

record.renderSync({ renderer: html });

Related

Author

Ivan Nikulin ([email protected])

More Repositories

1

parse5

HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.
TypeScript
3,476
star
2

publish-please

Safe and highly functional replacement for `npm publish`.
JavaScript
738
star
3

ineed

Web scraping and HTML-reprocessing. The easy way.
JavaScript
394
star
4

elegant-status

Create elegant task status for CLI.
JavaScript
131
star
5

dmn

Janitor for your node_modules
JavaScript
95
star
6

gulp-ll

Run CPU-consuming Gulp tasks in the separate processes to achieve faster builds.
JavaScript
84
star
7

esotope

ECMAScript code generator on steroids
JavaScript
38
star
8

promisify-event

Promisify EventEmitter's event.
JavaScript
35
star
9

mods

Modular JavaScript library in just 470 bytes of minified code
JavaScript
33
star
10

cookie-compat

Browsers RFC 6265 compatibility research - http://inikulin.github.io/cookie-compat.
JavaScript
30
star
11

replicator

Advanced JavaScript objects serialization.
JavaScript
25
star
12

highlight-es

Highlight ECMAScript syntax for the console or any other medium.
JavaScript
22
star
13

endpoint-utils

Utils to deal with TCP ports and hostnames. Safe for everyday use.
JavaScript
18
star
14

noop-fn

It's noop function. It does nothing.
JavaScript
13
star
15

time-limit-promise

Fulfill long runinng promises on timeout.
JavaScript
11
star
16

cp-sugar

Some sugar for child_process module.
JavaScript
8
star
17

read-file-relative

Read files with path relative to the current module without annoying boilerplate code
JavaScript
8
star
18

bin-v8-flags-filter

Filters out v8 flags for your Node.js CLIs.
JavaScript
8
star
19

pkgd

Get package publish info: package.json and file list.
JavaScript
4
star
20

is-es2016-keyword

Determine if string is an ES2016 keyword.
JavaScript
4
star
21

node-html-parser-bench

Node HTML parsers benchmark
HTML
3
star
22

if-newer

Run npm task only if any of source files are newer than destination.
JavaScript
2
star
23

os-family

Guess what? It gives you OS family. Whoa!
JavaScript
2
star
24

promise-stream-data

Create promises for the ReadableStream data.
JavaScript
2
star
25

data-url-encode-macro

Rust macro to generate base64+percent-encoded strings suitable for data URLs in compile time
Rust
2
star
26

BootML

Write less, markup more
JavaScript
1
star
27

github-user-contrib

Get GitHub user contributions info.
JavaScript
1
star
28

cargo-workspace-repro

Rust
1
star
29

notes

Just my notes on different topics for the personal use
1
star
30

testing-repo

This repo is used to test various npm-related modules
1
star