• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Getting a calleer list from JScript

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);
            objLogFile.Close();
      }  // 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.
0
michael_madsen
Asked:
michael_madsen
1 Solution
 
wranlonCommented:
Here is an example using bits from my Function Monitor tool (http://www.imnmotion.com/documents/html/technical/dhtml/funmon.html)  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");
            DoTest2("xx",{});
      }
      
      function DoTest2(s,o){
            Log("Test 2");
            DoTest3(s,o,3);
      }
      
      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());
                        if(sName==null){
                              vFP=null;
                              break;
                        }
                        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 += ")";
                        aRoute.push(sName);
                        vFP=vFP.caller;
                  }
                  retVal = aRoute.reverse().join("->");
            }
            else{
                  retVal="null";
            }
            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){
                  sFP=aM[0];
                  sFP=sFP.replace(/^function\s+/,"");
                  sFP=sFP.replace(/^\s*/,"");
                  sFP=sFP.replace(/\s*$/,"");
                  sFP=sFP.replace(/\($/,"");
                  return sFP;
            }
            else{
                  return null;
            }
      }
 %>
 
 
 <%
 Log("Example #1");
 DoTest1();
 
 %>
 <% for(var i = 0; i < aMsg.length;i++){ %>
 <p><%=aMsg[i] %></p>
 <%} %>
0
 
michael_madsenAuthor Commented:
wranlon: Very nice (and quick) solution. I have removed the attributes list (as I don't need them).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now