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