1 30 package com.genimen.djeneric.util; 31 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.io.PrintStream ; 36 import java.io.PrintWriter ; 37 import java.text.SimpleDateFormat ; 38 import java.util.Date ; 39 40 public class DjLogPrintStream extends PrintStream 41 { 42 FileOutputStream _fos; 43 String _banner; 44 45 public static void logStdOut(String banner, String fileName, boolean autoFlush, boolean writeHeader) throws Exception 46 { 47 DjLogPrintStream newStdOut = new DjLogPrintStream(banner, fileName, System.out, autoFlush, writeHeader); 48 System.setOut(newStdOut); 49 } 50 51 public static void logStdErr(String banner, String fileName, boolean autoFlush, boolean writeHeader) throws Exception 52 { 53 DjLogPrintStream newStdErr = new DjLogPrintStream(banner, fileName, System.err, autoFlush, writeHeader); 54 System.setErr(newStdErr); 55 } 56 57 public static void logAll(String banner, String fileName, boolean autoFlush, boolean writeHeader) throws Exception 58 { 59 DjLogPrintStream newStdOut = new DjLogPrintStream(banner, fileName, System.out, autoFlush, writeHeader); 60 FileOutputStream fos = newStdOut.getFileOutputStream(); 61 DjLogPrintStream newStdErr = new DjLogPrintStream(banner, fos, System.err, autoFlush, false); 62 63 System.setOut(newStdOut); 64 System.setErr(newStdErr); 65 } 66 67 public static void logAll(String banner, String fileName) throws Exception 68 { 69 logAll(banner, fileName, true, true); 70 } 71 72 private DjLogPrintStream(String banner, String logFileName, OutputStream out, boolean autoFlush, boolean writeHeader) 73 throws Exception 74 { 75 super(out, autoFlush); 76 _banner = banner; 77 78 _fos = new FileOutputStream (logFileName, true); 79 if (writeHeader) writeHeader(); 80 } 81 82 private DjLogPrintStream(String banner, FileOutputStream fos, OutputStream out, boolean autoFlush, boolean writeHeader) 83 throws Exception 84 { 85 super(out, autoFlush); 86 87 _banner = banner; 88 _fos = fos; 89 if (writeHeader) writeHeader(); 90 } 91 92 private String getDivLine(int len) 93 { 94 StringBuffer sb = new StringBuffer (); 95 for (int i = 0; i < len; i++) 96 sb.append("-"); 97 return sb.toString(); 98 } 99 100 private void writeHeader() throws Exception 101 { 102 SimpleDateFormat sf = new SimpleDateFormat ("dd-MM-yyyy HH:mm:ss"); 103 104 Date date = new Date (System.currentTimeMillis()); 105 PrintWriter pw = new PrintWriter (_fos); 106 pw.println(); 107 108 int len = 38; 109 110 if (_banner != null && _banner.trim().length() > 0) 111 { 112 len = Math.max(len, _banner.length()); 113 } 114 115 String dl = getDivLine(len); 116 pw.println(dl); 117 if (_banner != null && _banner.trim().length() > 0) pw.println(_banner); 118 printProperty(pw, "java.runtime.name"); 119 printProperty(pw, "java.runtime.version"); 120 printProperty(pw, "java.vm.name"); 121 printProperty(pw, "java.vm.version"); 122 printProperty(pw, "java.vm.vendor"); 123 printProperty(pw, "java.class.version"); 124 printProperty(pw, "java.home"); 125 printProperty(pw, "java.version"); 126 printProperty(pw, "file.encoding"); 127 pw.println("Logging started at " + sf.format(date)); 128 pw.println(dl); 129 130 pw.flush(); 131 } 132 133 protected void printProperty(PrintWriter pw, String property) 134 { 135 pw.println(property + "=" + System.getProperty(property)); 136 } 137 138 public FileOutputStream getFileOutputStream() 139 { 140 return _fos; 141 } 142 143 public void flush() 144 { 145 super.flush(); 146 try 147 { 148 _fos.flush(); 149 } 150 catch (IOException iox) 151 { 152 } 154 } 155 156 public void close() 157 { 158 super.close(); 159 160 try 161 { 162 _fos.close(); 163 } 164 catch (IOException iox) 165 { 166 } 168 169 } 170 171 public void write(int b) 172 { 173 super.write(b); 174 175 try 176 { 177 _fos.write(b); 178 } 179 catch (IOException iox) 180 { 181 } 183 } 184 185 public void write(byte buf[], int off, int len) 186 { 187 super.write(buf, off, len); 188 189 try 190 { 191 _fos.write(buf, off, len); 192 } 193 catch (IOException iox) 194 { 195 } 197 } 198 } | Popular Tags |