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.commons.lang;
020
021import static lucee.commons.io.SystemUtil.ERR;
022import static lucee.commons.io.SystemUtil.OUT;
023
024import java.io.PrintWriter;
025import java.util.Date;
026
027import lucee.commons.io.SystemUtil;
028import lucee.runtime.PageContext;
029import lucee.runtime.config.Config;
030import lucee.runtime.engine.ThreadLocalPageContext;
031
032public final class SystemOut {
033
034    /**
035     * logs a value 
036     * @param value
037     */
038    public static void printDate(PrintWriter pw,String value) {
039        long millis=System.currentTimeMillis();
040        pw.write(
041                        new Date(millis)
042                        +"-"
043                        +(millis-(millis/1000*1000))
044                        +" "+value+"\n");
045        pw.flush();
046    }
047    /**
048     * logs a value 
049     * @param value
050     */
051    public static void print(PrintWriter pw,String value) {
052        pw.write(value+"\n");
053        pw.flush();
054    } 
055
056
057        public static void printStack(PrintWriter pw) {
058                new Throwable().printStackTrace(pw);
059        }
060
061        public static void printStack(int type) {
062                Config config=ThreadLocalPageContext.getConfig();
063        if(config!=null) {
064                if(type==ERR)
065                        printStack(config.getErrWriter());
066                else 
067                        printStack(config.getOutWriter());
068        }
069        else {
070                printStack(new PrintWriter((type==ERR)?System.err:System.out));
071        }
072        }
073    
074    /**
075     * logs a value 
076     * @param value
077     */
078    public static void printDate(String value) {
079        printDate(value,OUT);
080    }
081    
082    public static void printDate(String value,int type) {
083        printDate(getPrinWriter(type),value);
084    }
085    
086
087    public static PrintWriter getPrinWriter(int type) {
088        Config config=ThreadLocalPageContext.getConfig();
089        if(config!=null) {
090                if(type==ERR) return config.getErrWriter();
091                return config.getOutWriter();
092        }
093        return SystemUtil.getPrintWriter(type);
094    }
095    
096    
097    
098    /**
099     * logs a value 
100     * @param value
101     */
102    
103    public static void print(String value) {
104        print(value, OUT);
105    }
106    
107    public static void print(String value,int type) {
108        PageContext pc=ThreadLocalPageContext.get();
109        if(pc!=null) {
110                if(type==ERR)
111                        print(pc.getConfig().getErrWriter(),value);
112                else 
113                        print(pc.getConfig().getOutWriter(),value);
114        }
115        else {
116                print(new PrintWriter((type==ERR)?System.err:System.out),value);
117        }
118    }
119
120}