View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.PrintStream;
25  import java.util.Date;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  /**
30   * Centralized Place for logging.
31   * <p>
32   * Because startup cannot rely on Jetty's Logging, an alternative logging is established.
33   * <p>
34   * Optional behavior is to create a ${jetty.base}/logs/start.log with whatever output the startup process produces.
35   */
36  public class StartLog
37  {
38      private final static StartLog INSTANCE = new StartLog();
39  
40      public static void debug(String format, Object... args)
41      {
42          if (INSTANCE.debug)
43          {
44              System.out.printf(format + "%n",args);
45          }
46      }
47  
48      public static void debug(Throwable t)
49      {
50          if (INSTANCE.debug)
51          {
52              t.printStackTrace(System.out);
53          }
54      }
55  
56      public static StartLog getInstance()
57      {
58          return INSTANCE;
59      }
60  
61      public static void info(String format, Object... args)
62      {
63          System.err.printf("WARNING: " + format + "%n",args);
64      }
65      
66      public static void warn(String format, Object... args)
67      {
68          System.err.printf("WARNING: " + format + "%n",args);
69      }
70  
71      public static void warn(Throwable t)
72      {
73          t.printStackTrace(System.err);
74      }
75      
76      public static boolean isDebugEnabled()
77      {
78          return INSTANCE.debug;
79      }
80  
81      private boolean debug = false;
82  
83      public void initialize(BaseHome baseHome, StartArgs args) throws IOException
84      {
85          // Debug with boolean
86          Pattern debugBoolPat = Pattern.compile("(-D)?debug=(.*)");
87          // Log file name
88          Pattern logFilePat = Pattern.compile("(-D)?start-log-file=(.*)");
89  
90          // TODO: support backward compatible --daemon argument ??
91  
92          Matcher matcher;
93          for (String arg : args.getCommandLine())
94          {
95              if ("--debug".equals(arg))
96              {
97                  debug = true;
98                  continue;
99              }
100 
101             matcher = debugBoolPat.matcher(arg);
102             if (matcher.matches())
103             {
104                 debug = Boolean.parseBoolean(matcher.group(2));
105                 continue;
106             }
107 
108             matcher = logFilePat.matcher(arg);
109             if (matcher.matches())
110             {
111                 String filename = matcher.group(2);
112                 File logfile = baseHome.getBaseFile(filename);
113                 initLogFile(logfile);
114             }
115         }
116     }
117 
118     public void initLogFile(File logfile) throws IOException
119     {
120         if (logfile != null)
121         {
122             File logDir = logfile.getParentFile();
123             if (!logDir.exists() || !logDir.canWrite())
124             {
125                 String err = String.format("Cannot write %s to directory %s [directory doesn't exist or is read-only]",logfile.getName(),
126                         logDir.getAbsolutePath());
127                 throw new UsageException(UsageException.ERR_LOGGING,new IOException(err));
128             }
129 
130             File startLog = logfile;
131 
132             if (!startLog.exists() && !startLog.createNewFile())
133             {
134                 // Output about error is lost in majority of cases.
135                 throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to create: " + startLog.getAbsolutePath()));
136             }
137 
138             if (!startLog.canWrite())
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.getAbsolutePath()));
142             }
143             PrintStream logger = new PrintStream(new FileOutputStream(startLog,false));
144             System.setOut(logger);
145             System.setErr(logger);
146             System.out.println("Establishing " + logfile + " on " + new Date());
147         }
148     }
149 }