Index: lutinutil/src/java/org/codelutin/util/CategorisedListenerSet.java diff -u lutinutil/src/java/org/codelutin/util/CategorisedListenerSet.java:1.3 lutinutil/src/java/org/codelutin/util/CategorisedListenerSet.java:1.4 --- lutinutil/src/java/org/codelutin/util/CategorisedListenerSet.java:1.3 Thu Aug 12 17:04:12 2004 +++ lutinutil/src/java/org/codelutin/util/CategorisedListenerSet.java Thu Aug 12 17:44:31 2004 @@ -23,9 +23,9 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * - * Mise a jour: $Date: 2004/08/12 17:04:12 $ + * Mise a jour: $Date: 2004/08/12 17:44:31 $ * par : $Author: bpoussin $ */ @@ -159,7 +159,7 @@ protected ListenerSet getAllListeners(Object category){ ListenerSet result = new ListenerSet(listenerClass); if(category == ALL){ - for(Iterator i=listeners.value().iterator(); i.hasNext();){ + for(Iterator i=listeners.values().iterator(); i.hasNext();){ result.addAll((ListenerSet)i.next()); } }else{ Index: lutinutil/src/java/org/codelutin/util/Log.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/Log.java:1.1 --- /dev/null Thu Aug 12 17:44:36 2004 +++ lutinutil/src/java/org/codelutin/util/Log.java Thu Aug 12 17:44:31 2004 @@ -0,0 +1,171 @@ +/* *##% + * Copyright (C) 2002, 2003 Code Lutin, Cédric Pineau, + Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * Log.java + * + * Created: 12 août 2004 + * + * @author Benjamin Poussin + * @version $Revision: 1.1 $ + * + * Mise a jour: $Date: 2004/08/12 17:44:31 $ + * par : $Author: bpoussin $ + */ + +package org.codelutin.util; + +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.EventListener; +import java.util.EventObject; + +/** +* Cette classe permet de mettre en place un monitoring d'application simplement. +* Le développeur a chaque fois qu'il le souhaite ajoute dans son code un +* Log.logUserInfo("...", "....") qui indique un message que l'utilisateur est +* suceptible de vouloir, par exemple le résultat de la sauvegarde d'un fichier. +*

+* Il suffit ensuite de creer un objet qui herite de {@link #LogListener}, puis +* de l'enregistrer sur certaine category d'event il recevra alors les +* evenements de l'utilisateur. +*

+* Une utilisation peut-être la bar de status qui afficherait le message. +*

+* exemple de code +*

+* LogListener l = new StatusBar();
+* Log.addLogListener(l);
+* ...
+* Log.logUserInfo("SAVE", "Sauvegarde réussi");
+* 
+*/ +public class Log { // Log + + static private Log LOG_INSTANCE = new Log(); + + /** + * L'interface que doivent respecter un listener + */ + static public interface LogListener extends EventListener { + public void logSend(LogEvent e); + } + + /** + * Les events envoyes aux listeners + */ + static public class LogEvent extends EventObject { + protected String category; + protected Level level; + protected String message; + protected Throwable exception; + + public LogEvent(Object source, String category, Level level, String message, Throwable exception){ + super(source==null?LOG_INSTANCE:source); + this.category = category; + this.level = level; + this.message = message; + this.exception = exception; + } + public String getCategory(){ + return category; + } + public Level getLevel() { + return level; + } + public String getMessage() { + return message; + } + /** + * L'exception envoyé dans le log, si le log ne contient pas d'exception + * alors null est retourne. + */ + public Throwable getException() { + return exception; + } + } + + /** + * Le Level pour les log utilisateur + */ + static private class UserLevel extends Level { + public UserLevel(String name, int value){ + super(name, value); + } + } + + /** + * Tous les listeners + */ + static protected CategorisedListenerSet listeners = new CategorisedListenerSet(LogListener.class); + + /** Level.INFO = 700 Level.FINE=500 **/ + public static final Level USER_INFO = new UserLevel("USERINFO", 600); + + /** + * Ajoute un listener sur tous les logs envoye + */ + static public void addLogListener(LogListener l){ + listeners.add(LOG_INSTANCE, l); + } + /** + * enleve un listener sur tous les logs envoye + */ + static public void removeLogListener(LogListener l){ + listeners.remove(LOG_INSTANCE, l); + } + /** + * Ajoute un listener sur une certaine category de log + */ + static public void addLogListener(LogListener l, String category){ + listeners.add(category, l); + } + /** + * enleve un listener sur une certaine category de log + */ + static public void removeLogListener(LogListener l, String category){ + listeners.remove(category, l); + } + + static protected void fire(String category, Level level, String message, Throwable exception){ + LogEvent e = new LogEvent(null, category, level, message, exception); + try{ + listeners.fire(category, "logSend", e); + listeners.fire(LOG_INSTANCE, "logSend", e); + }catch(Exception eee){ + Logger.getLogger(Log.class.getName() + ".fire").log(Level.WARNING, "Error during send log event", eee); + } + } + + /** + * Ajoute un message dans le USER_LEVEL. + * @param category la category du message, souvent un nom de module d'une + * application. + * @param message le message a envoyer + */ + static public void logUserInfo(String category, String message){ + logUserInfo(category, message, null); + } + + static public void logUserInfo(String category, String message, Throwable e){ + Logger.getLogger(category).log(USER_INFO, message); + fire(category, USER_INFO, message, e); + } + +} // Log