LazyInitFileLog.java revision 3265:b7583d50f67d
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.sjavac.server.log;
27
28import com.sun.tools.sjavac.Log;
29
30import java.io.FileWriter;
31import java.io.IOException;
32import java.io.PrintWriter;
33import java.nio.file.Files;
34import java.nio.file.Path;
35import java.nio.file.Paths;
36
37public class LazyInitFileLog extends Log {
38
39    String baseFilename;
40    Path destination = null;
41
42    public LazyInitFileLog(String baseFilename) {
43        super(null, null);
44        this.baseFilename = baseFilename;
45    }
46
47    protected void printLogMsg(Level msgLevel, String msg) {
48        try {
49            // Lazily initialize out/err
50            if (out == null && isLevelLogged(msgLevel)) {
51                destination = getAvailableDestination();
52                out = err = new PrintWriter(new FileWriter(destination.toFile()), true);
53            }
54            // Proceed to log the message
55            super.printLogMsg(msgLevel, msg);
56        } catch (IOException e) {
57            // This could be bad. We might have run into an error and we can't
58            // log it. Resort to printing on stdout.
59            System.out.println("IO error occurred: " + e.getMessage());
60            System.out.println("Original message: [" + msgLevel + "] " + msg);
61        }
62    }
63
64    /**
65     * @return The first available path of baseFilename, baseFilename.1,
66     * basefilename.2, ...
67     */
68    private Path getAvailableDestination() {
69        Path p = Paths.get(baseFilename);
70        int i = 1;
71        while (Files.exists(p)) {
72            p = Paths.get(baseFilename + "." + i++);
73        }
74        return p;
75    }
76
77    public Path getLogDestination() {
78        return destination;
79    }
80}
81