better way of printing the function caller's line number
So I have a function defined as follows:
printPrettyJson =
function (json)
{
// logger.log(chalk.magenta(JSON.stringify(json, null, 4)))
try
{
throw Error("printPrettyJson")
}
catch (error)
{
console.log(error.stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
}
What I do with this is to simply print out a JSON kind of object (since it'll otherwise show [Object object]. However, I was wondering if I do this without throwing a clumsy Error. I'm calling it from another file e.g.
File1
const printPrettyJson = require("file2)
Line 101: printPrettyJson(json)
File2
// printPrettyJson's definition in here
So how would I output line 101 from File1 on printPrettyJson's function call?
javascript node.js
add a comment |
So I have a function defined as follows:
printPrettyJson =
function (json)
{
// logger.log(chalk.magenta(JSON.stringify(json, null, 4)))
try
{
throw Error("printPrettyJson")
}
catch (error)
{
console.log(error.stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
}
What I do with this is to simply print out a JSON kind of object (since it'll otherwise show [Object object]. However, I was wondering if I do this without throwing a clumsy Error. I'm calling it from another file e.g.
File1
const printPrettyJson = require("file2)
Line 101: printPrettyJson(json)
File2
// printPrettyJson's definition in here
So how would I output line 101 from File1 on printPrettyJson's function call?
javascript node.js
1
Wouldconsole.tracehelp out?
– trincot
Nov 26 '18 at 19:15
@trincot that might actually be good enough. I did that before and it was outputting red text with[object Object]and I thought it was throwing an error. But I guess it's actually just printing the stack.
– A. Lau
Nov 27 '18 at 4:05
add a comment |
So I have a function defined as follows:
printPrettyJson =
function (json)
{
// logger.log(chalk.magenta(JSON.stringify(json, null, 4)))
try
{
throw Error("printPrettyJson")
}
catch (error)
{
console.log(error.stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
}
What I do with this is to simply print out a JSON kind of object (since it'll otherwise show [Object object]. However, I was wondering if I do this without throwing a clumsy Error. I'm calling it from another file e.g.
File1
const printPrettyJson = require("file2)
Line 101: printPrettyJson(json)
File2
// printPrettyJson's definition in here
So how would I output line 101 from File1 on printPrettyJson's function call?
javascript node.js
So I have a function defined as follows:
printPrettyJson =
function (json)
{
// logger.log(chalk.magenta(JSON.stringify(json, null, 4)))
try
{
throw Error("printPrettyJson")
}
catch (error)
{
console.log(error.stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
}
What I do with this is to simply print out a JSON kind of object (since it'll otherwise show [Object object]. However, I was wondering if I do this without throwing a clumsy Error. I'm calling it from another file e.g.
File1
const printPrettyJson = require("file2)
Line 101: printPrettyJson(json)
File2
// printPrettyJson's definition in here
So how would I output line 101 from File1 on printPrettyJson's function call?
javascript node.js
javascript node.js
asked Nov 26 '18 at 6:31
A. LauA. Lau
3,51062262
3,51062262
1
Wouldconsole.tracehelp out?
– trincot
Nov 26 '18 at 19:15
@trincot that might actually be good enough. I did that before and it was outputting red text with[object Object]and I thought it was throwing an error. But I guess it's actually just printing the stack.
– A. Lau
Nov 27 '18 at 4:05
add a comment |
1
Wouldconsole.tracehelp out?
– trincot
Nov 26 '18 at 19:15
@trincot that might actually be good enough. I did that before and it was outputting red text with[object Object]and I thought it was throwing an error. But I guess it's actually just printing the stack.
– A. Lau
Nov 27 '18 at 4:05
1
1
Would
console.trace help out?– trincot
Nov 26 '18 at 19:15
Would
console.trace help out?– trincot
Nov 26 '18 at 19:15
@trincot that might actually be good enough. I did that before and it was outputting red text with
[object Object] and I thought it was throwing an error. But I guess it's actually just printing the stack.– A. Lau
Nov 27 '18 at 4:05
@trincot that might actually be good enough. I did that before and it was outputting red text with
[object Object] and I thought it was throwing an error. But I guess it's actually just printing the stack.– A. Lau
Nov 27 '18 at 4:05
add a comment |
2 Answers
2
active
oldest
votes
You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.
add a comment |
You can get the line number from the stack trace by creating a new error object, it is not necessary to actually throw an error:
printPrettyJson =
function (json)
{
console.log(new Error().stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475791%2fbetter-way-of-printing-the-function-callers-line-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.
add a comment |
You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.
add a comment |
You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.
You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.
answered Nov 27 '18 at 7:32
trincottrincot
129k1689123
129k1689123
add a comment |
add a comment |
You can get the line number from the stack trace by creating a new error object, it is not necessary to actually throw an error:
printPrettyJson =
function (json)
{
console.log(new Error().stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
add a comment |
You can get the line number from the stack trace by creating a new error object, it is not necessary to actually throw an error:
printPrettyJson =
function (json)
{
console.log(new Error().stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
add a comment |
You can get the line number from the stack trace by creating a new error object, it is not necessary to actually throw an error:
printPrettyJson =
function (json)
{
console.log(new Error().stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
You can get the line number from the stack trace by creating a new error object, it is not necessary to actually throw an error:
printPrettyJson =
function (json)
{
console.log(new Error().stack)
console.debug(chalk.magenta(JSON.stringify(json, null, 4)))
}
answered Nov 26 '18 at 19:08
Patrick HundPatrick Hund
7,75872851
7,75872851
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53475791%2fbetter-way-of-printing-the-function-callers-line-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Would
console.tracehelp out?– trincot
Nov 26 '18 at 19:15
@trincot that might actually be good enough. I did that before and it was outputting red text with
[object Object]and I thought it was throwing an error. But I guess it's actually just printing the stack.– A. Lau
Nov 27 '18 at 4:05