1 package rero.gui; 2 3 import java.util.*; 4 import java.io.*; 5 6 import rero.config.*; 7 import rero.util.*; 8 9 import rero.client.*; 10 11 import rero.net.*; 12 13 public class BuiltInLogger 14 { 15 protected static boolean isEnabled = ClientState.getClientState().isOption("log.enabled", ClientDefaults.log_enabled); 16 protected static boolean timeStamp = ClientState.getClientState().isOption("log.timestamp", ClientDefaults.log_timestamp); 17 protected static boolean stripColors = ClientState.getClientState().isOption("log.strip", ClientDefaults.log_strip); 18 protected static HashMap logHandles = new HashMap(); 19 20 protected static ClientStateListener listener = null; 21 22 protected SocketInformation socket = null; 23 protected IRCSession client = null; 24 25 public BuiltInLogger(IRCSession _client) 26 { 27 client = _client; 28 29 if (listener == null) 30 { 31 listener = new LoggerPropListener(); 32 33 ClientState.getClientState().addClientStateListener("log.enabled", listener); 34 ClientState.getClientState().addClientStateListener("log.timestamp", listener); 35 ClientState.getClientState().addClientStateListener("log.strip", listener); 36 ClientState.getClientState().addClientStateListener("log.saveto", listener); 37 } 38 } 39 40 public void logMessage(String window, String text) 41 { 42 try 43 { 44 if (socket == null && client.getCapabilities() != null && client.getCapabilities().getSocketConnection() != null) 45 socket = client.getCapabilities().getSocketConnection().getSocketInformation(); 46 47 PrintWriter writer = getFileHandle(socket, window); 48 49 if (writer != null) 50 { 51 if (stripColors) 52 text = StringUtils.strip(text); 53 54 if (timeStamp) 55 text = ClientUtils.TimeDateStamp(ClientUtils.ctime()) + " " + text; 56 57 writer.println(text); 60 } 62 } 63 catch (Exception ex) 64 { 65 ex.printStackTrace(); 66 } 67 } 68 69 public boolean isEnabled() 70 { 71 return isEnabled; 72 } 73 74 private static class LoggerPropListener implements ClientStateListener 75 { 76 public void propertyChanged(String prop, String parm) 77 { 78 if (prop.equals("log.enabled")) 79 isEnabled = ClientState.getClientState().isOption("log.enabled", ClientDefaults.log_enabled); 80 81 if (prop.equals("log.saveto")) 82 { 83 Iterator i = logHandles.values().iterator(); 84 while (i.hasNext()) 85 { 86 PrintWriter temp = (PrintWriter)i.next(); 87 try { temp.flush(); temp.close(); } catch (Exception ex) { ex.printStackTrace(); } 88 } 89 90 logHandles.clear(); 91 } 92 93 timeStamp = ClientState.getClientState().isOption("log.timestamp", ClientDefaults.log_timestamp); 94 stripColors = ClientState.getClientState().isOption("log.strip", ClientDefaults.log_strip); 95 } 96 } 97 98 public static String getLogFileName(SocketInformation socket, String window) 99 { 100 String server = "Unknown"; 101 102 if (socket != null && socket.network != null && socket.network.length() > 0) 103 server = socket.network; 104 105 if (window.length() == 0) 106 window = rero.gui.windows.StatusWindow.STATUS_NAME; 107 108 if (window.charAt(0) == '=') 109 { 110 server = "dcc_chat"; 111 window = window.substring(1, window.length()); 112 } 113 114 File logDir = new File(ClientState.getClientState().getString("log.saveto", ClientDefaults.log_saveto)); 115 logDir = new File(logDir, server); 116 117 String filename = window.replaceAll("[^\\w\\#\\!\\$\\(\\)\\@\\^\\`\\{\\}\\']", "_"); 119 File output = new File(logDir, filename + ".log"); 120 return output.getAbsolutePath(); 121 } 122 123 private PrintWriter getFileHandle(SocketInformation socket, String window) 124 { 125 String server = "Unknown"; 126 127 if (socket != null && socket.network != null && socket.network.length() > 0) 128 server = socket.network; 129 130 if (window.length() == 0) 131 window = rero.gui.windows.StatusWindow.STATUS_NAME; 132 133 if (window.charAt(0) == '=') 134 { 135 server = "dcc_chat"; 136 window = window.substring(1, window.length()); 137 } 138 139 if (logHandles.containsKey(server + window)) 140 return (PrintWriter)logHandles.get(server + window); 141 142 try 143 { 144 File logDir = new File(ClientState.getClientState().getString("log.saveto", ClientDefaults.log_saveto)); 145 logDir = new File(logDir, server); 146 147 if (!logDir.exists()) 148 logDir.mkdirs(); 149 150 String filename = window.replaceAll("[^\\w\\#\\!\\$\\(\\)\\@\\^\\`\\{\\}\\']", "_"); 152 File output = new File(logDir, filename + ".log"); 153 154 PrintWriter rv = new PrintWriter(new FileOutputStream(output, true), true); 155 156 logHandles.put(server + window, rv); 157 158 return rv; 159 } 160 catch (Exception ex) 161 { 162 ex.printStackTrace(); 163 } 164 165 return null; 166 } 167 } 168 | Popular Tags |