better way of printing the function caller's line number












1















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?










share|improve this question


















  • 1





    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
















1















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?










share|improve this question


















  • 1





    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














1












1








1


0






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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 6:31









A. LauA. Lau

3,51062262




3,51062262








  • 1





    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














  • 1





    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








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












2 Answers
2






active

oldest

votes


















1














You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.






share|improve this answer































    1














    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)))
    }





    share|improve this answer























      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1














      You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.






      share|improve this answer




























        1














        You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.






        share|improve this answer


























          1












          1








          1







          You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.






          share|improve this answer













          You could just output the call stack without raising an error, using console.trace. It is in the standard specifications for the console API.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 7:32









          trincottrincot

          129k1689123




          129k1689123

























              1














              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)))
              }





              share|improve this answer




























                1














                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)))
                }





                share|improve this answer


























                  1












                  1








                  1







                  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)))
                  }





                  share|improve this answer













                  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)))
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 19:08









                  Patrick HundPatrick Hund

                  7,75872851




                  7,75872851






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno