jdk/src/share/classes/java/util/logging/FileHandler.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7803 56bc97d69d93
child 9275 1df1f7dfab7f
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.FileLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Simple file logging <tt>Handler</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The <tt>FileHandler</tt> can either write to a specified file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * or it can write to a rotating set of files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * For a rotating set of files, as each file reaches a given size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * limit, it is closed, rotated out, and a new file opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Successively older files are named by adding "0", "1", "2",
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
    42
 * etc. into the base filename.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * By default buffering is enabled in the IO libraries but each log
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * record is flushed out when it is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * By default the <tt>XMLFormatter</tt> class is used for formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <b>Configuration:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * By default each <tt>FileHandler</tt> is initialized using the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <tt>LogManager</tt> configuration properties.  If properties are not defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * (or have invalid values) then the specified default values are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li>   java.util.logging.FileHandler.level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *        specifies the default level for the <tt>Handler</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *        (defaults to <tt>Level.ALL</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <li>   java.util.logging.FileHandler.filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *        specifies the name of a <tt>Filter</tt> class to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *        (defaults to no <tt>Filter</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <li>   java.util.logging.FileHandler.formatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *        specifies the name of a <tt>Formatter</tt> class to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *        (defaults to <tt>java.util.logging.XMLFormatter</tt>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <li>   java.util.logging.FileHandler.encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *        the name of the character set encoding to use (defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *        the default platform encoding).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <li>   java.util.logging.FileHandler.limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *        specifies an approximate maximum amount to write (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *        to any one file.  If this is zero, then there is no limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *        (Defaults to no limit).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <li>   java.util.logging.FileHandler.count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *        specifies how many output files to cycle through (defaults to 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <li>   java.util.logging.FileHandler.pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *        specifies a pattern for generating the output file name.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *        below for details. (Defaults to "%h/java%u.log").
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <li>   java.util.logging.FileHandler.append
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *        specifies whether the FileHandler should append onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *        any existing files (defaults to false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * A pattern consists of a string that includes the following special
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * components that will be replaced at runtime:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <li>    "/"    the local pathname separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <li>     "%t"   the system temporary directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <li>     "%h"   the value of the "user.home" system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <li>     "%g"   the generation number to distinguish rotated logs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <li>     "%u"   a unique number to resolve conflicts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <li>     "%%"   translates to a single percent sign "%"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * If no "%g" field has been specified and the file count is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * than one, then the generation number will be added to the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * the generated filename, after a dot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Thus for example a pattern of "%t/java%g.log" with a count of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * would typically cause log files to be written on Solaris to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * /var/tmp/java0.log and /var/tmp/java1.log whereas on Windows 95 they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * would be typically written to C:\TEMP\java0.log and C:\TEMP\java1.log
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * Generation numbers follow the sequence 0, 1, 2, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Normally the "%u" unique field is set to 0.  However, if the <tt>FileHandler</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * tries to open the filename and finds the file is currently in use by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * another process it will increment the unique number field and try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * again.  This will be repeated until <tt>FileHandler</tt> finds a file name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * is  not currently in use. If there is a conflict and no "%u" field has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * been specified, it will be added at the end of the filename after a dot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * (This will be after any automatically added generation number.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Thus if three processes were all trying to log to fred%u.%g.txt then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * they  might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * the first file in their rotating sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Note that the use of unique ids to avoid conflicts is only guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * to work reliably when using a local disk file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
public class FileHandler extends StreamHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private MeteredStream meter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private boolean append;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private int limit;       // zero => no limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private String pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private String lockFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private FileOutputStream lockStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private File files[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final int MAX_LOCKS = 100;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   130
    private static java.util.HashMap<String, String> locks = new java.util.HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    // A metered stream is a subclass of OutputStream that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    //   (a) forwards all its output to a target stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    //   (b) keeps track of how many bytes have been written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private class MeteredStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        MeteredStream(OutputStream out, int written) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            this.written = written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            written++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        public void write(byte buff[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            out.write(buff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            written += buff.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        public void write(byte buff[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            out.write(buff,off,len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            written += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private void open(File fname, boolean append) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (append) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            len = (int)fname.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        FileOutputStream fout = new FileOutputStream(fname.toString(), append);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        BufferedOutputStream bout = new BufferedOutputStream(fout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        meter = new MeteredStream(bout, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        setOutputStream(meter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // Private method to configure a FileHandler from LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // properties and/or default values as specified in the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // javadoc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private void configure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String cname = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        pattern = manager.getStringProperty(cname + ".pattern", "%h/java%u.log");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        limit = manager.getIntProperty(cname + ".limit", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (limit < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            limit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        count = manager.getIntProperty(cname + ".count", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (count <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        append = manager.getBooleanProperty(cname + ".append", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        setLevel(manager.getLevelProperty(cname + ".level", Level.ALL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        setFilter(manager.getFilterProperty(cname + ".filter", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        setFormatter(manager.getFormatterProperty(cname + ".formatter", new XMLFormatter()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            setEncoding(manager.getStringProperty(cname +".encoding", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                setEncoding(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            } catch (Exception ex2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // doing a setEncoding with null should always work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Construct a default <tt>FileHandler</tt>.  This will be configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * entirely from <tt>LogManager</tt> properties (or their default values).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @exception  IOException if there are IO problems opening the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *             the caller does not have <tt>LoggingPermission("control"))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception  NullPointerException if pattern property is an empty String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public FileHandler() throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        openFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Initialize a <tt>FileHandler</tt> to write to the given filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * properties (or their default values) except that the given pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * argument is used as the filename pattern, the file limit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * set to no limit, and the file count is set to one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * There is no limit on the amount of data that may be written,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * so use this with care.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param pattern  the name of the output file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception  IOException if there are IO problems opening the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception  IllegalArgumentException if pattern is an empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public FileHandler(String pattern) throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (pattern.length() < 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        this.limit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        this.count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        openFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Initialize a <tt>FileHandler</tt> to write to the given filename,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * with optional append.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * properties (or their default values) except that the given pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * argument is used as the filename pattern, the file limit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * set to no limit, the file count is set to one, and the append
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * mode is set to the given <tt>append</tt> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * There is no limit on the amount of data that may be written,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * so use this with care.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param pattern  the name of the output file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param append  specifies append mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @exception  IOException if there are IO problems opening the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception  IllegalArgumentException if pattern is an empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public FileHandler(String pattern, boolean append) throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (pattern.length() < 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        this.limit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        this.count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        this.append = append;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        openFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Initialize a <tt>FileHandler</tt> to write to a set of files.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * (approximately) the given limit has been written to one file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * another file will be opened.  The output will cycle through a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * of count files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * properties (or their default values) except that the given pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * argument is used as the filename pattern, the file limit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * set to the limit argument, and the file count is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * given count argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * The count must be at least 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param pattern  the pattern for naming the output file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param limit  the maximum number of bytes to write to any one file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param count  the number of files to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception  IOException if there are IO problems opening the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @exception IllegalArgumentException if limit < 0, or count < 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @exception  IllegalArgumentException if pattern is an empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public FileHandler(String pattern, int limit, int count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                        throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (limit < 0 || count < 1 || pattern.length() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        this.limit = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        this.count = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        openFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Initialize a <tt>FileHandler</tt> to write to a set of files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * with optional append.  When (approximately) the given limit has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * been written to one file, another file will be opened.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * output will cycle through a set of count files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * The <tt>FileHandler</tt> is configured based on <tt>LogManager</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * properties (or their default values) except that the given pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * argument is used as the filename pattern, the file limit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * set to the limit argument, and the file count is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * given count argument, and the append mode is set to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <tt>append</tt> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * The count must be at least 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param pattern  the pattern for naming the output file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param limit  the maximum number of bytes to write to any one file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param count  the number of files to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param append  specifies append mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @exception  IOException if there are IO problems opening the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @exception IllegalArgumentException if limit < 0, or count < 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @exception  IllegalArgumentException if pattern is an empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public FileHandler(String pattern, int limit, int count, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                        throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (limit < 0 || count < 1 || pattern.length() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        this.limit = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        this.count = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        this.append = append;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        openFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // Private method to open the set of output files, based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    // configured instance variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private void openFiles() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        manager.checkAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (count < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
           throw new IllegalArgumentException("file count = " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (limit < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            limit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // We register our own ErrorManager during initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // so we can record exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        InitializationErrorManager em = new InitializationErrorManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        setErrorManager(em);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // Create a lock file.  This grants us exclusive access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // to our set of output files, as long as we are alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        int unique = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            unique++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (unique > MAX_LOCKS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                throw new IOException("Couldn't get lock for " + pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            // Generate a lock file name from the "unique" int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            lockFileName = generate(pattern, 0, unique).toString() + ".lck";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            // Now try to lock that filename.
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   394
            // Because some systems (e.g., Solaris) can only do file locks
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // between processes (and not within a process), we first check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // if we ourself already have the file locked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            synchronized(locks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                if (locks.get(lockFileName) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    // We already own this lock, for a different FileHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    // object.  Try again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                FileChannel fc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    lockStream = new FileOutputStream(lockFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    fc = lockStream.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                } catch (IOException ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    // We got an IOException while trying to open the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    // Try the next file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    FileLock fl = fc.tryLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    if (fl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        // We failed to get the lock.  Try next file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    // We got the lock OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                } catch (IOException ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    // We got an IOException while trying to get the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    // This normally indicates that locking is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    // on the target directory.  We have to proceed without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    // getting a lock.   Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                // We got the lock.  Remember it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                locks.put(lockFileName, lockFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        files = new File[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            files[i] = generate(pattern, i, unique);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // Create the initial log file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (append) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            open(files[0], true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            rotate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        // Did we detect any exceptions during initialization?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        Exception ex = em.lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (ex instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                throw (IOException) ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            } else if (ex instanceof SecurityException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                throw (SecurityException) ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                throw new IOException("Exception: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        // Install the normal default ErrorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        setErrorManager(new ErrorManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    // Generate a filename from a pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private File generate(String pattern, int generation, int unique) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        File file = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        String word = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int ix = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        boolean sawg = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        boolean sawu = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        while (ix < pattern.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            char ch = pattern.charAt(ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            ix++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            char ch2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (ix < pattern.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                ch2 = Character.toLowerCase(pattern.charAt(ix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (ch == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                if (file == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    file = new File(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    file = new File(file, word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                word = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            } else  if (ch == '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                if (ch2 == 't') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    String tmpDir = System.getProperty("java.io.tmpdir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    if (tmpDir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        tmpDir = System.getProperty("user.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    file = new File(tmpDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    ix++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    word = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                } else if (ch2 == 'h') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    file = new File(System.getProperty("user.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    if (isSetUID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                        // Ok, we are in a set UID program.  For safety's sake
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        // we disallow attempts to open files relative to %h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        throw new IOException("can't use %h in set UID program");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    ix++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    word = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                } else if (ch2 == 'g') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    word = word + generation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    sawg = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    ix++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                } else if (ch2 == 'u') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    word = word + unique;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    sawu = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    ix++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                } else if (ch2 == '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    word = word + "%";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    ix++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            word = word + ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (count > 1 && !sawg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            word = word + "." + generation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (unique > 0 && !sawu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            word = word + "." + unique;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (word.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (file == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                file = new File(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                file = new File(file, word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        return file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    // Rotate the set of output files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private synchronized void rotate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        Level oldLevel = getLevel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        setLevel(Level.OFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        for (int i = count-2; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            File f1 = files[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            File f2 = files[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (f1.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if (f2.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    f2.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                f1.renameTo(f2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            open(files[0], false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        } catch (IOException ix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            // We don't want to throw an exception here, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            // report the exception to any registered ErrorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            reportError(null, ix, ErrorManager.OPEN_FAILURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        setLevel(oldLevel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Format and publish a <tt>LogRecord</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public synchronized void publish(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (!isLoggable(record)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        super.publish(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (limit > 0 && meter.written >= limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // We performed access checks in the "init" method to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // we are only initialized from trusted code.  So we assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            // it is OK to write the target files, even if we are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            // currently being called from untrusted code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            // So it is safe to raise privilege here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    rotate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Close all the files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @exception  SecurityException  if a security manager exists and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *             the caller does not have <tt>LoggingPermission("control")</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public synchronized void close() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        // Unlock any lock file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (lockFileName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            // Closing the lock file's FileOutputStream will close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            // the underlying channel and free any locks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            lockStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            // Problems closing the stream.  Punt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        synchronized(locks) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            locks.remove(lockFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        new File(lockFileName).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        lockFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        lockStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    private static class InitializationErrorManager extends ErrorManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        Exception lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        public void error(String msg, Exception ex, int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            lastException = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    // Private native method to check if we are in a set UID program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    private static native boolean isSetUID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
}