Source code for /engineering/autohit-2003/src/autohit/common/AutohitLogInjectorWrapper.javaOriginal file AutohitLogInjectorWrapper.java
   1 /**
   2  * AUTOHIT 2003
   3  * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
   4  * 
   5  * This program is free software; you can redistribute it and/or modify 
   6  * it under the terms of the GNU General Public License as published by 
   7  * the Free Software Foundation; either version 2 of the License, or (at
   8  * your option) any later version.
   9  * This program is distributed in the hope that it will be useful, but
  10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12  * more details.
  13  * 
  14  * You should have received a copy of the GNU General Public License along
  15  * with this program; if not, write to the Free Software Foundation, Inc.,
  16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17  *
  18  * Additional license information can be found in the documentation.
  19  * @author Erich P Gatejen
  20  */
  21 package autohit.common;
  22 import autohit.common.channels.Atom;
  23 import autohit.common.channels.Injector;
  24 
  25 /**
  26  * This is a helper for using channels for logging.
  27  *
  28  * @author Erich P. Gatejen
  29  * @version 1.0
  30  * <i>Version History</i>
  31  * <code>EPG - Rewrite - 9Apr03</code> 
  32  * 
  33  */
  34 public class AutohitLogInjectorWrapper {
  35 
  36 	public Injector sinjector;
  37 	private String sender;
  38 	private boolean debugging;
  39 	private boolean ivePanicked;
  40 
  41 	/**
  42 	 *  Default constructor.
  43 	 */
  44 	public AutohitLogInjectorWrapper() {
  45 
  46 	}
  47 
  48 	/**
  49 	 *  Default constructor.  It will put the logger autohit namespace.
  50 	 *  It lets the instantiator handle any exceptions. 
  51 	 * @param senderID the sender id (you can leave this blank).
  52 	 * @param target the target injector for logging
  53 	 */
  54 	public void init(String senderID, Injector target) {
  55 		sinjector = target;
  56 		sender = senderID;
  57 		ivePanicked = false;
  58 		debugging = false;
  59 	}
  60 
  61 	/**
  62 	 *  Log helper - debug
  63 	 * @param msg Log message
  64 	 * @param num numeric value
  65 	 */
  66 	public void debug(String msg, int num) {
  67 		if (debugging) {
  68 			this.log(msg, num, Atom.DEBUG);
  69 		}
  70 	}
  71 
  72 	/**
  73 	 *  Log helper - debug
  74 	 * @param msg Log message
  75 	 */
  76 	public void debug(String msg) {
  77 		if (debugging) {
  78 			this.log(msg, AutohitErrorCodes.INFORMATIONAL, Atom.DEBUG);
  79 		}
  80 	}
  81 
  82 	/**
  83 	 *  Log helper - debug
  84 	 * @param msg Log message
  85 	 * @param num numeric value
  86 	 */
  87 	public void info(String msg, int num) {
  88 		this.log(msg, num, Atom.ROUTINE);
  89 	}
  90 
  91 	/**
  92 	 *  Log helper - debug
  93 	 * @param msg Log message
  94 	 */
  95 	public void info(String msg) {
  96 		this.log(msg, AutohitErrorCodes.INFORMATIONAL, Atom.ROUTINE);
  97 	}
  98 
  99 	/**
 100 	 *  Log helper - error
 101 	 * @param msg Log message
 102 	 * @param num numeric value
 103 	 */
 104 	public void error(String msg, int num) {
 105 		this.log(msg, num, Atom.FLASH);
 106 	}
 107 
 108 	/**
 109 	 *  Log helper - error
 110 	 * @param msg Log message
 111 	 */
 112 	public void error(String msg) {
 113 		this.log(msg, AutohitErrorCodes.ERROR, Atom.FLASH);
 114 	}
 115 
 116 	/**
 117 	 *  Log helper - warning
 118 	 * @param msg Log message
 119 	 * @param num numeric value
 120 	 */
 121 	public void warning(String msg, int num) {
 122 		this.log(msg, num, Atom.PRIORITY);
 123 	}
 124 
 125 	/**
 126 	 *  Log helper - warning
 127 	 * @param msg Log message
 128 	 */
 129 	public void warning(String msg) {
 130 		this.log(msg, AutohitErrorCodes.WARNING, Atom.PRIORITY);
 131 	}
 132 
 133 	/**
 134 	 *  Log helper - warning
 135 	 * @param msg Log message
 136 	 */
 137 	public void log(String msg, int numeric, int priority) {
 138 		try {
 139 			Atom a = new Atom(Atom.TYPE_LOG, priority, numeric, (Object) msg);
 140 			a.senderID = sender;
 141 			sinjector.post(a);
 142 		} catch (Exception e) {
 143 			// this is a very bad thing.
 144 			if (!ivePanicked) {
 145 				ivePanicked = true;
 146 				System.out.println(
 147 					"PANIC!  PANIC!  PANIC!  PANIC!  PANIC!  PANIC!  PANIC!  PANIC!  The logging system failed in AutohitLogInjectorWrapper.  No exception propagated.  System state undefined.  message="
 148 						+ e.getMessage());
 149 			}
 150 		}
 151 	}
 152 
 153 	/**
 154 	 *  Set debugging flag.  Will early filter debug statements
 155 	 * @param f flag is true or false
 156 	 */
 157 	public void debugFlag(boolean f) {
 158 		debugging = f;
 159 	}
 160 
 161 	/**
 162 	 * Report debugging flag 
 163 	 * @return debugging state
 164 	 */
 165 	public boolean debugState() {
 166 		return debugging;
 167 	}
 168 
 169 }