View Javadoc

1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2001-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   *  @version $Id: StdOutAppender.java 1557 2008-01-30 15:42:52Z barteo $
21   */
22  package org.microemu.log;
23  
24  import java.io.PrintStream;
25  
26  /**
27   * @author vlads
28   * 
29   */
30  public class StdOutAppender implements LoggerAppender {
31  
32  	public static boolean enabled = true;
33  	
34  	public static String formatLocation(StackTraceElement ste) {
35  		if (ste == null) {
36  			return "";
37  		}
38  		// Make Line# clickable in eclipse
39  		return ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getFileName() + ":" + ste.getLineNumber()
40  				+ ")";
41  	}
42  
43  	/*
44  	 * (non-Javadoc)
45  	 * 
46  	 * @see org.microemu.log.LoggerAppender#append(org.microemu.log.LoggingEvent)
47  	 */
48  	public void append(LoggingEvent event) {
49  		if (!enabled) {
50  			return;
51  		}
52  		PrintStream out = System.out; 
53      	if (event.getLevel() == LoggingEvent.ERROR) {
54      		out = System.err;
55      	}
56      	String data = "";
57      	if (event.hasData()) {
58      		data = " [" + event.getFormatedData() + "]";
59      	}
60      	String location = formatLocation(event.getLocation());
61      	if (location.length() > 0) {
62      		location = "\n\t  " + location;
63      	}
64      	out.println(event.getMessage() + data + location);
65      	if (event.getThrowable() != null) {
66      		event.getThrowable().printStackTrace(out);
67      	}
68  
69  	}
70  
71  }