📄 logformatter.java
字号:
/*
* @(#)LogFormatter.java ver 1.2 6/20/2005
*
* Copyright 2005 Weishuai Yang (wyang@cs.binghamton.edu).
* All rights reserved.
*
*/
package gps.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.LogRecord;
/**
* formats log records
*
*
* @author Weishuai Yang
* @version 1.2, 6/20/2005
*/
public class LogFormatter extends java.util.logging.Formatter {
/**
* debug or not, if true includes real time
*/
boolean mDebug=false;
/**
* dummy constructor
*
*/
public LogFormatter(){ }
/**
* constructs a log formatter object
* @param debug debug or not
*/
public LogFormatter(boolean debug){
mDebug=debug;
}
/**
* Line separator string. This is the value of the line.separator
* property at the moment that the SimpleFormatter was created.
*
*/
private String lineSeparator = (String) java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction("line.separator"));
/**
* Format the given LogRecord.
* @param record the log record to be formatted.
* @return a formatted log record
*/
public synchronized String format(LogRecord record) {
StringBuffer sb = new StringBuffer();
String message = formatMessage(record);
if(mDebug){
sb.append(record.getLevel().getLocalizedName());
sb.append(": ");
}
sb.append(message);
sb.append(lineSeparator);
if (record.getThrown() != null) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
record.getThrown().printStackTrace(pw);
pw.close();
sb.append(sw.toString());
} catch (Exception ex) {
}
}
return sb.toString();
}
/**
* prints a formatted number following printf conventions
* @param fmt the format string
* @param x the double to print
* @return the formated string
*/
public static String sprintf(String fmt, double x)
{
return(new Format(fmt).form(x));
}
/**
* prints a formatted number following printf conventions
* @param fmt the format string
* @param x the long to print
* @return the formated string
*/
public static String sprintf(String fmt, long x)
{ return(new Format(fmt).form(x));
}
/**
* prints a formatted number following printf conventions
* @param fmt the format string
* @param x the character to
* @return the formated string
*/
public static String sprintf(String fmt, char x)
{ return(new Format(fmt).form(x));
}
/**
* prints a formatted number following printf conventions
* @param x a string that represents the digits to print
* @return the formated string
*/
public static String sprintf(String fmt, String x)
{ return(new Format(fmt).form(x));
}
/**
* a test stub for the format class
*/
public static void main(String[] a)
{ double x = 1.23456789012;
double y = 123;
double z = 1.2345e30;
double w = 1.02;
double u = 1.234e-5;
int d = 0xCAFE;
System.out.println(LogFormatter.sprintf("x = |%f|\n", x));
System.out.println(LogFormatter.sprintf("u = |%20f|\n", u));
System.out.println(LogFormatter.sprintf("x = |% .5f|\n", x));
System.out.println(LogFormatter.sprintf("w = |%20.5f|\n", w));
System.out.println(LogFormatter.sprintf("x = |%020.5f|\n", x));
System.out.println(LogFormatter.sprintf("x = |%+20.5f|\n", x));
System.out.println(LogFormatter.sprintf("x = |%+020.5f|\n", x));
System.out.println(LogFormatter.sprintf("x = |% 020.5f|\n", x));
System.out.println(LogFormatter.sprintf("y = |%#+20.5f|\n", y));
System.out.println(LogFormatter.sprintf("y = |%-+20.5f|\n", y));
System.out.println(LogFormatter.sprintf("z = |%20.5f|\n", z));
System.out.println(LogFormatter.sprintf("x = |%e|\n", x));
System.out.println(LogFormatter.sprintf("u = |%20e|\n", u));
System.out.println(LogFormatter.sprintf("x = |% .5e|\n", x));
System.out.println(LogFormatter.sprintf("w = |%20.5e|\n", w));
System.out.println(LogFormatter.sprintf("x = |%020.5e|\n", x));
System.out.println(LogFormatter.sprintf("x = |%+20.5e|\n", x));
System.out.println(LogFormatter.sprintf("x = |%+020.5e|\n", x));
System.out.println(LogFormatter.sprintf("x = |% 020.5e|\n", x));
System.out.println(LogFormatter.sprintf("y = |%#+20.5e|\n", y));
System.out.println(LogFormatter.sprintf("y = |%-+20.5e|\n", y));
System.out.println(LogFormatter.sprintf("x = |%g|\n", x));
System.out.println(LogFormatter.sprintf("z = |%g|\n", z));
System.out.println(LogFormatter.sprintf("w = |%g|\n", w));
System.out.println(LogFormatter.sprintf("u = |%g|\n", u));
System.out.println(LogFormatter.sprintf("y = |%.2g|\n", y));
System.out.println(LogFormatter.sprintf("y = |%#.2g|\n", y));
System.out.println(LogFormatter.sprintf("d = |%d|\n", d));
System.out.println(LogFormatter.sprintf("d = |%20d|\n", d));
System.out.println(LogFormatter.sprintf("d = |%020d|\n", d));
System.out.println(LogFormatter.sprintf("d = |%+20d|\n", d));
System.out.println(LogFormatter.sprintf("d = |% 020d|\n", d));
System.out.println(LogFormatter.sprintf("d = |%-20d|\n", d));
System.out.println(LogFormatter.sprintf("d = |%20.8d|\n", d));
System.out.println(LogFormatter.sprintf("d = |%x|\n", d));
System.out.println(LogFormatter.sprintf("d = |%20X|\n", d));
System.out.println(LogFormatter.sprintf("d = |%#20x|\n", d));
System.out.println(LogFormatter.sprintf("d = |%020X|\n", d));
System.out.println(LogFormatter.sprintf("d = |%20.8x|\n", d));
System.out.println(LogFormatter.sprintf("d = |%o|\n", d));
System.out.println(LogFormatter.sprintf("d = |%020o|\n", d));
System.out.println(LogFormatter.sprintf("d = |%#20o|\n", d));
System.out.println(LogFormatter.sprintf("d = |%#020o|\n", d));
System.out.println(LogFormatter.sprintf("d = |%20.12o|\n", d));
System.out.println(LogFormatter.sprintf("s = |%-20s|\n", "Hello"));
System.out.println(LogFormatter.sprintf("s = |%-20c|\n", '!'));
// regression test to confirm fix of reported bugs
System.out.println(LogFormatter.sprintf("|%i|\n", Long.MIN_VALUE));
System.out.println(LogFormatter.sprintf("|%6.2e|\n", 0.0));
System.out.println(LogFormatter.sprintf("|%6.2g|\n", 0.0));
System.out.println(LogFormatter.sprintf("|%6.2f|\n", 9.99));
System.out.println(LogFormatter.sprintf("|%6.2f|\n", 9.999));
System.out.println(LogFormatter.sprintf("|%6.0f|\n", 9.999));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -