LogManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
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 
00036 import java.io.CharArrayWriter;
00037 import java.io.PrintWriter;
00038 import java.io.PrintStream;
00039 
00040 
00041 
00085 public class LogManager {
00086 
00090   protected static LogManager instance = null;
00091   protected LogTracer tracer;
00092   protected static HashMap logTable;
00093 
00094 
00099   protected LogManager(Properties props) {
00100     this();
00101     tracer.setLogTracerProperties(props);
00102     tracer.setLogStream(new PrintStream( System.out,true ));
00103     ApplicationContextManager.getManager().addManager(this);
00104   }
00105 
00106   protected LogManager() {
00107     super();
00108     logTable = new HashMap(10);
00109     tracer = new DefaultLogTracer();
00110   }
00111 
00116   public static  void attachLogTracerToPriority(int priority, LogTracer t)  {
00117     logTable.put(new Integer(priority), t);
00118   }
00119 
00120 
00127   public synchronized static LogManager getInstance() {
00128     return instance;
00129   }
00130   
00131   public synchronized static void registerLogManager(Properties props) {
00132         instance = new LogManager(props);
00133         // put in application context to avoid class garbadge
00134         ApplicationContextManager.getManager().addManager(instance);
00135   }
00136 
00137   public static void trace(OpenMISException exp) {
00138     instance.trace(exp.getService(), exp.getPriority(), exp);
00139   }
00140 
00145   public void trace(int service, int priority, Object obj) {
00146     String message = null;
00147     if (obj == null) {
00148       message = "ERROR Null message Logged";
00149     } else if (obj instanceof Exception)  {
00150       CharArrayWriter stream = new CharArrayWriter();
00151       ((Exception)obj).printStackTrace(new PrintWriter(stream));
00152       stream.flush();
00153       stream.close();
00154       message = new String(stream.toCharArray());
00155     } else {
00156       message = obj.toString();
00157     }
00158     if (tracer == null) {
00159       System.out.println(message);
00160     } else  {
00161       LogTracer tracelog = (LogTracer) logTable.get(new Integer(priority));
00162       if (tracelog == null) {
00163         tracelog = tracer;
00164       }
00165       tracelog.trace(service, priority, message);
00166     }
00167 
00168   }
00169 
00174   public static void traceEmergency(int service, Object obj) {
00175     instance.trace(service, LogPriorities.EMERGENCY, obj);
00176   }
00177 
00182   public static void traceCritique(int service, Object obj) {
00183     instance.trace(service, LogPriorities.CRITIQUE, obj);
00184   }
00185 
00190   public static void traceAlert(int service, Object obj) {
00191     instance.trace(service, LogPriorities.ALERT, obj);
00192   }
00193 
00198   public static void traceError(int service, Object obj) {
00199     instance.trace(service, LogPriorities.ERROR, obj);
00200   }
00201 
00206   public static void traceWarning(int service, Object obj) {
00207     instance.trace(service, LogPriorities.WARNING, obj);
00208   }
00209 
00214   public static void traceNotice(int service, Object obj) {
00215     instance.trace(service, LogPriorities.NOTICE, obj);
00216   }
00217 
00222   public static void traceInfo(int service, Object obj) {
00223     instance.trace(service, LogPriorities.INFO, obj);
00224   }
00225 
00230   public static void traceDebug(int service, Object obj) {
00231     instance.trace(service, LogPriorities.DEBUG, obj);
00232   }
00233 
00238   public static void traceUserSynchro(int service, Object obj) {
00239     instance.trace(service, LogPriorities.USERSYNCHRO, obj);
00240   }
00241 
00242         public LogTracer getLogTracer()  {
00243                 return tracer;
00244         }
00245 
00246         public void setLogTracer(LogTracer t)  {
00247                 tracer = t;
00248         }
00249 
00250 }

Generated on Mon Dec 4 11:03:27 2006 for OpenMobileIS by  doxygen 1.5.1-p1