📄 logger.java
字号:
/* * ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * *//** * Title: <p> * Description: <p> * Copyright: Copyright (c) <p> * Company: <p> * @author * @version 1.0 */package vocal.util;import javax.swing.JLabel;import javax.swing.SwingUtilities;import java.text.SimpleDateFormat;import java.util.Date;import java.util.StringTokenizer;import java.io.ByteArrayOutputStream;import java.io.PrintStream;import java.awt.Color;import javax.swing.border.*;public class Logger{ // Identifiers for the destinations it is possible to send log messages to private static final int DESTINATION_STDOUT = 0; private static final int DESTINATION_TEXTLABEL = 1; private static final int DESTINATION_TEXTAREA = 2; private static final int DESTINATION_FILE = 3; private static final int MAX_DESTINATION = DESTINATION_FILE; // identifiers for the individual message components private static final int COMPONENT_MESSAGE = 0; private static final int COMPONENT_METHOD = 1; private static final int COMPONENT_TIMESTAMP = 2; private static final int COMPONENT_TAG = 3; private static final int MAX_COMPONENT = COMPONENT_TAG; /** * Array stores flags for each destination/message component combination * which determine whether the particular message component will be written * to that destination. */ private static boolean[][] msgConfig = new boolean[MAX_DESTINATION + 1][MAX_COMPONENT + 1]; private static final String TRACE_TAG = "---TRACE--- "; private static final String ERROR_TAG = "---ERROR--- "; private static String messageTag = TRACE_TAG; private static final SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss:SSSS"); private static JLabel textLabel = new JLabel(); static { // turn of logging for all message components for all destinations for (int i = 1; i < MAX_COMPONENT; i++) { for (int j = 0; j < MAX_DESTINATION; j++) { msgConfig[j][i] = false; } } // but send the message itself everywhere for (int i = 0; i <= MAX_DESTINATION; i++) { msgConfig[i][COMPONENT_MESSAGE] = true; } // log the method and tag to stdout msgConfig[DESTINATION_STDOUT][COMPONENT_METHOD] = true; msgConfig[DESTINATION_STDOUT][COMPONENT_TAG] = true; } private Logger() {} public static void setMessageConfig(int destination, int messageId, boolean enabled) { msgConfig[destination][messageId] = enabled; } public static void swingLog(final String msg) { Runnable newLog = new Runnable() { public void run() { log(msg); } }; SwingUtilities.invokeLater(newLog); } public static void log(String msg) { for (int destination = 0; destination < MAX_DESTINATION; destination++) { boolean[] enabled = msgConfig[destination]; String message = composeLogMessage(msg, enabled); if (!message.equals("")) { switch (destination) { case DESTINATION_STDOUT: { System.out.println(message); } break; case DESTINATION_TEXTLABEL: { textLabel.setText(message); } break; case DESTINATION_FILE: {} break; case DESTINATION_TEXTAREA: {} break; } // end switch destination } // end if there was a message } // end for each destination } private static String composeLogMessage(String usrMessage, boolean[] componentEnabled) { String message = ""; if (componentEnabled[COMPONENT_TIMESTAMP]) { long currentTime = System.currentTimeMillis(); String date = dateFormatter.format(new Date(currentTime)); message = date + " - "; } if (componentEnabled[COMPONENT_TAG]) { message += messageTag; } if (componentEnabled[COMPONENT_MESSAGE]) { message += usrMessage; } if (componentEnabled[COMPONENT_METHOD]) { message += " - " + getMethod(); } return message; } private static String getMethod() { String method = ""; try { throw new Exception("message"); } catch (Exception e) { String trace = getStackTrace(e); StringTokenizer traceTokenizer = new StringTokenizer(trace, "\n", false); String token = traceTokenizer.nextToken(); // name of the exception token = traceTokenizer.nextToken(); while (token.indexOf("Logger") == -1) //in 1.3, throwable and exception show up in the stack trace { token = traceTokenizer.nextToken(); } while (token.indexOf("Logger") != -1) { token = traceTokenizer.nextToken(); } method = token.trim(); } return method; } private static String getStackTrace(Throwable ex) { ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(byteOutput); ex.printStackTrace(printStream); return byteOutput.toString(); } public static JLabel getLogLabel() { return textLabel; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -