View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.start;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.PrintStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.StandardOpenOption;
27  import java.util.Date;
28  
29  import org.eclipse.jetty.start.config.CommandLineConfigSource;
30  
31  /**
32   * Centralized Place for logging.
33   * <p>
34   * Because startup cannot rely on Jetty's Logging, an alternative logging is established.
35   * <p>
36   * Optional behavior is to create a ${jetty.base}/logs/start.log with whatever output the startup process produces.
37   */
38  public class StartLog
39  {
40      private final static PrintStream stdout = System.out;
41      private final static PrintStream stderr = System.err;
42      private static volatile PrintStream out = System.out;
43      private static volatile PrintStream err = System.err;
44      private final static StartLog INSTANCE = new StartLog();
45  
46      public static void debug(String format, Object... args)
47      {
48          if (INSTANCE.debug)
49          {
50              out.printf(format + "%n",args);
51          }
52      }
53  
54      public static void debug(Throwable t)
55      {
56          if (INSTANCE.debug)
57          {
58              t.printStackTrace(out);
59          }
60      }
61  
62      public static StartLog getInstance()
63      {
64          return INSTANCE;
65      }
66  
67      public static void info(String format, Object... args)
68      {
69          err.printf("INFO: " + format + "%n",args);
70      }
71  
72      public static void warn(String format, Object... args)
73      {
74          err.printf("WARNING: " + format + "%n",args);
75      }
76  
77      public static void warn(Throwable t)
78      {
79          t.printStackTrace(err);
80      }
81  
82      public static boolean isDebugEnabled()
83      {
84          return INSTANCE.debug;
85      }
86  
87      private boolean debug = false;
88  
89      public void initialize(BaseHome baseHome, CommandLineConfigSource cmdLineSource) throws IOException
90      {
91          String dbgProp = cmdLineSource.getProperty("debug");
92          if (dbgProp != null)
93          {
94              debug = Boolean.parseBoolean(dbgProp);
95          }
96  
97          String logFileName = cmdLineSource.getProperty("start-log-file");
98  
99          for (RawArgs.Entry arg : cmdLineSource.getArgs())
100         {
101             if ("--debug".equals(arg.getLine()))
102             {
103                 debug = true;
104                 continue;
105             }
106 
107             if (arg.startsWith("--start-log-file"))
108             {
109                 logFileName = Props.getValue(arg.getLine());
110                 continue;
111             }
112         }
113 
114         if (logFileName != null)
115         {
116             Path logfile = baseHome.getPath(logFileName);
117             initLogFile(logfile);
118         }
119     }
120 
121     public void initLogFile(Path logfile) throws IOException
122     {
123         if (logfile != null)
124         {
125             try
126             {
127                 Path logDir = logfile.getParent();
128                 FS.ensureDirectoryWritable(logDir);
129 
130                 Path startLog = logfile;
131 
132                 if (!FS.exists(startLog) && !FS.createNewFile(startLog))
133                 {
134                     // Output about error is lost in majority of cases.
135                     throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to create: " + startLog.toAbsolutePath()));
136                 }
137 
138                 if (!FS.canWrite(startLog))
139                 {
140                     // Output about error is lost in majority of cases.
141                     throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to write to: " + startLog.toAbsolutePath()));
142                 }
143 
144                 err.println("StartLog to " + logfile);
145                 OutputStream fileout = Files.newOutputStream(startLog,StandardOpenOption.CREATE,StandardOpenOption.APPEND);
146                 PrintStream logger = new PrintStream(fileout);
147                 out=logger;
148                 err=logger;
149                 System.setErr(logger);
150                 System.setOut(logger);
151                 err.println("StartLog Establishing " + logfile + " on " + new Date());
152             }
153             catch (IOException e)
154             {
155                 throw new UsageException(UsageException.ERR_LOGGING,e);
156             }
157         }
158     }
159 
160     public static void enableDebug()
161     {
162         getInstance().debug = true;
163     }
164     
165     public static void endStartLog()
166     {
167         if (stderr!=err && getInstance().debug)
168         {
169             err.println("StartLog ended");
170             stderr.println("StartLog ended");
171         }
172         System.setErr(stderr);
173         System.setOut(stdout);
174     }
175 }