filehandler.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 499 行 · 第 1/2 页
JAVA
499 行
/* FIXME: Javadoc missing. */
public FileHandler(String pattern, boolean append)
throws IOException, SecurityException
{
this(pattern,
/* limit */ 0,
/* count */ 1,
append);
}
/* FIXME: Javadoc missing. */
public FileHandler(String pattern, int limit, int count)
throws IOException, SecurityException
{
this(pattern, limit, count,
LogManager.getBooleanProperty(
"java.util.logging.FileHandler.append",
/* default */ false));
}
/**
* Constructs a <code>FileHandler</code> given the pattern for the
* location and name of the produced log files, the size limit, the
* number of log files thorough which the handler will rotate, and
* the <code>append</code> property. All other property values are
* taken from the current {@link LogManager LogManager}
* configuration.
*
* @param pattern The pattern for the location and name of the
* produced log files. See the section on <a
* href="#filePatterns">file name patterns</a> for details.
* If <code>pattern</code> is <code>null</code>, the value is
* taken from the {@link LogManager LogManager} configuration
* property
* <code>java.util.logging.FileHandler.pattern</code>.
* However, this is a pecularity of the GNU implementation,
* and Sun's API specification does not mention what behavior
* is to be expected for <code>null</code>. Therefore,
* applications should not rely on this feature.
*
* @param limit specifies the number of bytes a log file is
* approximately allowed to reach before it is closed and the
* handler switches to the next file in the rotating set. A
* value of zero means that files can grow without limit.
*
* @param count specifies the number of log files through which this
* handler cycles.
*
* @param append specifies whether the handler will append log
* records to existing files (<code>true</code>), or whether the
* handler will clear log files upon switching to them
* (<code>false</code>).
*
* @throws java.io.IOException FIXME: The Sun Javadoc says: "if
* there are IO problems opening the files." This conflicts
* with the general principle that configuration errors do
* not prohibit construction. Needs review.
*
* @throws SecurityException if a security manager exists and
* the caller is not granted the permission to control
* the logging infrastructure.
* <p>FIXME: This seems in contrast to all other handler
* constructors -- verify this by running tests against
* the Sun reference implementation.
*/
public FileHandler(String pattern,
int limit,
int count,
boolean append)
throws IOException, SecurityException
{
super(createFileStream(pattern, limit, count, append,
/* generation */ 0),
"java.util.logging.FileHandler",
/* default level */ Level.ALL,
/* formatter */ null,
/* default formatter */ XMLFormatter.class);
if ((limit <0) || (count < 1))
throw new IllegalArgumentException();
this.pattern = pattern;
this.limit = limit;
this.count = count;
this.append = append;
}
/* FIXME: Javadoc missing. */
private static java.io.OutputStream createFileStream(String pattern,
int limit,
int count,
boolean append,
int generation)
{
String path;
int unique = 0;
/* Throws a SecurityException if the caller does not have
* LoggingPermission("control").
*/
LogManager.getLogManager().checkAccess();
/* Default value from the java.util.logging.FileHandler.pattern
* LogManager configuration property.
*/
if (pattern == null)
pattern = LogManager.getLogManager().getProperty(
"java.util.logging.FileHandler.pattern");
if (pattern == null)
pattern = "%h/java%u.log";
do
{
path = replaceFileNameEscapes(pattern, generation, unique, count);
try
{
File file = new File(path);
if (file.createNewFile())
return new FileOutputStream(path, append);
}
catch (Exception ex)
{
ex.printStackTrace();
}
unique = unique + 1;
if (pattern.indexOf("%u") < 0)
pattern = pattern + ".%u";
}
while (true);
}
/**
* Replaces the substrings <code>"/"</code> by the value of the
* system property <code>"file.separator"</code>, <code>"%t"</code>
* by the value of the system property
* <code>"java.io.tmpdir"</code>, <code>"%h"</code> by the value of
* the system property <code>"user.home"</code>, <code>"%g"</code>
* by the value of <code>generation</code>, <code>"%u"</code> by the
* value of <code>uniqueNumber</code>, and <code>"%%"</code> by a
* single percent character. If <code>pattern<code> does
* <em>not</em> contain the sequence <code>"%g"</code>,
* the value of <code>generation</code> will be appended to
* the result.
*
* @throws NullPointerException if one of the system properties
* <code>"file.separator"</code>,
* <code>"java.io.tmpdir"</code>, or
* <code>"user.home"</code> has no value and the
* corresponding escape sequence appears in
* <code>pattern</code>.
*/
private static String replaceFileNameEscapes(String pattern,
int generation,
int uniqueNumber,
int count)
{
StringBuffer buf = new StringBuffer(pattern);
String replaceWith;
boolean foundGeneration = false;
int pos = 0;
do
{
// Uncomment the next line for finding bugs.
// System.out.println(buf.substring(0,pos) + '|' + buf.substring(pos));
if (buf.charAt(pos) == '/')
{
/* The same value is also provided by java.io.File.separator. */
replaceWith = System.getProperty("file.separator");
buf.replace(pos, pos + 1, replaceWith);
pos = pos + replaceWith.length() - 1;
continue;
}
if (buf.charAt(pos) == '%')
{
switch (buf.charAt(pos + 1))
{
case 't':
replaceWith = System.getProperty("java.io.tmpdir");
break;
case 'h':
replaceWith = System.getProperty("user.home");
break;
case 'g':
replaceWith = Integer.toString(generation);
foundGeneration = true;
break;
case 'u':
replaceWith = Integer.toString(uniqueNumber);
break;
case '%':
replaceWith = "%";
break;
default:
replaceWith = "??";
break; // FIXME: Throw exception?
}
buf.replace(pos, pos + 2, replaceWith);
pos = pos + replaceWith.length() - 1;
continue;
}
}
while (++pos < buf.length() - 1);
if (!foundGeneration && (count > 1))
{
buf.append('.');
buf.append(generation);
}
return buf.toString();
}
/* FIXME: Javadoc missing, implementation incomplete. */
public void publish(LogRecord record)
{
super.publish(record);
/* FIXME: Decide when to switch over. How do we get to
* the number of bytes published so far? Two possibilities:
* 1. File.length, 2. have metering wrapper around
* output stream counting the number of written bytes.
*/
/* FIXME: Switch over if needed! This implementation always
* writes into a single file, i.e. behaves as if limit
* always was zero. So, the implementation is somewhat
* functional but incomplete.
*/
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?