001    package railo.runtime.type.trace;
002    
003    import railo.commons.io.res.util.ResourceUtil;
004    import railo.runtime.PageContext;
005    import railo.runtime.debug.Debugger;
006    import railo.runtime.engine.ThreadLocalPageContext;
007    import railo.runtime.type.Array;
008    import railo.runtime.type.Query;
009    import railo.runtime.type.Struct;
010    import railo.runtime.type.UDF;
011    import railo.runtime.type.dt.DateTime;
012    
013    public class TraceObjectSupport implements TraceObject {
014    
015    
016            
017            protected Object o;
018            protected  Debugger debugger;
019            
020            protected int type;
021            protected String category;
022            protected String text;
023    
024            
025            public TraceObjectSupport(Debugger debugger,Object o,int type,String category,String text) {
026                    this.o=o;
027                    //this.log=log;
028                    this.type=type;
029                    this.category=category;
030                    this.text=text;
031                    this.debugger=debugger;
032            }
033            
034            
035    
036    
037            @Override
038            
039            public String toString() {
040                    
041                    return o.toString();
042            }
043    
044            public boolean equals(Object obj) {
045                    
046                    return o.equals(obj);
047            }
048            
049    
050            protected void log() {
051                    try{log(debugger,type,category,text,null,null);}catch(Throwable t){}
052            }
053            
054            protected void log(Object varName) {
055                    try{log(debugger,type,category,text,varName.toString(),null);}catch(Throwable t){}
056            }
057            
058            protected void log(Object varName,Object varValue) {
059                    try{log(debugger,type,category,text,varName.toString(),varValue.toString());}catch(Throwable t){}
060            }
061    
062            public synchronized static void log(Debugger debugger,int type,String category,String text,String varName,String varValue) {
063                    
064                    Throwable t=new Exception("Stack trace");
065                    Throwable cause = t.getCause(); 
066                    while(cause!=null){
067                            t=cause;
068                            cause = t.getCause(); 
069                    }
070                    StackTraceElement[] traces = t.getStackTrace();
071                    
072            int line=0;
073                    String template=null;
074                    StackTraceElement trace=null;
075                    for(int i=0;i<traces.length;i++) {
076                            trace=traces[i];
077                            template=trace.getFileName();
078                            if(trace.getLineNumber()<=0 || template==null || ResourceUtil.getExtension(template,"").equals("java") ||
079                                            template.endsWith("Dump.cfc") || template.endsWith("dump.cfm"))// MUST bad impl 
080                                    continue;
081                            line=trace.getLineNumber();
082                            break;
083                    }
084                    //print.e(t);
085                    if(line==0) return;
086                    
087                    /*String type=traces[2].getMethodName();
088                    
089                    int row=qry.addRow();
090                    qry.setAtEL(LABEL, row, label);
091                    qry.setAtEL(ACTION, row, type(type));
092                    qry.setAtEL(PARAMS, row, addional);
093                    */
094                    String action=type(traces[2].getMethodName());
095                    debugger.addTrace(type, category, text, template,line,action, varName, varValue);
096                            
097            }       
098            
099    
100            protected static String type(String type) {
101                    if(type.equals("appendEL")) return "append";
102                    if(type.equals("setEL")) return "set";
103                    if(type.equals("removeEL")) return "remove";
104                    if(type.equals("keys")) return "list";
105                    if(type.equals("toDumpData")) return "dump";
106                    
107                    
108                    return type;
109            }
110            
111    
112    
113            protected PageContext pc() {
114                    return ThreadLocalPageContext.get();
115            }
116            
117            public static TraceObject toTraceObject(Debugger debugger,Object obj, int type, String category, String text) {
118                    if(obj instanceof TraceObject)
119                            return (TraceObject) obj;
120                    else if(obj instanceof UDF)
121                            return new TOUDF(debugger,(UDF) obj,type,category,text);
122                    else if(obj instanceof Query)
123                            return new TOQuery(debugger,(Query) obj,type,category,text);
124                    else if(obj instanceof Array)
125                            return new TOArray(debugger,(Array) obj,type,category,text);
126                    else if(obj instanceof Struct)
127                            return new TOStruct(debugger,(Struct) obj,type,category,text);
128                    else if(obj instanceof DateTime)
129                            return new TODateTime(debugger,(DateTime) obj,type,category,text);
130                    
131                    
132                    return new TOObjects(debugger,obj,type,category,text);
133            }
134            
135    }