⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 print.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description://  General Printing/Logging utilities.// ----------------------------------------------------------------------------// Change History://  2006/03/26  Martin D. Flynn//     -Initial release//  2006/04/23  Martin D. Flynn//     -Updated to support a more granular message logging.  Eventually, this//      should be modified to support Log4J.//  2006/06/30  Martin D. Flynn//     -Repackaged//  2007/03/13  Martin D. Flynn//     -Optimized calls to 'getLogLevel' and 'getLogHeaderLevel'//  2007/09/16  Martin D. Flynn//     -Moved all runtime configuration initialization to 'initRTConfig()'//  2008/03/28  Martin D. Flynn//     -Wrapped System.[out|err] in a PrintWriter that understands UTF-8 encoding.// ----------------------------------------------------------------------------package org.opengts.util;import java.io.*;import java.net.*;import java.sql.*;import java.text.*;public class Print{    // ------------------------------------------------------------------------    public  static final int    LOG_UNDEFINED       = -1;    public  static final int    LOG_OFF             = 0;    public  static final int    LOG_FATAL           = 1;    public  static final int    LOG_ERROR           = 2;    public  static final int    LOG_WARN            = 3;    public  static final int    LOG_INFO            = 4;    public  static final int    LOG_DEBUG           = 5;    public  static final int    LOG_ALL             = 6;    // ------------------------------------------------------------------------    private static final String _JAVA               = ".java";        // ------------------------------------------------------------------------    private static PrintStream stdout               = null;    private static PrintStream stderr               = null;    private static PrintStream sysStdout            = null;    private static PrintStream sysStderr            = null;        // ------------------------------------------------------------------------    private static boolean printLogIncludeFrame     = true;    private static boolean printLogIncludeDate      = false;    private static boolean printEmailExceptions     = false;    private static String  printLogName             = null;    private static File    printLogFile             = null;    private static long    printMaxLogFileSize      = 0L;    private static int     printLogLevel            = LOG_INFO;    private static int     printLogHeaderLevel      = LOG_INFO;    public static void initRTConfig()    {        if (RTConfig.isInitialized()) {                        /* logging level */            printLogLevel        = Print.parseLogLevel(RTConfig.getString(RTKey.LOG_LEVEL,null));            printLogHeaderLevel  = Print.parseLogLevel(RTConfig.getString(RTKey.LOG_LEVEL_HEADER,null));            /* log message format */            printLogIncludeFrame = RTConfig.getBoolean(RTKey.LOG_INCL_STACKFRAME,false) || Print.isDebugLoggingLevel();            printLogIncludeDate  = RTConfig.getBoolean(RTKey.LOG_INCL_DATE,false);                        /* email exceptions? */            printEmailExceptions = RTConfig.getBoolean(RTKey.LOG_EMAIL_EXCEPTIONS,false);                        /* max log file size */            printMaxLogFileSize  = RTConfig.getLong(RTKey.LOG_FILE_MAX_SIZE,0L);                        /* log name */            printLogName = RTConfig.getString(RTKey.LOG_NAME,null);            if ((printLogName == null) || printLogName.equals("")) {                printLogName = RTConfig.getString(RTKey.WEBAPP_CONTEXT_NAME, null);                if ((printLogName == null) || printLogName.equals("")) {                    String cn = RTConfig.getString(RTKey.MAIN_CLASS, null);                    if (cn != null) {                        int p = cn.lastIndexOf(".");                        if (p >= 0) {                            printLogName = cn.substring(p+1);                        }                    }                }                if ((printLogName == null) || printLogName.equals("")) {                    printLogName = RTConfig.isWebApp()? "webapp" : "main";                }                RTConfig.setString(RTKey.LOG_NAME, printLogName);            }            /* log file */            String fileStr = RTConfig.insertKeyValues(RTConfig.getString(RTKey.LOG_FILE,null));            printLogFile = ((fileStr != null) && !fileStr.equals(""))? new File(fileStr) : null;        } else {                        Print.sysPrintln("ERROR: RTConfig has not been initialized!");                    }    }    protected static boolean _includeStackFrame()    {        return printLogIncludeFrame;    }    protected static boolean _includeDate()    {        return printLogIncludeDate;    }    protected static boolean _emailExceptions()    {        return printEmailExceptions;    }    // ------------------------------------------------------------------------    private static String localhostName = null;    public static String getHostName()    {        /* host name */        if (Print.localhostName == null) {            try {                String hd = InetAddress.getLocalHost().getHostName();                int p = hd.indexOf(".");                Print.localhostName = (p >= 0)? hd.substring(0,p) : hd;            } catch (UnknownHostException uhe) {                Print.localhostName = "UNKNOWN";            }        }        return Print.localhostName;    }    // ------------------------------------------------------------------------    public static String formatDate(String fmt)    {        return (new DateTime()).format(fmt,null);        //java.util.Date nowDate = new java.util.Date(System.currentTimeMillis());        //SimpleDateFormat sdf = new SimpleDateFormat(fmt);        //StringBuffer sb = new StringBuffer();        //sdf.format(nowDate, sb, new FieldPosition(0));        //return sb.toString();    }    // ------------------------------------------------------------------------    public static void setStdout(PrintStream out)    {        Print.stdout = out;    }    public static void setStderr(PrintStream err)    {        Print.stderr = err;    }    public static PrintStream getStdout()    {        return (Print.stdout != null)? Print.stdout : Print.getSystemOut();    }    public static PrintStream getStderr()    {        return (Print.stderr != null)? Print.stderr : Print.getSystemErr();    }    // ------------------------------------------------------------------------        private static String printEncoding = null;    private static PrintStream sysOut = null;    private static PrintStream sysErr = null;        public static void setEncoding(String enc)    {        Print.printEncoding = ((enc != null) && !enc.equals(""))? enc : null;        Print.sysOut = null;        Print.sysErr = null;    }        public static String getEncoding()    {        return (Print.printEncoding != null)? Print.printEncoding : StringTools.getCharacterEncoding();    }    public static PrintStream getSystemOut()    {        if (Print.sysOut == null) {            try {                Print.sysOut = new PrintStream(System.out, true, Print.getEncoding());            } catch (UnsupportedEncodingException uee) {                Print.sysOut = System.out;            }        }        return Print.sysOut;    }        public static PrintStream getSystemErr()    {        if (Print.sysErr == null) {            try {                Print.sysErr = new PrintStream(System.err, true, "UTF-8");            } catch (UnsupportedEncodingException uee) {                Print.sysErr = System.err;            }        }        return Print.sysErr;    }    // ------------------------------------------------------------------------    protected static String _getStackFrame(int frame)    {        /* extract stack frame */        Throwable t = new Throwable();        t.fillInStackTrace();        StackTraceElement st[] = t.getStackTrace();        StackTraceElement sf = (st != null)? st[frame + 1] : null;        /* no stack frame? */        if (sf == null) {            return "?";        }        /* get file */        String clazz = sf.getClassName();        String file  = sf.getFileName();        if (file == null) {            // Java code was compiled with 'debug=false'            int p = 0;            for (; (p < clazz.length()) && !Character.isUpperCase(clazz.charAt(p)); p++);            if (p < clazz.length()) { clazz = clazz.substring(p); }        } else        if (file.toLowerCase().endsWith(_JAVA)) {             file = file.substring(0, file.length() - _JAVA.length());             int p = clazz.indexOf(file);            if (p >= 0) { clazz = clazz.substring(p); }        }        /* format frame description */        StringBuffer sb = new StringBuffer();        sb.append(clazz);        sb.append(".").append(sf.getMethodName());        sb.append(":").append(sf.getLineNumber());        return sb.toString();    }    // ------------------------------------------------------------------------    public static void _println(String msg)    {        // Does not use RTConfig        Print._println(null, msg);    }        public static void _println(PrintStream ps, String msg)    {        // Does not use RTConfig        Print._print(ps, 1, true, msg + "\n");    }    protected static void _println(PrintStream ps, int frame, boolean printFrame, String msg)    {        // Does not use RTConfig        Print._print(ps, frame + 1, printFrame, msg + "\n");    }    protected static void _println(PrintStream ps, int frame, String msg)    {        Print._print(ps, frame + 1, _includeStackFrame(), msg + "\n");    }    protected static void _print(PrintStream ps, int frame, String msg)    {        Print._print(ps, frame + 1, _includeStackFrame(), msg);    }    protected static void _print(PrintStream ps, int frame, boolean printFrame, String msg)    {        // - use of RTConfig is NOT allowed in this method!        // - if not writing to 'Print.stdout', then we really want to open/close this file        /* Print stream */        PrintStream out = (ps != null)? ps : Print.getSysStdout();        /* write */        if (out != null) {            StringBuffer sb = new StringBuffer();            if (printFrame) {                sb.append("[");                sb.append(_getStackFrame(frame + 1));                sb.append("] ");            }            sb.append(msg);            out.print(sb.toString());            out.flush();        }    }    // ------------------------------------------------------------------------    private static boolean allOutputToStdout = false;        public static void setAllOutputToStdout(boolean state)    {        Print.allOutputToStdout = state;    }        public static void setSysStdout(PrintStream out)    {        Print.sysStdout = out;    }    public static PrintStream getSysStdout()    {        return (Print.sysStdout != null)? Print.sysStdout : Print.getStdout();    }    public static void setSysStderr(PrintStream out)    {        Print.sysStderr = out;    }    public static PrintStream getSysStderr()    {        if (Print.allOutputToStdout) {            return Print.getSysStdout();        } else {            return (Print.sysStderr != null)? Print.sysStderr : Print.getStderr();        }    }    // ------------------------------------------------------------------------    public static void sysPrint(String msg)    {        PrintStream out = Print.getSysStdout();        Print._print(out, 1, false, msg);    }    public static void sysPrint(StringBuffer msg)    {        PrintStream out = Print.getSysStdout();        Print._print(out, 1, false, msg.toString());    }    public static void sysPrintln(String msg)    {        PrintStream out = Print.getSysStdout();        Print._println(out, 1, false, msg);    }    public static void sysPrintln(StringBuffer msg)    {        PrintStream out = Print.getSysStdout();        Print._println(out, 1, false, msg.toString());    }    // ------------------------------------------------------------------------    public static void errPrint(String msg)    {        PrintStream out = Print.getSysStderr();        Print._print(out, 1, false, msg);    }    public static void errPrint(StringBuffer msg)    {        PrintStream out = Print.getSysStderr();        Print._print(out, 1, false, msg.toString());    }    public static void errPrintln(String msg)    {        PrintStream out = Print.getSysStderr();        Print._println(out, 1, false, msg);    }    public static void errPrintln(StringBuffer msg)    {        PrintStream out = Print.getSysStderr();        Print._println(out, 1, false, msg.toString());    }    // ------------------------------------------------------------------------    public static void print(String msg)    {        Print._print(null, 1, false, msg);    }    public static void print(StringBuffer msg)    {        Print._print(null, 1, false, msg.toString());    }    public static void println(String msg)    {        Print._println(null, 1, false, msg);    }    public static void println(StringBuffer msg)    {        Print._println(null, 1, false, msg.toString());    }    // ------------------------------------------------------------------------    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -