1 51 package org.apache.fop.messaging; 52 53 import org.apache.avalon.framework.logger.ConsoleLogger; 54 import org.apache.avalon.framework.logger.Logger; 55 56 import java.io.*; 57 import java.util.*; 58 59 87 88 public class MessageHandler { 89 public static final int SCREEN = 0; 90 public static final int FILE = 1; 91 public static final int EVENT = 2; 92 public static final int NONE = 3; 94 private static Logger logger = null; 95 private static String logfileName = "fop.log"; 96 private static PrintWriter writer; 97 private static int outputMethod = SCREEN; 98 private static boolean fileOpened = false; 99 private static boolean appendToFile = true; 100 private static String message = ""; 101 private static String prefix = ""; 102 private static Vector listeners = new Vector(); 103 private static boolean IDisSet = false; 104 private static boolean quiet = false; 105 106 110 111 private static String getMessage() { 112 return message; 113 } 114 115 120 private static void setMessage(String m) { 121 if (IDisSet) { 122 message = getID() + ":" + m; 123 } else { 124 message = m; 125 } 126 } 127 128 132 public static void log(String message) { 133 if (quiet) { 134 return; 135 } 136 137 if (logger == null) { 138 logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); 139 logger.warn("Screen logger not set - Using ConsoleLogger."); 140 } 141 142 setMessage(message); 143 switch (outputMethod) { 144 case SCREEN: 145 logger.info(getMessage()); 146 break; 147 case FILE: 148 if (fileOpened) { 149 writer.print(getMessage()); 150 writer.flush(); 151 } else { 152 openFile(); 153 writer.print(getMessage()); 154 writer.flush(); 155 } 156 break; 157 case EVENT: 158 setMessage(message); 159 Enumeration enum = listeners.elements(); 160 while (enum.hasMoreElements()) { 161 ((MessageListener)enum.nextElement()).processMessage(new MessageEvent(getMessage())); 162 } 163 break; 164 case NONE: 165 break; 167 default: 168 logger.info(message); 169 } 170 } 171 172 176 public static void logln(String message) { 177 log(message); 178 } 179 180 184 185 public static void error(String errorMessage) { 186 if (logger == null) { 187 logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO); 188 logger.warn("Screen logger not set - Using ConsoleLogger."); 189 } 190 191 setMessage(errorMessage); 192 switch (outputMethod) { 193 case SCREEN: 194 logger.error(getMessage()); 195 break; 196 case FILE: 197 if (fileOpened) { 198 writer.print(getMessage()); 199 writer.flush(); 200 } else { 201 openFile(); 202 writer.print(getMessage()); 203 writer.flush(); 204 } 205 break; 206 case EVENT: 207 setMessage(message); 208 Enumeration enum = listeners.elements(); 209 while (enum.hasMoreElements()) { 210 MessageEvent messEv = new MessageEvent(getMessage()); 211 messEv.setMessageType(MessageEvent.ERROR); 212 ((MessageListener)enum.nextElement()).processMessage(messEv); 213 } 214 break; 215 case NONE: 216 break; 218 default: 219 logger.error(errorMessage); 220 } 221 } 222 223 227 public static void errorln(String errorMessage) { 228 error(errorMessage); 229 } 230 231 235 public static void addListener(MessageListener listener) { 236 listeners.add(listener); 237 } 238 239 243 public static void removeListener(MessageListener listener) { 244 listeners.removeElement(listener); 245 } 246 247 251 public static void setScreenLogger(Logger newLogger) { 252 if (newLogger == null) 253 throw new NullPointerException(); 254 logger = newLogger; 255 } 256 257 263 public static void setOutputMethod(int method) { 264 if (method > NONE) { 265 MessageHandler.error("Error: Unknown output method"); 266 } else { 267 outputMethod = method; 268 } 269 } 270 271 275 public static int getOutputMethod() { 276 return outputMethod; 277 } 278 279 284 public static void setLogfileName(String filename, boolean append) { 285 logfileName = filename; 286 appendToFile = append; 287 } 288 289 293 public static String getLogfileName() { 294 return logfileName; 295 } 296 297 300 private static void openFile() { 301 try { 302 writer = 303 new PrintWriter(new FileWriter(logfileName, appendToFile), 304 true); 305 writer.println("\n=============================================="); 306 fileOpened = true; 307 } catch (IOException ioe) { 308 System.err.println("Error: " + ioe); 309 } 310 } 311 312 318 private static String getID() { 319 return Thread.currentThread().toString(); 320 } 321 322 330 331 public static void setID(boolean id) { 332 IDisSet = id; 333 } 334 335 341 public static void setQuiet(boolean quietMode) { 342 quiet = quietMode; 343 } 344 345 } 346 | Popular Tags |