1 6 7 12 13 package org.netbeans.modules.logger.listeners; 14 15 import java.io.*; 16 import java.util.Properties ; 17 import org.netbeans.modules.logger.UserInput; 18 19 20 26 public abstract class ListenerTools { 27 28 34 private StringBuffer buffer; 35 36 39 private static String name; 40 41 44 private static String level; 45 46 47 52 private String id; 53 54 58 public static final String TS = Long.toString(System.currentTimeMillis()); 59 62 public static final String SEP = System.getProperty("file.separator"); 63 67 public static final String LOG_PATH = findOutLogPath(); 68 72 public static final String LAYOUT_PATH = findOutLayoutPath(); 73 74 75 80 private static FileWriter errorOut; 81 82 83 86 private FileWriter out; 87 88 89 93 public ListenerTools(String who) { 94 if(name==null) findOutProperties(); 95 id = who; 96 buffer = new StringBuffer (); 97 File outFile = new File(LOG_PATH+TS+"."+level+"."+name+"."+who+".log"); 98 try{ 99 out = new FileWriter(outFile); 100 } catch(IOException e){ 101 logError(e); 102 } 103 } 104 105 108 private static void initErrorLog(){ 109 File outFile = new File (LOG_PATH + TS + "." + level + "." + name + ".ERR.log"); 110 try{ 111 errorOut = new FileWriter(outFile); 112 } catch(IOException e){ 113 System.out.println("Logger Module:"+e.getMessage()); 115 } 116 } 117 118 119 124 public static boolean findOutProperties(){ 125 try{ 126 String paramsPath = new String (LOG_PATH+"properties"); 127 File f = new File(paramsPath); 128 if(f.exists()){ 129 FileInputStream input = new FileInputStream(f); 130 Properties p = new Properties (); 131 p.load(input); 132 name = p.getProperty("name"); 133 level = p.getProperty("level"); 134 input.close(); 135 return true; 136 } else{ 137 name = "name"; 138 level = UserInput.LEVEL2.split("\\s+")[0]; 139 } 140 } catch(FileNotFoundException e){ 141 logError(e); 142 } catch(IOException e){ 143 logError(e); 144 } 145 return false; 146 } 147 148 154 public static void saveParameters(String myLevel, String myName){ 155 level = myLevel; 156 name = myName; 157 try{ 158 String paramsPath = new String (LOG_PATH+"properties"); 159 File f = new File(paramsPath); 160 FileOutputStream output = new FileOutputStream(f); 161 Properties p = new Properties (); 162 p.setProperty("level", myLevel); 163 p.setProperty("name", myName); 164 p.store(output, null); 165 output.close(); 166 } catch(IOException e){ 167 logError(e); 168 } 169 } 170 171 175 private static String findOutLogPath(){ 176 String path = System.getProperty("netbeans.user"); 177 path = path + SEP + "var" + SEP + "log" + SEP + "ui" + SEP; 178 File f = new File(path); 179 f.mkdirs(); 180 return path; 181 } 182 183 187 private static String findOutLayoutPath(){ 188 String path=System.getProperty("netbeans.user"); 189 path=path+SEP+"config"+SEP+"Windows2Local"+SEP+"Modes"+SEP; 190 File f = new File(path); 191 f.mkdirs(); 192 return path; 193 } 194 195 196 201 protected String getClassName(Object o){ 202 if(o==null) return "null"; 203 String s = o.getClass().getName(); 204 return s.substring(s.lastIndexOf('.')+1); 205 } 206 207 210 public void close(){ 211 try{ 212 out.close(); 213 errorOut.close(); 214 }catch(IOException e){ 215 System.out.println("Logger Module:"+e.getMessage()); 216 } 217 } 218 219 222 protected void newBuffer(){ 223 buffer = new StringBuffer (); 224 } 225 226 230 protected void append(String s){ 231 if(buffer!=null) buffer.append("\n"+s); 232 } 233 234 237 protected void erase(){ 238 buffer = null; 239 } 240 241 244 protected void flush(){ 245 if(buffer!=null){ 246 try{ 247 out.write(buffer.toString()+"\n"); 248 out.flush(); 249 }catch(IOException e){ 250 logError(e); 251 } 252 } 253 buffer=null; 254 } 255 256 262 public static void logError(Throwable t){ 263 t.printStackTrace(System.out); 264 if(errorOut==null){ 265 initErrorLog(); 266 } 267 268 StackTraceElement element [] = t.getStackTrace(); 269 270 try{ 271 errorOut.write(t.toString()); 272 for(int i=0; i<element.length; i++){ 273 errorOut.write(element[i].toString()+"\n"); 274 } 275 errorOut.write("\n"); 276 errorOut.flush(); 277 }catch(IOException e){ 278 System.out.println("LoggerModule:"+e.getMessage()); 281 } 282 } 283 284 } 285 | Popular Tags |