001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.type.trace;
020
021import lucee.commons.io.res.util.ResourceUtil;
022import lucee.commons.lang.ExceptionUtil;
023import lucee.runtime.PageContext;
024import lucee.runtime.debug.Debugger;
025import lucee.runtime.engine.ThreadLocalPageContext;
026import lucee.runtime.type.Array;
027import lucee.runtime.type.Query;
028import lucee.runtime.type.Struct;
029import lucee.runtime.type.UDFPlus;
030import lucee.runtime.type.dt.DateTime;
031
032public class TraceObjectSupport implements TraceObject {
033
034
035        
036        protected Object o;
037        protected  Debugger debugger;
038        
039        protected int type;
040        protected String category;
041        protected String text;
042
043        
044        public TraceObjectSupport(Debugger debugger,Object o,int type,String category,String text) {
045                this.o=o;
046                //this.log=log;
047                this.type=type;
048                this.category=category;
049                this.text=text;
050                this.debugger=debugger;
051        }
052        
053        
054
055
056        @Override
057        
058        public String toString() {
059                
060                return o.toString();
061        }
062
063        public boolean equals(Object obj) {
064                
065                return o.equals(obj);
066        }
067        
068
069        protected void log() {
070                try{log(debugger,type,category,text,null,null);}catch(Throwable t){
071                        ExceptionUtil.rethrowIfNecessary(t);
072                }
073        }
074        
075        protected void log(Object varName) {
076                try{log(debugger,type,category,text,varName.toString(),null);}catch(Throwable t){
077                        ExceptionUtil.rethrowIfNecessary(t);}
078        }
079        
080        protected void log(Object varName,Object varValue) {
081                try{log(debugger,type,category,text,varName.toString(),varValue.toString());}catch(Throwable t){
082                        ExceptionUtil.rethrowIfNecessary(t);}
083        }
084
085        public synchronized static void log(Debugger debugger,int type,String category,String text,String varName,String varValue) {
086                
087                StackTraceElement[] traces = Thread.currentThread().getStackTrace();
088                
089        int line=0;
090                String template=null;
091                StackTraceElement trace=null;
092                for(int i=0;i<traces.length;i++) {
093                        trace=traces[i];
094                        template=trace.getFileName();
095                        if(trace.getLineNumber()<=0 || template==null || ResourceUtil.getExtension(template,"").equals("java") ||
096                                        template.endsWith("Dump.cfc") || template.endsWith("dump.cfm"))// MUST bad impl 
097                                continue;
098                        line=trace.getLineNumber();
099                        break;
100                }
101                //print.e(t);
102                if(line==0) return;
103                String action=type(traces[2].getMethodName());
104                if(debugger!=null)debugger.addTrace(type, category, text, template,line,action, varName, varValue);
105                        
106        }       
107        
108
109        protected static String type(String type) {
110                if(type.equals("appendEL")) return "append";
111                if(type.equals("setEL")) return "set";
112                if(type.equals("removeEL")) return "remove";
113                if(type.equals("keys")) return "list";
114                if(type.equals("toDumpData")) return "dump";
115                
116                
117                return type;
118        }
119        
120
121
122        protected PageContext pc() {
123                return ThreadLocalPageContext.get();
124        }
125        
126        public static TraceObject toTraceObject(Debugger debugger,Object obj, int type, String category, String text) {
127                if(obj instanceof TraceObject)
128                        return (TraceObject) obj;
129                else if(obj instanceof UDFPlus)
130                        return new TOUDF(debugger,(UDFPlus) obj,type,category,text);
131                else if(obj instanceof Query)
132                        return new TOQuery(debugger,(Query) obj,type,category,text);
133                else if(obj instanceof Array)
134                        return new TOArray(debugger,(Array) obj,type,category,text);
135                else if(obj instanceof Struct)
136                        return new TOStruct(debugger,(Struct) obj,type,category,text);
137                else if(obj instanceof DateTime)
138                        return new TODateTime(debugger,(DateTime) obj,type,category,text);
139                
140                
141                return new TOObjects(debugger,obj,type,category,text);
142        }
143        
144}