1 package com.dwipal; 2 3 4 import javax.swing.*; 5 import java.io.*; 6 import java.util.*; 7 8 public class DwSnmpMibOutputHandler 9 { 10 11 JTextArea outputText; 12 JTextArea outputError; 13 boolean doLog=false; 14 boolean autoScroll=true; 15 static BufferedWriter outfile; 16 17 DwSnmpMibOutputHandler() { 18 } 19 20 public void setAutoScroll(boolean as) { 21 autoScroll=as; 22 } 23 public void setOutput(JTextArea o) { 24 outputText=o; 25 26 } 27 28 public void setOutputError (JTextArea e) { 29 outputError=e; 30 } 31 32 33 void showErrorMessage(String s) { 34 JOptionPane.showMessageDialog(outputText,s,"Mib Browser",JOptionPane.OK_OPTION); 35 } 36 37 38 public void setLogging(boolean log) { 39 40 try { 41 if(log==true) { 42 String strFileName=getLogFileName(); 43 outfile=new BufferedWriter(new FileWriter(strFileName,true)); 44 outfile.write("\n**********************************************************\n"); 45 outfile.write("MIB Browser Started at : " + new Date()); 46 outfile.write("\n**********************************************************\n"); 47 System.out.println("Output log file: "+ strFileName); 48 this.doLog=true; 49 50 java.util.Timer tmr=new java.util.Timer (true); 51 class SnmpTimerTask extends java.util.TimerTask { 52 public void run() { 53 try { 54 outfile.flush(); 55 } catch (Exception e) { 56 System.out.println("Error in writing to log file: " + e); 57 } 58 } 59 }; 60 long lFlushTime=getFlushTime(); 61 System.out.println("Log will be refreshed every " + lFlushTime/1000 + " seconds."); 62 tmr.schedule(new SnmpTimerTask(),lFlushTime,lFlushTime); 63 64 65 Thread thrdFlush=new Thread (new Runnable () { 66 public void run() { 67 try { 68 System.out.println("Have a nice day !!"); 69 outfile.write("\n**********************************************************\n"); 70 outfile.write("MIB Browser Stopped at : " + new Date()); 71 outfile.write("\n**********************************************************\n"); 72 outfile.flush(); 73 outfile.close(); 74 } catch (Exception e) { 75 System.out.println("Error while writing to log file: "+ e); 76 } 77 } 78 }); 79 Runtime.getRuntime().addShutdownHook(thrdFlush); 80 81 } else outfile.close(); 82 }catch(Exception e) { 83 System.out.println("Error : Cannot log" + e.toString()); 84 return; 86 } 87 doLog=true; 88 } 89 90 private String getLogFileName() { 91 String strFileName=System.getProperty("logfilename"); 92 if(strFileName==null) { 93 strFileName="mibbrowser.log"; 94 } 95 return strFileName; 96 } 97 98 private long getFlushTime() { 99 long lTime=0; 100 String strTime=System.getProperty("logrefreshtime"); 101 if(strTime!=null) { 102 try { 103 lTime=Long.parseLong(strTime); 104 lTime=lTime*1000; 105 } catch (Exception e) { 106 System.out.println("Invalid value for log refresh time. default will be used."); 107 } 108 } 109 110 if(lTime<1000) { lTime=60*1000; } 113 return lTime; 114 } 115 116 public void println(String s) { 117 if(outputText !=null ) { 118 outputText.append("\n"+s); 119 if(autoScroll==true) outputText.setCaretPosition(outputText.getDocument().getLength() - 1); 120 } 121 try { { 124 outfile.write(s+"\n"); 125 } 126 }catch(Exception e) {System.out.println(e.toString());} 127 } 128 129 public void print(String s) { 130 if(outputText !=null) { 131 outputText.append (s); 132 if(autoScroll==true) outputText.setCaretPosition(outputText.getDocument().getLength() - 1); 133 } 134 else System.out.println(s); 135 try { outfile.write(s); 137 }catch(Exception e) {System.out.println(e.toString());} 138 139 } 140 141 public void printError(String e) { 142 if(outputError!=null) outputError.append("\n"+e); 143 else System.err.println(e); 144 try { outfile.write("\n"+e+"\n"); 146 }catch(Exception ex) {System.out.println(e.toString());} 147 148 } 149 } 150
| Popular Tags
|