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   protected int minprioritylevel = LogPriorities.NOPRIORITY;
00094 
00095 
00100   protected LogManager(Properties props) {
00101     this();
00102     tracer.setLogTracerProperties(props);
00103     tracer.setLogStream(new PrintStream( System.out,true ));
00104     ApplicationContextManager.getManager().addManager(this);
00105   }
00106 
00107   protected LogManager() {
00108     super();
00109     logTable = new HashMap(10);
00110     tracer = new DefaultLogTracer();
00111   }
00112   
00113   public void traceSystemProperties()   {
00114         Properties props = System.getProperties();
00115         Enumeration enums = props.keys();
00116         while (enums.hasMoreElements()){
00117                 String key = (String) enums.nextElement();
00118                 String value = props.getProperty(key);
00119                 LogManager.traceInfo(0, "Property key :"+key+" value:"+value);
00120         }
00121   }
00122   
00128   public void setMinimumPriorityTraced(int priority)    {
00129         this.minprioritylevel = priority;
00130   }
00131 
00136   public static  void attachLogTracerToPriority(int priority, LogTracer t)  {
00137     logTable.put(new Integer(priority), t);
00138   }
00139 
00140 
00147   public synchronized static LogManager getInstance() {
00148     return instance;
00149   }
00150   
00151   public synchronized static void registerLogManager(Properties props) {
00152   System.out.println("LogManager registerLogManager props"+props);
00153         instance = new LogManager(props);
00154         // put in application context to avoid class garbadge
00155         ApplicationContextManager.getManager().addManager(instance);
00156   }
00157 
00158   public static void trace(OpenMISException exp) {
00159     instance.trace(exp.getService(), exp.getPriority(), exp);
00160   }
00161 
00166   public void trace(int service, int priority, Object obj) {
00167         if (priority > this.minprioritylevel) return;
00168     String message = null;
00169     if (obj == null) {
00170       message = "ERROR Null message Logged";
00171     } else if (obj instanceof Exception)  {
00172       CharArrayWriter stream = new CharArrayWriter();
00173       ((Exception)obj).printStackTrace(new PrintWriter(stream));
00174       stream.flush();
00175       stream.close();
00176       message = new String(stream.toCharArray());
00177     } else {
00178       message = obj.toString();
00179     }
00180     if (tracer == null) {
00181       System.out.println(message);
00182     } else  {
00183       LogTracer tracelog = (LogTracer) logTable.get(new Integer(priority));
00184       if (tracelog == null) {
00185         tracelog = tracer;
00186       }
00187       tracelog.trace(service, priority, message);
00188     }
00189 
00190   }
00191 
00196   public static void traceEmergency(int service, Object obj) {
00197     instance.trace(service, LogPriorities.EMERGENCY, obj);
00198   }
00199 
00204   public static void traceCritique(int service, Object obj) {
00205     instance.trace(service, LogPriorities.CRITIQUE, obj);
00206   }
00207 
00212   public static void traceAlert(int service, Object obj) {
00213     instance.trace(service, LogPriorities.ALERT, obj);
00214   }
00215 
00220   public static void traceError(int service, Object obj) {
00221     instance.trace(service, LogPriorities.ERROR, obj);
00222   }
00223 
00228   public static void traceWarning(int service, Object obj) {
00229     instance.trace(service, LogPriorities.WARNING, obj);
00230   }
00231 
00236   public static void traceNotice(int service, Object obj) {
00237     instance.trace(service, LogPriorities.NOTICE, obj);
00238   }
00239 
00244   public static void traceInfo(int service, Object obj) {
00245     instance.trace(service, LogPriorities.INFO, obj);
00246   }
00247 
00252   public static void traceDebug(int service, Object obj) {
00253     instance.trace(service, LogPriorities.DEBUG, obj);
00254   }
00255 
00260   public static void traceUserSynchro(int service, Object obj) {
00261     instance.trace(service, LogPriorities.USERSYNCHRO, obj);
00262   }
00263 
00264         public LogTracer getLogTracer()  {
00265                 return tracer;
00266         }
00267 
00268         public void setLogTracer(LogTracer t)  {
00269                 tracer = t;
00270         }
00271 
00272 }

Generated on Mon Jan 11 21:19:15 2010 for OpenMobileIS by  doxygen 1.5.4