MyLog

package iPhone; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.net.UnknownHostException; import java.util.Calendar; import java.util.GregorianCalendar; public class MyLog { private FileWriter fw; private PrintWriter pw; private PrintStream ps; private IOData ioData; public MyLog(String path, IOData ioData, String fileName) { this.ioData = ioData; this.createFile(path, fileName); this.info(fileName + ". [Host Name]: " + this.getHostName()); } public void createFile(String path, String fileName){ try { this.fw = new FileWriter(path + "\\" + fileName + ".txt", true); this.pw = new PrintWriter(fw); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();} } private String getHostName(){ java.net.InetAddress localMachine = null; try { localMachine = java.net.InetAddress.getLocalHost(); } catch (UnknownHostException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace(); } return localMachine.getHostName(); } public void debug(String stringa){ try { this.fw.write("[" + this.now() + "][DEBUG] " + stringa + "\n"); this.fw.flush(); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();} } public void info(String stringa){ try { this.fw.write("[" + this.now() + "][INFO] " + stringa + "\n"); this.fw.flush(); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();} } public void error(String stringa){ try { this.fw.write("[" + this.now() + "][ERROR] " + stringa + "\n"); this.fw.flush(); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();} } public void errorException(Exception e){ try { this.fw.write("[" + this.now() + "][ERROR] "); } catch (IOException f) { ioData.sendMessage(e.getMessage()); e.printStackTrace();} e.printStackTrace(this.pw); this.pw.flush(); } public void closeFile(){ try { this.fw.flush(); this.fw.close(); this.pw.flush(); this.pw.close(); } catch (IOException e) { ioData.sendMessage(e.getMessage()); e.printStackTrace();} } private String now() { Calendar gc = new GregorianCalendar(); int day_ = gc.get(Calendar.DAY_OF_MONTH)-1; String day = day_ + ""; if (day_ < 10) day = "0" + day_; int month_ = gc.get(Calendar.MONTH)+1; String month = month_ + ""; if (month_ < 10) month = "0" + month_; String year = gc.get(Calendar.YEAR)+ ""; int sec_ = gc.get(Calendar.SECOND); String sec = sec_ + ""; if (sec_ < 10) sec = "0" + sec_; int min_ = gc.get(Calendar.MINUTE); String min = min_ + ""; if (min_ < 10) min = "0" + min_; int hour_ = gc.get(Calendar.HOUR_OF_DAY); String hour = hour_ + ""; if (hour_ < 10) hour = "0" + hour_; return year + "/" + month + "/" + day + " " + hour + ":" + min; } }