1 30 31 32 package org.hsqldb.lib; 33 34 import java.io.File ; 35 import java.io.FileWriter ; 36 import java.io.PrintWriter ; 37 38 import org.hsqldb.HsqlDateTime; 39 40 52 public class SimpleLog { 53 54 public static int LOG_NONE = 0; 55 public static int LOG_ERROR = 1; 56 public static int LOG_NORMAL = 2; 57 private PrintWriter writer; 58 private int level; 59 60 public SimpleLog(String path, int level, boolean useFile) { 61 62 this.level = level; 63 64 if (level != LOG_NONE) { 65 if (useFile) { 66 File file = new File (path); 67 68 makeLog(file); 69 } else { 70 writer = new PrintWriter (System.out); 71 } 72 } 73 } 74 75 private void makeLog(File file) { 76 77 try { 78 FileUtil.makeParentDirectories(file); 79 80 writer = new PrintWriter (new FileWriter (file.getPath(), true), 81 true); 82 } catch (Exception e) { 83 writer = new PrintWriter (System.out); 84 } 85 } 86 87 public int getLevel() { 88 return level; 89 } 90 91 public PrintWriter getPrintWriter() { 92 return writer; 93 } 94 95 public synchronized void sendLine(int atLevel, String message) { 96 97 if (level >= atLevel) { 98 writer.println(HsqlDateTime.getSytemTimeString() + " " + message); 99 } 100 } 101 102 public synchronized void logContext(int atLevel, String message) { 103 104 if (level < atLevel) { 105 return; 106 } 107 108 String info = HsqlDateTime.getSytemTimeString(); 109 110 Throwable temp = new Throwable (); 112 StackTraceElement [] elements = temp.getStackTrace(); 113 114 if (elements.length > 1) { 115 info += " " + elements[1].getClassName() + "." 116 + elements[1].getMethodName(); 117 } 118 119 writer.println(info + " " + message); 121 } 122 123 public synchronized void logContext(Throwable t, String message) { 124 125 if (level == LOG_NONE) { 126 return; 127 } 128 129 String info = HsqlDateTime.getSytemTimeString(); 130 131 Throwable temp = new Throwable (); 133 StackTraceElement [] elements = temp.getStackTrace(); 134 135 if (elements.length > 1) { 136 info += " " + elements[1].getClassName() + "." 137 + elements[1].getMethodName(); 138 } 139 140 elements = t.getStackTrace(); 141 142 if (elements.length > 0) { 143 info += " " + elements[0].getClassName() + "." 144 + elements[0].getMethodName(); 145 } 146 147 if (message == null) { 149 message = ""; 150 } 151 152 writer.println(info + " " + t.toString() + " " + message); 153 } 154 155 public void flush() { 156 157 if (writer != null) { 158 writer.flush(); 159 } 160 } 161 162 public void close() { 163 164 if (writer != null) { 165 writer.close(); 166 } 167 } 168 } 169 | Popular Tags |