Getting a calleer list from JScript

Posted on 2006-06-07
Last Modified: 2012-06-21
I am writing an ASP project (with JScript - NOT VBScript)
I am about to write a simple file-log for the project.
So far it just enables me to write something to a text-file, by inserting one line into the code (where you wan't something logged):

      WriteToLog ("Something has happended");

Now the WriteToLog function itself is very simple like this:

      function WriteToLog(strText) {
            var strDate = GetDateTimeString();
            var strSessionId = Session.SessionID;

            var ForAppending = 8;
            var objFso = Server.CreateObject("Scripting.FileSystemObject");
            var strLogfilename = GetLogDirectory() + "\\" + GetLogFilename();
            var objLogFile = objFso.OpenTextFile(strLogfilename, ForAppending, true);
            objLogFile.WriteLine(strDate + "   " + "[" + strSessionId + "]" + "   " + strText);
      }  // WriteToLog()

This results in something like this in the log-file:

      2006-06-07 11:38:27,670   [858642225]   Something has happended

What I would like it to add to this, is the entire path of function calls. Let me explain it better with an example:

Let's say that the webpage we are retrieving is called showuser.asp?uid=1234
This page calls a function (in an included page) called GetUserById()
The GetUserById function calls another function called GetAllUsers()
The GetAllUsers function calls another function called GetUserRecordSet()
The GetUserRecordSet function is the function that issues the WriteToLog call.
Now I would like the WriteToLog to trace the call backwards to show all the callers, like this:

      2006-06-07 11:38:27,670   [858642225]   GetUserById()->GetAllUsers()->GetUserRecordSet()   Something has happended

I imagine that this is somehow possible through the Function object's caller property, but I can't figure out how to do it.
Question by:michael_madsen
    LVL 5

    Accepted Solution

    Here is an example using bits from my Function Monitor tool (  I added in the argument list for the callers, but that will make some arguments such as functions look messy.

          var aMsg = [];
          function DoTest1(){
                Log("Test 1");
          function DoTest2(s,o){
                Log("Test 2");
          function DoTest3(x,o,y){
                Log("Test 3");
          function Log(sMsg){
                route = traceRoute(Log.caller);
                if(route == null || route == "null") route = "Global";
                aMsg[aMsg.length] = (new Date()).toString() + " - " + route + " -- " + sMsg;

          function traceRoute(vFP){
                var retVal="",aRoute=[],iCounter=0, sName;
                if(vFP != null){
                      while(vFP && vFP != null ){
                            sName = getFunctionName(vFP.toString());
                            sName += "(";
                            for(var i = 0; i < vFP.arguments.length; i++){
                                  if(i > 0) sName += ", ";
                                  var sQ = "";
                                  var vArg = vFP.arguments[i];
                                  var sType = typeof vArg;
                                  if(sType == "string") sQ = "\"";
                                  sName += sQ + (sType == "object" ? "obj" : vArg) + sQ + " {as " + sType + "}";
                            sName += ")";
                      retVal = aRoute.reverse().join("->");
                return retVal;
          function getFunctionName(sFP){
                var aM=sFP.match(/function\s([A-Za-z0-9_]*)\(/gi);      
                if(sFP==null) return sFP;
                if(aM!=null && aM.length){
                      return sFP;
                      return null;
     Log("Example #1");
     <% for(var i = 0; i < aMsg.length;i++){ %>
     <p><%=aMsg[i] %></p>
     <%} %>
    LVL 2

    Author Comment

    wranlon: Very nice (and quick) solution. I have removed the attributes list (as I don't need them).

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
    Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
    This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now