Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

LogManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.common.util.log;
00030 
00031 import java.util.*;
00032 
00033 import org.openmobileis.common.context.ApplicationContextManager;
00034 import org.openmobileis.common.util.exception.OpenMISException;
00035 import java.io.CharArrayWriter;
00036 import java.io.PrintWriter;
00037 import java.io.PrintStream;
00038 
00039 
00040 
00084 public class LogManager {
00085 
00089   protected static LogManager instance = null;
00090   protected static LogTracer tracer;
00091   protected static HashMap logTable;
00092 
00093 
00098   protected LogManager(Properties props) {
00099     this();
00100     tracer.setLogTracerProperties(props);
00101     tracer.setLogStream(new PrintStream( System.out,true ));
00102   }
00103 
00104   protected LogManager() {
00105     super();
00106     logTable = new HashMap(10);
00107     tracer = new DefaultLogTracer();
00108   }
00109 
00114   public static  void attachLogTracerToPriority(int priority, LogTracer t)  {
00115     logTable.put(new Integer(priority), t);
00116   }
00117 
00118 
00125   public synchronized static LogManager getInstance(Properties props) {
00126     if ( instance == null ) {
00127       instance = new LogManager(props);
00128                 //put in application context to avoid class garbadge
00129       ApplicationContextManager.getManager().addManager(instance);
00130     }
00131     return instance;
00132   }
00133 
00134   public static void trace(OpenMISException exp) {
00135     instance.trace(exp.getService(), exp.getPriority(), exp);
00136   }
00137 
00142   public void trace(int service, int priority, Object obj) {
00143     String message = null;
00144     if (obj == null) {
00145       message = "ERROR Null message Logged";
00146     } else if (obj instanceof Exception)  {
00147       CharArrayWriter stream = new CharArrayWriter();
00148       ((Exception)obj).printStackTrace(new PrintWriter(stream));
00149       stream.flush();
00150       stream.close();
00151       message = new String(stream.toCharArray());
00152     } else {
00153       message = obj.toString();
00154     }
00155     if (tracer == null) {
00156       System.out.println(message);
00157     } else  {
00158       LogTracer tracelog = (LogTracer) logTable.get(new Integer(priority));
00159       if (tracelog == null) {
00160         tracelog = tracer;
00161       }
00162       tracelog.trace(service, priority, message);
00163     }
00164 
00165   }
00166 
00171   public static void traceEmergency(int service, Object obj) {
00172     instance.trace(service, LogPriorities.EMERGENCY, obj);
00173   }
00174 
00179   public static void traceCritique(int service, Object obj) {
00180     instance.trace(service, LogPriorities.CRITIQUE, obj);
00181   }
00182 
00187   public static void traceAlert(int service, Object obj) {
00188     instance.trace(service, LogPriorities.ALERT, obj);
00189   }
00190 
00195   public static void traceError(int service, Object obj) {
00196     instance.trace(service, LogPriorities.ERROR, obj);
00197   }
00198 
00203   public static void traceWarning(int service, Object obj) {
00204     instance.trace(service, LogPriorities.WARNING, obj);
00205   }
00206 
00211   public static void traceNotice(int service, Object obj) {
00212     instance.trace(service, LogPriorities.NOTICE, obj);
00213   }
00214 
00219   public static void traceInfo(int service, Object obj) {
00220     instance.trace(service, LogPriorities.INFO, obj);
00221   }
00222 
00227   public static void traceDebug(int service, Object obj) {
00228     instance.trace(service, LogPriorities.DEBUG, obj);
00229   }
00230 
00235   public static void traceUserSynchro(int service, Object obj) {
00236     instance.trace(service, LogPriorities.USERSYNCHRO, obj);
00237   }
00238 
00239         public static LogTracer getLogTracer()  {
00240                 return tracer;
00241         }
00242 
00243         public static void setLogTracer(LogTracer t)  {
00244                 tracer = t;
00245         }
00246 
00247 }

Generated on Thu Oct 6 10:06:33 2005 for OpenMobileIS by  doxygen 1.4.3