1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
33
34
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
86 Pattern debugBoolPat = Pattern.compile("(-D)?debug=(.*)");
87
88 Pattern logFilePat = Pattern.compile("(-D)?start-log-file=(.*)");
89
90
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
135 throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to create: " + startLog.getAbsolutePath()));
136 }
137
138 if (!startLog.canWrite())
139 {
140
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 }