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.functions.system; 020 021import java.io.IOException; 022import java.util.Iterator; 023 024import lucee.commons.io.IOUtil; 025import lucee.commons.io.res.Resource; 026import lucee.commons.io.res.util.ResourceUtil; 027import lucee.commons.lang.StringUtil; 028import lucee.runtime.PageContext; 029import lucee.runtime.PageContextImpl; 030import lucee.runtime.exp.PageException; 031import lucee.runtime.op.Caster; 032import lucee.runtime.type.Array; 033import lucee.runtime.type.Struct; 034import lucee.runtime.type.util.KeyConstants; 035 036public class CallStackDump { 037 038 public static String call(PageContext pc) throws PageException { 039 return call(pc,null); 040 } 041 042 public static String call(PageContext pc, String output) throws PageException { 043 Array arr = (Array)CallStackGet.call(pc); 044 Struct sct=null; 045 String func; 046 047 // create stack 048 StringBuilder sb=new StringBuilder(); 049 Iterator<Object> it = arr.valueIterator(); 050 while(it.hasNext()){ 051 sct=(Struct) it.next(); 052 func=(String) sct.get(KeyConstants._function); 053 sb.append(sct.get(KeyConstants._template)); 054 if(func.length()>0) { 055 sb.append(':'); 056 sb.append(func); 057 } 058 sb.append(':'); 059 sb.append(Caster.toString(sct.get(CallStackGet.LINE_NUMBER))); 060 sb.append('\n'); 061 } 062 063 // output 064 try{ 065 if(StringUtil.isEmpty(output,true) || output.trim().equalsIgnoreCase("browser")) { 066 pc.forceWrite("<pre>"); 067 pc.forceWrite(sb.toString()); 068 pc.forceWrite("</pre>"); 069 } 070 else if(output.trim().equalsIgnoreCase("console")) { 071 System.out.println(sb.toString()); 072 } 073 else { 074 Resource res = ResourceUtil.toResourceNotExisting(pc, output); 075 IOUtil.write( 076 res, 077 sb.toString()+"\n", 078 ((PageContextImpl)pc).getResourceCharset().name(), true); 079 } 080 } 081 catch(IOException ioe){ 082 throw Caster.toPageException(ioe); 083 } 084 085 return null; 086 } 087}